有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用TestNG和Java(Selenium)进行并行测试

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

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

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

    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>

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

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

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


共 (1) 个答案

  1. # 1 楼答案

    我已经合并了你的设置。BrowserFactory类中的java类来临时编写代码。 以下是您的更新代码:

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