我想实现并行测试,但似乎有些东西没有正常工作。当我为浏览器执行测试用例浏览器时,测试用例将通过100%.但是当我实现并行测试时,它们很少通过,但通常是失败的。
我正在Eclipse上执行我的测试用例,它们运行在带有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>我有两个问题,我很想在这篇文章中得到解答。
发布于 2020-02-06 01:22:46
我已经将您的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 accordingly.hope,这对您有帮助。
选项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);
}https://stackoverflow.com/questions/60083943
复制相似问题