使用JUnit 4的Selenium / Appium并行执行

时间:2015-09-15 03:49:27

标签: multithreading selenium parallel-processing webdriver appium

尝试在多个设备上并行执行appium测试。想法就像启动JUnitRunner类作为参数化(例如deviceList),每个设备创建并行线程。从跑步者,通过JUnitCore.run调用TestSuite。问题是在测试用例中实例化驱动程序需要设备名称(可能来自Runner类),但JUnitCore不提供这样的选项(通过实例化来调用Suite / Test类)。有什么帮助吗?

代码如下:JUnitRunner.java

@RunWith(Parallelized.class) // extension of the Parameterized runner
    public class JUTRunner {    
    private String device;

    /**
     * @param device
     */
    public JUTRunner(String device) {
        this.device = device;
    }

    /**
     * @return
     */
    @Parameters
    public static Collection<Object[]> getParameters() {
        List<String> deviceList = findAllDevices();
        List<Object[]> parameters = new ArrayList<Object[]>(deviceList.size());

        for (String device : deviceList) {
            parameters.add(new Object[] { device });
        }
        return parameters;
    }

    /**
     * @return
     */
    private static List<String> findAllDevices() {
        return DeviceList.getInstance().getDeviceList();
    }

    /**
     * @throws InterruptedException
     */
    @Test
    public void testOnDevice() throws InterruptedException {
        Result result = JUnitCore.runClasses(JUTSuite.class);

        Result result = new JUnitCore().run(suite);     
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        if (result.wasSuccessful()) {
            System.out.println("All tests finished successfully...");
        }
    }
    }

和TestCase.java

public class MyTest extends TestCase {
    protected String device;
    protected AppiumDriver<MobileElement> driver;

    private int deviceNum;
    @Rule
    public TestName testName = new TestName();

    @Before
    protected void setUp() throws Exception {
        this.driver = new AndroidDriver(device).getDriver();
    }
    @Test
    public void testLogin() {
        System.out.println(testName.getMethodName() + device);
    }
    }

0 个答案:

没有答案