Created
July 15, 2022 16:29
-
-
Save SarahElson/a2a58e2dfc72260b67dede5e37035136 to your computer and use it in GitHub Desktop.
Automated App Testing Using Appium With TestNG [Tutorial]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lambdatest.appium.sample; | |
import static java.text.MessageFormat.format; | |
import java.io.File; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import com.lambdatest.appium.sample.enums.Environment; | |
import com.lambdatest.appium.sample.utils.Swipe; | |
import io.appium.java_client.AppiumDriver; | |
import io.appium.java_client.MobileElement; | |
import io.appium.java_client.remote.AndroidMobileCapabilityType; | |
import io.appium.java_client.remote.AutomationName; | |
import io.appium.java_client.remote.MobileCapabilityType; | |
import io.appium.java_client.service.local.AppiumDriverLocalService; | |
import io.appium.java_client.service.local.AppiumServiceBuilder; | |
import io.appium.java_client.service.local.flags.GeneralServerFlag; | |
import org.openqa.selenium.Capabilities; | |
import org.openqa.selenium.remote.CapabilityType; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
import org.testng.ITestContext; | |
import org.testng.annotations.AfterTest; | |
// 1. | |
public class BaseTest<D extends AppiumDriver<MobileElement>> { | |
// 2. | |
protected static final String LT_KEY = System.getenv ("LT_ACCESS_KEY"); | |
protected static final String LT_USER = System.getenv ("LT_USERNAME"); | |
// 3. | |
protected D driver; | |
protected AppiumDriverLocalService service; | |
protected Swipe<D> swipe; | |
protected WebDriverWait wait; | |
// 4. | |
@AfterTest (alwaysRun = true) | |
public void tearDown (final ITestContext context) { | |
if (Environment.valueOf (context.getCurrentXmlTest () | |
.getParameter ("environment")) == Environment.CLOUD) { | |
final var status = (context.getFailedTests () | |
.size () > 0) ? "failed" : "passed"; | |
this.driver.executeScript (format ("lambda-status={0}", status)); | |
} | |
this.driver.quit (); | |
if (this.service != null && this.service.isRunning ()) { | |
this.service.stop (); | |
} | |
} | |
// 5. | |
protected Capabilities getOptions (final Environment environment, final String platform, final String deviceName, | |
final String version, final String appKey) { | |
final var app = environment == Environment.CLOUD | |
? System.getenv (appKey) | |
: System.getProperty ("user.dir") + appKey; | |
final DesiredCapabilities capabilities = new DesiredCapabilities (); | |
capabilities.setCapability (CapabilityType.PLATFORM_NAME, platform); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment