简体   繁体   中英

Parallel Testing with TestNG and Java (Selenium)

I want to implement parallel testing, but seems something is not working properly. When I execute my test cases browser for browser the test cases are passing 100%... but when I implement parallel testing they rarely are passing but normally they are failing.

I'm executing my test cases on Eclipse IDE, and they are runing on Docker with Selenium grid.

this is my browser parallel testing class:

    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;
  }

}

this class is called setup.java and this class is calling the browser that I've put on my 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);

This is my 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>

I have 2 questions that I'd love that were answered in this post.

  1. How can I improve my code to run parallelly my test cases (I mean Firefox and Chrome at the same time with the same test cases)

  2. When one test case fails in the execution the other ones are being skipped.

I have merged your setup.java class in BrowserFactory class to improvised your code little bit. Here your updated code:

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);

}
}

update your xml accordingly.hope this helps you.

Option 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);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM