【问题标题】:Parallel Testing with TestNG and Java (Selenium)使用 TestNG 和 Java (Selenium) 进行并行测试
【发布时间】:2020-05-21 20:21:57
【问题描述】:

我想实现并行测试,但似乎有些东西不能正常工作。当我为浏览器执行测试用例浏览器时,测试用例 100% 通过...但是当我实施并行测试时,它们很少通过,但通常它们会失败。

我在 Eclipse IDE 上执行我的测试用例,它们在带有 Selenium 网格的 Docker 上运行。

这是我的浏览器并行测试类:

    public class BrowserFactory {

  private static final String FIREFOX = "firefox";
  private static final String CHROME = "chrome";
  private static final String SAFARI = "safari";
  private static final String IE = "internet explorer";

  private static String seleniumGridHub = UtlManageConfig.gethubURL();
  private static String weburl = UtlManageConfig.getWEBURL();

  public static DesiredCapabilities capabilities = null;
  public static MutableCapabilities options = null;


  public static WebDriver createInstance(String multiBrowser) throws MalformedURLException {
    WebDriver driver = null;
    try {

      switch(multiBrowser){

        case FIREFOX:
          FirefoxProfile profile = new FirefoxProfile();
          profile.setPreference("dom.disable_beforeunload", true);
          profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");             
          options = new FirefoxOptions();
          options.setCapability(FirefoxDriver.PROFILE, profile);
          options.setCapability("moz:webdriverClick", false);
          options.setCapability(CapabilityType.BROWSER_NAME, FIREFOX);

          URL server = new URL(seleniumGridHub);
          driver = new RemoteWebDriver(server, options);




          break;

        case CHROME:
          ChromeOptions chromeOptions = new ChromeOptions();
          chromeOptions.addArguments("--disable-popup-blocking");
          chromeOptions.setCapability(CapabilityType.BROWSER_NAME, CHROME);
          URL server2 = new URL(seleniumGridHub);
          driver = new RemoteWebDriver(server2, chromeOptions);


          break;

        case SAFARI:

          SafariOptions safariOptions = new SafariOptions();
          safariOptions.setUseTechnologyPreview(true);

          driver = new SafariDriver(safariOptions);
          break;

        default:
          InternetExplorerOptions ieOptions = new InternetExplorerOptions();
          driver = new InternetExplorerDriver(ieOptions);
          break;

      }

    }catch (Exception e) {
      e.getStackTrace();

      return driver;
    }

    return driver;
  }

}

这个类被称为setup.java,这个类正在调用我放在我的xml中的浏览器。

public WebDriver driver = null;
driver = BrowserFactory.createInstance(browser);
DriverFactory.getInstance().setDriver(driver);
      driver = DriverFactory.getInstance().getDriver();
      driver.get(weburl);

      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="books Test" parallel= "tests">
    <test name="Firefox Test">
        <parameter name="browser" value="firefox" />
        <groups>
            <run>
                <include name="books" />
                <include name="bookssell" />
            </run>
        </groups>
        <classes>
            <class
                name="books" />
        </classes>
    </test>
    <test name="Chrome Test">
        <parameter name="browser" value="chrome" />
        <groups>
            <run>
                <include name="books" />
                <include name="bookssell" />
            </run>
        </groups>
        <classes>
            <class
                name="books" />
        </classes>
    </test>
</suite>

我有 2 个问题希望在这篇文章中得到解答。

  1. 如何改进我的代码以并行运行我的测试用例(我的意思是 Firefox 和 Chrome 同时使用相同的测试用例)

  2. 当一个测试用例在执行中失败时,将跳过其他测试用例。

【问题讨论】:

  • 失败的原因是您的代码不是 Tread-Safe。您必须想出一种方法来使您的驱动程序实例同步。
  • @pdrersin,我该怎么做?这就是我问的原因,由于实例而无法正常工作,但我不知道该怎么做
  • 你应该查找ThreadLocal类。
  • 能否给我一些网址或指示

标签: java selenium webdriver testng


【解决方案1】:

我已将您的 setup.java 类合并到 BrowserFactory 类中,以稍微改进您的代码。 这是您更新的代码:

public class BrowserFactory {

  private static final String FIREFOX = "firefox";
  private static final String CHROME = "chrome";
  private static final String SAFARI = "safari";
  private static final String IE = "internet explorer";

  private static String seleniumGridHub = UtlManageConfig.gethubURL();
  private static String weburl = UtlManageConfig.getWEBURL();

  public static DesiredCapabilities capabilities = null;
  public static MutableCapabilities options = null;
  Public WebDriver driver;


  public static createInstance(String multiBrowser) throws MalformedURLException {

    try {

      switch(multiBrowser){

        case FIREFOX:
          FirefoxProfile profile = new FirefoxProfile();
          profile.setPreference("dom.disable_beforeunload", true);
          profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");             
          options = new FirefoxOptions();
          options.setCapability(FirefoxDriver.PROFILE, profile);
          options.setCapability("moz:webdriverClick", false);
          options.setCapability(CapabilityType.BROWSER_NAME, FIREFOX);

          URL server = new URL(seleniumGridHub);
          driver = new RemoteWebDriver(server, options);




          break;

        case CHROME:
          ChromeOptions chromeOptions = new ChromeOptions();
          chromeOptions.addArguments("--disable-popup-blocking");
          chromeOptions.setCapability(CapabilityType.BROWSER_NAME, CHROME);
          URL server2 = new URL(seleniumGridHub);
          driver = new RemoteWebDriver(server2, chromeOptions);


          break;

        case SAFARI:

          SafariOptions safariOptions = new SafariOptions();
          safariOptions.setUseTechnologyPreview(true);

          driver = new SafariDriver(safariOptions);
          break;

        default:
          InternetExplorerOptions ieOptions = new InternetExplorerOptions();
          driver = new InternetExplorerDriver(ieOptions);
          break;

      }

    }catch (Exception e) {
      e.getStackTrace();


    }


  }

  public static Browserlogin(String multiBrowser){
driver = BrowserFactory.createInstance(browser);
DriverFactory.getInstance().setDriver(driver);
      driver = DriverFactory.getInstance().getDriver();
      driver.get(weburl);

      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

}
}

相应地更新您的 xml。希望这对您有所帮助。

选项 2

public class BrowserFactory {

  private static final String FIREFOX = "firefox";
  private static final String CHROME = "chrome";
  private static final String SAFARI = "safari";
  private static final String IE = "internet explorer";

  private static String seleniumGridHub = UtlManageConfig.gethubURL();
  private static String weburl = UtlManageConfig.getWEBURL();

  public static DesiredCapabilities capabilities = null;
  public static MutableCapabilities options = null;
  protected static ThreadLocal<RemoteWebDriver> driver = new ThreadLocal<>();

 @BeforeMethod 
 @Parameters(value={"multiBrowser"})
 public static createInstance(String multiBrowser) throws MalformedURLException {

    try {

      switch(multiBrowser){

        case FIREFOX:
          FirefoxProfile profile = new FirefoxProfile();
          profile.setPreference("dom.disable_beforeunload", true);
          profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");             
          options = new FirefoxOptions();
          options.setCapability(FirefoxDriver.PROFILE, profile);
          options.setCapability("moz:webdriverClick", false);
          options.setCapability(CapabilityType.BROWSER_NAME, FIREFOX);

          URL server = new URL(seleniumGridHub);
          driver = new RemoteWebDriver(server, options);




          break;

        case CHROME:
          ChromeOptions chromeOptions = new ChromeOptions();
          chromeOptions.addArguments("--disable-popup-blocking");
          chromeOptions.setCapability(CapabilityType.BROWSER_NAME, CHROME);
          URL server2 = new URL(seleniumGridHub);
          driver = new RemoteWebDriver(server2, chromeOptions);


          break;

        case SAFARI:

          SafariOptions safariOptions = new SafariOptions();
          safariOptions.setUseTechnologyPreview(true);

          driver = new SafariDriver(safariOptions);
          break;

        default:
          InternetExplorerOptions ieOptions = new InternetExplorerOptions();
          driver = new InternetExplorerDriver(ieOptions);
          break;

      }

    }catch (Exception e) {
      e.getStackTrace();


    }


  }
  public WebDriver getDriver() {
        //Get driver from ThreadLocalMap
        return driver;
    }
      @AfterMethod
    public void tearDown() {
        getDriver().quit();
    }

}

@Test
Class setup extends BrowserFactory{


       getDriver().get(weburl);

      getDriver().manage().window().maximize();
      getDriver().manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 }

【讨论】:

  • 我有另一个类叫做:TestBooks 这个类已经实现了所有要运行的测试用例,它有所有的 TestNG 标签,比如@BeforeMethod @Test @afteClass 等等......我无法执行您与我分享的选项 2 的原因
猜你喜欢
  • 2022-01-23
  • 2021-10-22
  • 2019-03-18
  • 1970-01-01
  • 2016-04-25
  • 2020-11-06
  • 2014-04-04
  • 2018-03-24
  • 1970-01-01
相关资源
最近更新 更多