gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:34:37 25 4
gpt4 key购买 nike

我想实现并行测试,但似乎有些东西无法正常工作。当我为浏览器执行测试用例时,测试用例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. 当一个测试用例执行失败时,其他测试用例将被跳过。

最佳答案

我已将您的 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);
}

关于java - 使用 TestNG 和 Java (Selenium) 进行并行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60083943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com