大家好,我有一个 TestNG 测试套件,它按顺序执行测试。但在开始运行测试之前。它会打开所需数量的浏览器,然后依次运行测试。我想更改此行为以打开一个浏览器,运行测试并关闭浏览器。接下来打开另一个浏览器,运行测试并关闭等等。这可能吗?
我在 IntelliJ 上使用 TestNG、JAVA。示例测试套件:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="false" preserve-order="true" thread-count="1">
<test name="something1" group-by-instances="true">
<classes>
<class name="com.this.is.First"/>
</classes>
</test>
<test name="something2" group-by-instances="true">
<classes>
<class name="com.this.is.Second"/>
</classes>
</test>
<test name="something3" group-by-instances="true">
<classes>
<class name="com.this.is.Third"/>
</classes>
</suite>
所有测试都继承 Main 类,该类具有 @BeforeClass 和 @AfterClass 方法,我在其中实例化浏览器。
每个类别可以有多个测试,有时只有一个。
根据要求主类:
public class MainBeforeAfter
{
protected WebDriver driver;
protected String testUrl = "someurl";
public MainBeforeAfter()
{
if (System.getProperty("webdriver.chrome.driver") == null)
{
System.setProperty("webdriver.chrome.driver", "C:/Drivers/chromedriver_win32/chromedriver.exe");
}
if (System.getProperty("test.url") != null)
{
testUrl = System.getProperty("test.url");
}
System.out.println(System.getProperty("webdriver.chrome.driver"));
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
driver = new ChromeDriver(options);
//driver = new ChromeDriver();
}
@BeforeClass public void beforeClass() throws InterruptedException
{
driver.get(testUrl);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
enterLogin();
}
protected void enterLogin()
{
driver.findElement(By.name("username")).sendKeys("user");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("password")).sendKeys(Keys.ENTER);
}
@AfterClass public void afterClass() throws InterruptedException
{
driver.close();
try
{
driver.quit();
}
catch (Exception e)
{
System.out.println("Unable to close browser after login header exception caught: " + e);
}
}
}
最佳答案
尝试这样:
这应该可以解决问题,就在类后面的标签名称内
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" preserve-order="true">
<test name="something1">
<classes>
<class name="com.this.is.First"/>
<class name="com.this.is.Second"/>
<class name="com.this.is.Third"/>
</classes>
</suite>
希望对你有帮助
关于java - 如何让 TestNG 一次打开一个浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514680/