我正在尝试使用 Appium 和 TestNG 在两台真正的 Android 设备上进行并行 Web 浏览器测试。
我的代码:
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();
}
}
和我的测试NG 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 -->
当我将其作为testNG套件运行时,两个Appium服务器都会启动,并且每个移动设备都连接到预期的移动设备。但是,实际测试本身似乎只在 1 台设备上运行,通常是两次。如何让这些测试在两个单独的设备上同时运行?我错过了什么吗?我认为 udid 和 systemPort 功能将确保连接到正确服务器的每个设备,并且测试可以在两个设备上运行。
静态字段,我在并行TestNG + Appium + Android真实设备测试中遇到了同样的问题。
公共静态 Appium 驱动程序
尝试使驱动程序不是静态的。
您没有在 TestNG.xml 中提供线程计数来支持并行运行。请添加线程计数,如下所述。
<suite name="Suite" parallel="tests" thread-count="2" >