gpt4 book ai didi

java - 如何使用 testng 并行运行我的 Selenium 测试方法

转载 作者:行者123 更新时间:2023-11-29 06:51:32 25 4
gpt4 key购买 nike

我正在尝试使用 testng 并行运行我的自动化测试(Selenium webdriver)。这是我正在运行的节点:

java -Dwebdriver.gecko.driver=chromedriver.exe -jar selenium-server-standalone-3.4.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=chrome,maxInstances=2 -maxSession 2

这是我的测试类:

public class TestParallel {

Login login;

//@BeforeMethod(alwaysRun = true)
public SeleniumDriverCore testSetup() throws FileNotFoundException, IOException{
SeleniumDriverCore driver = new SeleniumDriverCore("config/chromeDriverConfig");
Properties config = new Properties();
config.load(new FileInputStream("config/testConfig"));
this.login = new Login(driver);
driver.browser.open("https://test.test.xyz");

driver.browser.maximize();
driver.waits.waitForPageToLoad();
return driver;
}

@Test(groups={"parallel"})
public void test_one() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}

@Test(groups={"parallel"})
public void test_two() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}

@Test(groups={"parallel"})
public void test_three() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}

@Test(groups={"parallel"})
public void test_four() throws FileNotFoundException, IOException{
SeleniumDriverCore driver=testSetup();
login.navigateToPage(Pages.LOGIN);
login.assertion.verifyLoginPopupAndTitleDisplayed();
testCleanup(driver);
}


public void testCleanup(SeleniumDriverCore driver){
driver.close();
driver.quit();
}

}

这是我的 xml:

<suite name="Ontega - All Tests Mobile" parallel="methods" thread-count="2">
<test name="Ontega - All Tests Mobile">
<groups>
<run>
<include name="parallel"/>
<exclude name="open-defects"/>
</run>
</groups>
<packages>
<package name="tests.*"/>
</packages>
</test>
</suite>

当我运行 XML 时,我希望测试一次在两个线程中的两个浏览器上运行,但是当我运行 XML 时,我第一次运行两个浏览器实例,然后它们递增并50% 的测试失败了,正如您所看到的,我正在尝试在每个方法中实例化驱动程序,尽管这不是我的框架的工作方式,但我正在尝试解决这个问题的瓶颈。任何帮助将不胜感激提前致谢

最佳答案

以下是在 TestNG 中执行此操作的一些方法。您基本上通过 @BeforeMethod@AfterMethod 配置方法来管理 Webdriver 实例化和清理。因此,您需要决定如何与您的 @Test 方法共享创建的 webdriver 实例。为此,您有三个选择:

  1. 您使用 ThreadLocal 变体,因为 TestNG 向您保证它将执行 @BeforeMethod@Test @AfterMethod 全部在同一个线程中。

这是一个示例,向您展示了这一点

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClassSampleUsingThreadLocal {
private static final ThreadLocal<RemoteWebDriver> drivers = new ThreadLocal<>();

@BeforeMethod
public void instantiateBrowser(ITestResult testResult) {
RemoteWebDriver driver = new ChromeDriver();
drivers.set(driver);
}

@Test(dataProvider = "dp")
public void testMethod(String url) {
Reporter.log("Launching the URL [" + url + "] on Thread [" + Thread.currentThread().getId() + "]", true);
driver().get(url);
Reporter.log("Page Title :" + driver().getTitle(), true);
}

@DataProvider(name = "dp", parallel = true)
public Object[][] getData() {
return new Object[][]{
{"http://www.google.com"}, {"http://www.stackoverflow.com"}, {"http://facebook.com"}
};
}

@AfterMethod
public void cleanupBrowser() {
RemoteWebDriver driver = driver();
driver.quit();
}

private RemoteWebDriver driver() {
RemoteWebDriver driver = drivers.get();
if (driver == null) {
throw new IllegalStateException("Driver should have not been null.");
}
return driver;
}

}
  • 您可以通过 ITestResult 对象共享 webdriver 实例。下面是一个示例,展示了这一点的实际效果。
  • import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.ITestResult;
    import org.testng.Reporter;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

    public class TestClassSample {
    private static final String WEBDRIVER = "driver";

    @BeforeMethod
    public void instantiateBrowser(ITestResult testResult) {
    RemoteWebDriver driver = new ChromeDriver();
    testResult.setAttribute(WEBDRIVER, driver);
    }

    @Test(dataProvider = "dp")
    public void testMethod(String url) {
    Reporter.log("Launching the URL [" + url + "] on Thread [" + Thread.currentThread().getId() + "]", true);
    driver().get(url);
    Reporter.log("Page Title :" + driver().getTitle(), true);
    }

    @DataProvider(name = "dp", parallel = true)
    public Object[][] getData() {
    return new Object[][]{
    {"http://www.google.com"},
    {"http://www.stackoverflow.com"},
    {"http://facebook.com"}
    };
    }

    @AfterMethod
    public void cleanupBrowser(ITestResult testResult) {
    RemoteWebDriver driver = driver(testResult);
    driver.quit();
    }

    private RemoteWebDriver driver() {
    return driver(Reporter.getCurrentTestResult());
    }

    private RemoteWebDriver driver(ITestResult testResult) {
    if (testResult == null) {
    throw new IllegalStateException("testResult should have not been null.");
    }
    Object driverObject = testResult.getAttribute(WEBDRIVER);
    if (driverObject == null) {
    throw new IllegalStateException("Driver should have not been null.");
    }
    if (!(driverObject instanceof RemoteWebDriver)) {
    throw new IllegalStateException("Driver is not a valid webdriver object");
    }
    return (RemoteWebDriver) driverObject;
    }
    }
  • 您将 Webdriver 实例化和清理提取到 TestNG 监听器中(实现了 org.testng.IInvokedMethodListener 的监听器,它将创建的 Webdriver 设置到 ITestResult 中(如图所示)在选项 2 中)或进入 ThreadLocal(如选项 1 中所示)。您可以在我的 blog post 中找到有关此选项的更多详细信息以及代码片段。
  • 关于java - 如何使用 testng 并行运行我的 Selenium 测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46698136/

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