I am trying to conduct parallel web browser tests on two real Android devices using Appium and TestNG.
My code:
public class ParaTests {
private AppiumDriverLocalService service;
private AppiumServiceBuilder builder;
private AppiumDriverLocalService service2;
private AppiumServiceBuilder builder2;
private DesiredCapabilities cap;
public static AppiumDriver<WebElement> driver;
@BeforeSuite
public void startServer() {
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4726);
//Start the first server with the builder
service = AppiumDriverLocalService.buildService(builder);
service.start();
builder2 = new AppiumServiceBuilder();
builder2.withIPAddress("127.0.0.1");
builder2.usingPort(4727);
//Start the second server with the builder
service2 = AppiumDriverLocalService.buildService(builder2);
service2.start();
}
@BeforeTest
@org.testng.annotations.Parameters("device")
public void setUp(String device) throws MalformedURLException {
if(device.equalsIgnoreCase("pixel 2")){
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Pixel 2");
capabilities.setCapability("platformVersion", "8.1.0");
capabilities.setCapability("udid", "Fty4654541A00532");
capabilities.setCapability("systemPort", "4726");
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("deviceOrientation", "portrait");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4726/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
}
if(device.equalsIgnoreCase("nexus 5")){
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Nexus 5");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("udid", "039fsef3543g8");
capabilities.setCapability("systemPort", "4727");
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("deviceOrientation", "portrait");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4727/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
}
}
@Test
public void mobileTest() throws MalformedURLException{
driver.get("https://www.testing7858494.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
driver.findElementByClassName("mobile-menu-icon").click();
jse.executeScript("window.scrollBy(0,400)", "");
driver.findElementById("menu-item-613").click();
jse.executeScript("window.scrollBy(0,800)", "");
driver.findElementById("input_6_1").sendKeys("Test");
driver.findElementById("input_6_7").sendKeys("123456789");
driver.findElementById("input_6_3").sendKeys("test1234567.com");
driver.findElementById("input_6_4").sendKeys("Automated mobile web browser testing");
System.out.println("Script has finished execution.");
driver.quit();
}
@AfterTest
public void stopServer() {
service.stop();
service2.stop();
}
}
And my testNG XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<test name="Pixel2Test">
<parameter name="device" value="pixel 2" />
<classes>
<class name="Testing.ParaTests" />
</classes>
</test>
<test name="NexusTest">
<parameter name="device" value="nexus 5" />
<classes>
<class name="Testing.ParaTests" />
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
When i run this as a testNG suite, both Appium servers start and each of the mobile devices connect to the expected ones. However, the actual test itself only seems to run on 1 device, often two times. How do i get these tests to run at the same time on both separate devices? Am i missing something? I thought that the udid and systemPort capabilities would make sure the each device connected to the correct server and the test could be run on both devices.