首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用TestNG和Java并行测试(Selenium)

用TestNG和Java并行测试(Selenium)
EN

Stack Overflow用户
提问于 2020-02-05 20:51:53
回答 1查看 244关注 0票数 0

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

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

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

代码语言:javascript
复制
    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上安装的浏览器。

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>

我有两个问题,我很想在这篇文章中得到解答。

  1. 如何改进我的代码以并行运行我的测试用例(我指的是火狐和Chrome同时运行同一个测试用例)
  2. 当一个测试用例在执行失败时,另一个测试用例被跳过。
EN

回答 1

Stack Overflow用户

发布于 2020-02-06 01:22:46

我已经将您的setup.java类合并到BrowserFactory类中,以临时编写您的代码。在这里,您的更新代码:

代码语言:javascript
复制
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 accordingly.hope,这对您有帮助。

选项2

代码语言:javascript
复制
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);
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60083943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档