如何在Selenium IntelliJ中运行每个测试用例后关闭窗口?
我有一组三个测试用例,我希望避免打开多个浏览器窗口,因为我用Selenium自动化了这个过程。是否有一种方法可以在每个测试用例完成后关闭浏览器而不给出错误如何在Selenium IntelliJ中运行每个测试用例后关闭窗口?,selenium,intellij-idea,Selenium,Intellij Idea,我有一组三个测试用例,我希望避免打开多个浏览器窗口,因为我用Selenium自动化了这个过程。是否有一种方法可以在每个测试用例完成后关闭浏览器而不给出错误 使用close()和quit()都会给出1的错误代码。我们使用下面的方法来处理这些问题 1) 创建一个基类,该基类具有将始终运行的beforeSuite、beforeTest、afterTest和afterSuite方法 2) 每个测试计划都应该扩展这个类来创建驱动程序并关闭驱动程序 BasePage.java package com.te
使用close()和quit()都会给出1的错误代码。我们使用下面的方法来处理这些问题 1) 创建一个基类,该基类具有将始终运行的beforeSuite、beforeTest、afterTest和afterSuite方法 2) 每个测试计划都应该扩展这个类来创建驱动程序并关闭驱动程序 BasePage.java
package com.test.test3;
import java.lang.reflect.Method;
import java.util.Date;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
public class BasePage {
public WebDriver driver = null;
private Date start;
/*
* Below method will initialize the driver once test method started
* execution
*/
public void initializeDriver(WebDriver driver) {
this.driver = driver;
}
/*
* Below method will kill driver
*/
public void tearDown() {
if (this.driver != null) {
this.driver.quit();
}
}
@AfterMethod(alwaysRun = true)
public void afterTestMethod(Method method) {
// Clean ups for test level services
tearDown();
}
@AfterSuite(alwaysRun = true)
public void afterTestSuite() {
tearDown();
}
}
Testplan.java
package com.test.test3;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class TestPlan extends BasePage{
@Test(groups = { "test"})
public void test() {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("TestAutomation test");
}
@Test(groups = { "test"})
public void test1() {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("TestAutomation test");
}
@Test(groups = { "test"})
public void test2() {
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("TestAutomation test");
}
}
testNg.xml
<suite name="API TEST CASES">
<test name="api test" parallel="methods">
<groups>
<run>
<include name="test" />
</run>
</groups>
<classes>
<class name="com.test.test3.TestPlan" />
</classes>
</test>
发布您启动浏览器并试图关闭浏览器的示例代码。