我正在尝试使用TestNG的testing.xml对crossbrowsers进行简单的测试。在第一次运行Chrome时,脚本运行正常。但是在firefox测试开始时给出“没有这样的会话”错误。
我简化了代码,使其易于阅读。我希望它有所帮助。
这是我的TestNG testing.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="DefaultSuite" thread-count="1" parallel="tests">
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="loginTest">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="loginTest">
</class>
</classes>
</test>
</suite>
这是我的代码
public class loginTest {
private static WebDriver driver;
@BeforeTest
@Parameters("browser")
public void testSetup(String browser) throws Exception {
//Check if parameter passed from TestNG is 'firefox'
if(browser.equalsIgnoreCase("firefox")){
driver = webBrowsers.browserAl("Firefox");
}
//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
driver = webBrowsers.browserAl("Chrome");
}
else{
//If no browser passed throw exception
throw new Exception("Browser is not correct");
}
}
@Test(priority=0)
public void LoginTest(){
driver.get("http://www.hurriyet.com.tr/");
}
@AfterTest
public void kapat() {
webBrowsers.closeDriver();
}
}
最后是我的webBrowsers.class
public class webBrowsers {
private static WebDriver driver = null;
private static String browserName;
public static WebDriver browserAl(String browserName) {
if (browserName.equals("Firefox")) {
if (driver == null) {
System.setProperty("webdriver.gecko.driver","./drivers/geckodriver.exe");
driver = new FirefoxDriver();
}
} else if (browserName.equals("Edge")) {
if (driver == null) {
System.setProperty("webdriver.edge.driver", "./drivers/MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
} else if (browserName.equals("Chrome")) {
if (driver == null) {
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
driver = new ChromeDriver();
}
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
return driver;
}
public static void closeDriver() {
driver.close();
}
}
问题出在你的班级webBrowsers
。您正在使用静态webdriver实例。因此,对于第一个<test>
执行,driver
实例将不为null,它将被正确实例化。但是,当您执行第二个<test>
标记时,条件driver == null
将为false,并且您最终会获得对为先前的<test>
标记创建的webdriver对象的引用。更糟糕的是,你有一个@AfterTest
方法,它也可以清理驱动程序实例。因此,您现在基本上使用有效的webdriver
实例,但已打开的会话已被清理。
最终结果:您的错误。
这是一个清理过的代码版本,应该可以解决这个问题。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class WebBrowsers {
public static WebDriver browserAl(String browserName) {
WebDriver driver = null;
if (browserName.equals("Firefox")) {
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");
driver = new FirefoxDriver();
} else if (browserName.equals("Edge")) {
System.setProperty("webdriver.edge.driver", "./drivers/MicrosoftWebDriver.exe");
driver = new EdgeDriver();
} else if (browserName.equals("Chrome")) {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
return driver;
}
public static void closeDriver(WebDriver driver) {
if (driver != null) {
driver.quit();
}
}
}
以下是您的测试类的外观
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class LoginTest {
private WebDriver driver;
@BeforeTest
@Parameters("browser")
public void testSetup(String browser) throws Exception {
//Check if parameter passed from TestNG is 'firefox'
if (browser.equalsIgnoreCase("firefox")) {
driver = WebBrowsers.browserAl("Firefox");
}
//Check if parameter passed as 'chrome'
else if (browser.equalsIgnoreCase("chrome")) {
driver = WebBrowsers.browserAl("Chrome");
} else {
//If no browser passed throw exception
throw new Exception("Browser is not correct");
}
}
@Test
public void loginTest() {
driver.get("http://www.hurriyet.com.tr/");
}
@AfterTest
public void kapat() {
WebBrowsers.closeDriver(driver);
}
}