使用线程概念java在多个设备中并行执行

时间:2016-04-30 18:41:37

标签: java multithreading testng appium concept

我是java的新手。请帮忙。 我有一个要求,我需要在java中的多个设备中执行相同的测试场景。 此外,该要求说我的代码不应该只在一个devive中执行,它应该一次在多个设备中启动相同的场景。

是否可以一次执行。

注意: 我一次连接了3个设备。 但它会在其他设备之后执行相同的方案。

我正在使用appium server和testng

请帮忙.. 提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

public void startAppium() {
    //start appium instance
    try {
        Thread.sleep((long)(Math.random() * 10000)); //wait from 0 to 10 sec for parallel process run
        ProcessBuilder builder = new ProcessBuilder(getCmd());
        builder.redirectOutput("path to log file"); //here you can find logs of appium
        builder.redirectErrorStream(true);
        appium = builder.start();
        Thread.sleep(3000); //wait 3 sec until server started
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

public class Test{

    WebDriver driver = null;
    int timeOut=180;
    int port=-1;

    Test(int port){
        this.port=port;
    }

    public void testEmulator(int p) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
        capabilities.setCapability(CapabilityType.VERSION, "4.3");
        capabilities.setCapability(CapabilityType.PLATFORM, "Windows");

        capabilities.setCapability("udid","emulator-"+p);

        capabilities.setCapability("app-package", "your.app.pkg");
        capabilities.setCapability("app-activity",
            "your.app.pkg.Activity");
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:"+this.port+"/wd/hub"),
        capabilities);
        driver.manage().timeouts().implicitlyWait(timeOut, TimeUnit.SECONDS);
        Thread.sleep(50000);
    }

    public void tearDown() {
        if (driver != null)
            driver.quit();
    }

    public void runTest() {
        try {
            testEmulator(5554);     // for emulator on port 5554
            tearDown();
            testEmulator(5556);     // for emulator on port 5556
            tearDown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        new Test(4723).runTest();       // appium server port 4723
    }
}