[英]How to Close Browser after a Test Fails during Parallel Testing
Trying to close the browser when a test fails in Selenium. 当Selenium中的测试失败时,尝试关闭浏览器。 Tried to include an AfterMethod but not browser is still not closing on fail. 试图包括AfterMethod,但并非如此,浏览器在失败时仍无法关闭。 Any help would be greatly appreciated! 任何帮助将不胜感激!
Here is my java class: 这是我的java类:
public class googleTestClass extends Methods{
public void executeGoogle() throws InterruptedException {
this.goToURL("https://www.google.com");
this.enterValue("name","q","google test 1");
}
@Test
public void test1() throws InterruptedException {
googleTestClass object1;
object1 = new googleTestClass();
object1.launchBrowser();
object1.executeGoogle();
}
@Test
public void test2() throws InterruptedException {
googleTestClass object2;
object2 = new googleTestClass();
object2.launchBrowser();
object2.executeGoogle();
}
@AfterMethod
public void tearDown() throws InterruptedException, IOException {
driver.quit();
}
}
Here is the mentioned Methods class: // import statements 这是上面提到的Methods类:// import语句
public class Methods {
public WebDriver driver;
public void launchBrowser() {System.setProperty("webdriver.chrome.driver","C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
public void goToURL(String url) {
driver.get(url);
}
public void enterValue(String htmltype, String identifier, String value) throws InterruptedException {
if (htmltype == "id") {
WebElement element = driver.findElement(By.id(identifier));
element.clear();
element.sendKeys(value);
element.submit();
}
if (htmltype =="name") {
WebElement element = driver.findElement(By.name(identifier));
element.clear();
element.sendKeys(value);
element.submit();
}
Thread.sleep(3000);
}
Here is the used testNG file: 这是使用的testNG文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="5" name="Test" parallel="methods">
<classes>
<class name="webDrivertests.googleTestClass">
<methods>
<include name ="test1"/>
<include name ="test2"/>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Is the AfterMethod
not being reached because it is written incorrectly or does the problem lies in driver.quit?
是否由于编写不正确而无法到达AfterMethod
还是问题出在driver.quit?
Any help would be great. 任何帮助都会很棒。 Thank you! 谢谢!
The problem is that when you run the Test class and @AfterMethod
is called, it is trying to quit from the driver that belongs to the top class but not to the classes which are instantiated inside the methods. 问题在于,当您运行Test类并@AfterMethod
,它将尝试@AfterMethod
顶级类但不属于方法内部实例化的类的驱动程序中退出。 Here is a small example for better understanding : 这是一个更好理解的小例子:
JUnit test class : JUnit测试类:
import org.junit.Test;
import org.junit.After;
public class intTestClass extends Methods{
@Test
public void test1() throws InterruptedException {
intTestClass object1;
object1 = new intTestClass();
object1.setInt();
}
@After
public void tearDown() {
System.out.println("testInt is: "+ testInt);
}
}
And the Methods class : 和Methods类:
public class Methods {
public int testInt = 0;
public void setInt() {
testInt = 1;
}
}
Here, when you run the JUnit test, what you expect is that the testInt
value to be printed as 1. However, the method with the annotation @After(@AfterMethod for testng)
checks the value of the testInt
variable which belongs to the top level intTestClass rather than the intTestClass that is instantiated inside the test1()
method. 在这里,当您运行JUnit测试时,您期望的是将testInt
值打印为1。但是,带有注解@After(@AfterMethod for testng)
检查属于顶部的testInt
变量的值。 级别 intTestClass,而不是在test1()
方法内部实例化的intTestClass 。
Therefore, we see the output as : 因此,我们看到的输出为:
testInt is: 0
If we apply this to your scenario, what you need to do is that move your launchBrowser()
method inside your test class and call the method with an @BeforeClass
annotation. 如果我们将此方法应用于您的方案,则需要做的是在测试类内部移动launchBrowser()
方法,并使用@BeforeClass
批注调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.