Hi Team
I need help with how to run multiple test classes in one single session of appium i.e in one single session of appium server multiple test scripts would run i.e the app launches once and multiple test scripts will run under a single session.
I am sharing with you mobile base class and mobile setup class Please guide how to proceed
Mobile Base class:
public class MobileBaseClass {
public static AppiumDriver driver;
public static FlutterFinder find;
public static ChromeDriver wdriver;
public static FirefoxDriver fdriver;
public static EdgeDriver edriver;
protected static AppiumDriverLocalService service;
public static AppiumServiceBuilder builder;
@BeforeSuite
@Parameters({ "portNumber", "executionOn" })
public void beforeClassSetup(String portNumber, String executionOn) throws IOException, Exception {
// Runtime.getRuntime().exec("taskkill /F /IM node.exe");
if (executionOn.equals("mobile")) {
startService(portNumber);
} else {
System.out.println("Web Execution Started");
}
}
@BeforeClass
@Parameters({ "executionOn", "deviceName", "platformName", "platformVersion", "portNumber", "browser" })
public void setUp(String executionOn, String deviceName, String platformName, String platformVersion,
String portNumber, String browser) throws Exception {
logger = LogManager.getLogger(BaseClass.class);
if ((executionOn.equals("mobile")) && (platformName.equals("Flutter"))) {
MobileSetup mobileS = new MobileSetup();// to be changed
mobileS.mobileAndroidSetup(deviceName, platformVersion);
FlutterFinder find = new FlutterFinder(driver);
mobileS.appPopupClicks();
// mobileS.mpinSet();
} else {
System.out.println("Execution on is not valid. Expected Values are web or mobile");
}
}
@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
/*
* if (wdriver != null) { wdriver.quit();
*
* }
*/
}
@AfterSuite
@Parameters({ "portNumber", "executionOn" })
public void AfterClassTearDown(String portNumber, String executionOn) throws IOException {
if (executionOn.equals("mobile")) {
String packageName = "com.offlineplay";
String command = "adb uninstall " + packageName;
// Execute the command
Process process = Runtime.getRuntime().exec(command);
service.stop();
} // Appium Server stop
// Device close
}
void startService(String portNumber) throws Exception {
AppiumServiceBuilder builder = new AppiumServiceBuilder();
// Tell builder where node is installed. Or set this path in an environment
// variable named NODE_PATH
builder.usingDriverExecutable(new File(AppiumNodePath));
// Tell builder where Appium is installed. Or set this path in an environment
// variable named APPIUM_PATH
builder.withAppiumJS(new File(AppiumPath));
builder.withIPAddress("127.0.0.1");
builder.usingPort(Integer.parseInt(portNumber));
HashMap<String, String> environment = new HashMap<String, String>();
environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
builder.withEnvironment(environment);
builder.withArgument(() -> "--base-path", "/wd/");
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withLogFile(new File(System.getProperty("user.dir") + "/target/resources/appium_server_logs"
+ portNumber + Thread.currentThread().getId()));
System.out.println("SFDSDFSDG" + builder.toString());
service = AppiumDriverLocalService.buildService(builder);
System.out.println("hi " + builder);
service.start();
Thread.sleep(20000);
System.out.println("After Service Start URL: " + service.getUrl());
}
public static String getExceptionAfterMethodRun(ITestResult result) {
String exceptionAsString = null;
try {
StringWriter sw = new StringWriter();
result.getThrowable().printStackTrace(new PrintWriter(sw));
exceptionAsString = sw.toString();
exceptionAsString = StringUtils.substringBefore(exceptionAsString, "at com.");
} catch (Exception e) {
System.out.println("Exception encountered while gettinh exception after Test Run");
}
return exceptionAsString;
}
}
Mobile setup class:
public class MobileSetup extends MobileBaseClass {
//AppiumFlutterDriver driver1;
public void mobileAndroidSetup(String deviceName, String platformVersion) {
System.out.println("Android Mobile Setup");
try {
// mvn clean test -Dtestngfile=TestNG.xml
// runAppiumService(portNumber);
UiAutomator2Options options = new UiAutomator2Options();
// DesiredCapabilities dc = new DesiredCapabilities();
options.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);// deviceName
options.setCapability(MobileCapabilityType.UDID, deviceName);
options.setCapability("autoGrantPermissions", true);
// dc.setCapability(MobileCapabilityType.PLATFORM_VERSION,platformVersion);
options.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
options.setCapability(MobileCapabilityType.APP,
System.getProperty("user.dir") + "/src/test/resources/apps/" + AppName);
// options.setCapability("automationName","UiAutomator2");
options.setCapability("automationName", "Flutter");
options.setCapability("unicodeKeyboard", false);
options.setCapability("flutter:enableFlutterDriver", true);
options.setCapability("resetKeyboard", false);
options.setCapability("printPageSourceOnFindFailure", true);
options.setCapability("noReset", false);
options.setCapability("appium:plugins", "{\"appium-gestures-plugin\": {\"enabled\": true}}");
//options.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
// dc.setCapability("forwardingPort ","45761");
// dc.setCapability("autoWebview", true);
// dc.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
// dc.setCapability("appWaitForLaunch", false);
//options.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
options.setCapability("newCommandTimeout", 600);
options.setChromedriverExecutable(
"C:\\Users\\Mobile.Automation\\.appium\\node_modules\\appium-flutter-driver\\node_modules\\appium-chromedriver\\chromedriver\\win\\chromedriver.exe"
);
driver = new AndroidDriver(service.getUrl(), options);
// driver1 = new AppiumFlutterDriver(service.getUrl(), options);
FlutterFinder find = new FlutterFinder(driver);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// appPopupClicks();
// mpinSet();
} catch (Exception exp) {
// TODO: handle exception
System.out.println("Cause is :" + exp.getCause());
System.out.println("Message is :" + exp.getMessage());
exp.printStackTrace();
}
}
public void flutterfinderinitialize() throws InterruptedException {
FlutterFinder find = new FlutterFinder(driver);
Thread.sleep(10000);
}
public void appPopupClicks() throws InterruptedException {
FlutterFinder find = new FlutterFinder(driver);
Thread.sleep(20000);
FlutterElement ElevatedButton = find.byValueKey("Continue");
ElevatedButton.click();
Thread.sleep(10000);
}
}