使用Appium在真实Android设备上进行并行测试

时间:2018-03-01 12:20:27

标签: java mobile automated-tests testng appium-android

我正在尝试使用Appium和TestNG在两个真正的Android设备上进行并行网络浏览器测试。

我的代码:

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();
}
}

我的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 -->

当我将其作为testNG套件运行时,两个Appium服务器都会启动,每个移动设备都会连接到预期的设备。但是,实际测试本身似乎只能在1台设备上运行,通常是两次。如何在两个独立的设备上同时运行这些测试?我错过了什么吗?我认为udid和systemPort功能可确保每个设备连接到正确的服务器,并且测试可以在两个设备上运行。

2 个答案:

答案 0 :(得分:0)

由于静态字段,我在并行TestNG + Appium + Android真实设备测试中遇到了相同的问题。

  

公共静态AppiumDriver驱动程序

尝试使驱动程序不是静态的。

答案 1 :(得分:0)

您没有在 TestNG.xml 中提供线程数来支持并行运行。请添加如下所述的线程数。

<suite name="Suite" parallel="tests" thread-count="2" >