根据browserclose()方法,应在操作结束时关闭Internet Explorer。但它还没有结束。
以下是代码:
多重测试:
<suite name="TestingSuite">
<test name="Tester">
<classes>
<class name="framework2.NewTest"/>
<class name="framework2.NewTest2"/>
</classes>
</test>
</suite>
NewTest.Java:
package framework2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class NewTest {
public WebDriver driver;
@BeforeTest
public void launchbrowser() {
System.setProperty("webdriver.ie.driver", "C:\\Users\\rprem\\Downloads\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("https://www.google.com");
System.out.println("TestCase 0: Google was launched");
}
@Test
public void OpenMail(){
driver.get("https://www.gmail.com");
System.out.println("Testcase 1: Gmail was launached");
}
@AfterTest
public void browserclose (){
driver.close();
System.out.println("TestCase 2: Browser was closed");
}
}
NewTest2.java:
package framework2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;
public class NewTest2 {
@Test
public void OpenYahooMail(){
WebDriver driver = new InternetExplorerDriver();
System.setProperty("WebDriver.ie.driver", "C:\\Users\\rprem\\Downloads\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe");
driver.get("https://login.yahoo.com/");
System.out.println("Testcase2_1: Yahoo mail was launached");
}
}
日志:
Started InternetExplorerDriver server (64-bit)
3.4.0.0
Listening on port 2133
Only local connections are allowed
Jul 29, 2017 5:16:13 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
TestCase 0: Google was launched
Testcase 1: Gmail was launached
Started InternetExplorerDriver server (64-bit)
3.4.0.0
Listening on port 23709
Only local connections are allowed
Jul 29, 2017 5:16:18 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Testcase2_1: Yahoo mail was launached
TestCase 2: Browser was closed
PASSED: OpenMail
PASSED: OpenYahooMail
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
答案 0 :(得分:0)
问题在于您的测试代码。
您有WebDriver
个实例作为班级NewTest.java
中的数据成员。您正在使用driver.close()
带注释的@AfterTest
方法进行browserclose()
调用。
您通过语句WebDriver
在类NewTest2.java
中实例化了另一个WebDriver driver = new InternetExplorerDriver();
实例,但是通过调用driver.quit()
来清除此驱动程序。
所以你需要在driver.quit()
中调用NewTest2.java
。
您的测试代码还有一些其他问题。
@BeforeTest
和@AfterTest
带注释的方法管理WebDriver实例化和清理是一个坏主意,因为这些方法每<test>
个标记只调用一次。因此,如果您的<class>
代码中有两个或更多<test>
个条目。您应该使用@BeforeClass
和@AfterClass
来管理相同的内容。WebDriver
个实例,您应该调用driver.quit()
而不是driver.close()