本文介绍了使用 testNG 在多个浏览器实例中并行运行一个类中的多个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我使用 Web-driver + TestNG 框架的休闲场景的示例代码.

Please tell me sample code for the fallowing scenario using Web-driver + TestNG framework.

一个类有多个测试.在运行这些测试时,它应该一次在同一浏览器(例如:Firefox)的多个实例中并行运行所有测试.因此,对于每个测试,它都应该打开一个新的浏览器实例.

One class having the multiple tests. While running these tests it should run all the tests as parallel in multiple instances of same browser (Ex: Firefox) at a time. So for every test it should open a new instance of a browser.

推荐答案

到目前为止,我的解决方案是这样的:

My solution so far has been something like this:

public MyTestClass {
   SupportedBrowser browser;
   private static ThreadLocal<WebDriver> threadLocalDriver = new ThreadLocal<WebDriver>();

   @DataProvider (name = "getBrowsers")
   public Object[][] getBrowsers {
      return Object[][] {
          {
              SupportedBrowser.FIREFOX;
          },
          {
              SupportedBrowser.CHROME;
          }
      };
   }

   @Factory (dataProvider = "getBrowsers")
   public MyTestClass(SupportedBrowser browser) {
      this.browser = browser;
   }

   @BeforeMethod
   public void setup()
   {
      threadLocalDriver.set(browser.getDriver());
   }
   @Test
   public void test1()
   {
       WebDriver driver = threadLocalDriver.get();
       //do stuff
   }
   @AfterMethod
   public void tearDown()
   {
       WebDriver driver = threadLocalDriver.get();
       driver.quit();
   }
}

这是我的枚举:

public enum SupportedBrowser {
    FIREFOX, CHROME;  //add more as needed

    public getDriver() {
       if(this == SupportedBrowser.FIREFOX) {
           return new RemoteDriver(hubAddress, DesiredCapabilities.firefox());  //alternatively could be new FirefoxDriver()
       }
       else {
           return new RemoteDriver(hubAddress, DesiredCapabilities.chrome());
       }

    }
}

请原谅糟糕的代码约定,我没有在 IDE 中编写它(尽管我使用过类似的东西并且它有效).该类针对每个不同的浏览器运行一次.每种方法都有自己独立的驱动程序,确保您的测试可以同时运行.它还允许每个方法采用自己的 DataProvider,以防您需要使用不同参数多次运行测试方法.还要确保在 testng.xml 文件中将 parallel 属性设置为方法"级别.

Please forgive bad code conventions, I didn't write this in an IDE (though I have used something like this and it works).The class is run once fore each different browser. Each method has its own independent driver, making sure your test can run concurrently. It also allows each method to take its own DataProvider, in case you need a test method to run multiple times with different arguments. Also make sure that the parallel attribute is set to the "method" level in your testng.xml file.

我的代码的唯一问题是确保驱动程序在测试失败时退出.现在,此方法将关闭失败的测试留给 selenium 网格(使用 -timeout).请看我的问题:在TestNG中@BeforeMethod和@AfterMethod之间共享驱动.

The only issue with my code is making sure the driver quits if the test fails. Right now, this method leaves closing failed tests up to selenium grid (using -timeout). Please see my question: Sharing driver between @BeforeMethod and @AfterMethod in TestNG.

我现在已将 ThreadLocal 变量添加到在整个线程中共享驱动程序的代码中,因此您可以在 @AfterMethod 中调用 driver.quit().

I have now added a ThreadLocal variable to the code that shares the driver throughout the thread, so you can call driver.quit() in the @AfterMethod.

这篇关于使用 testNG 在多个浏览器实例中并行运行一个类中的多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 11:31