配置失败:@BeforeMethod 设置 org.openqa.selenium.NoSuchSessionException:会话 ID 为空。调用 quit() 后使用 WebDriver?
FAILED CONFIGURATION: @BeforeMethod setup org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
我在这里发现了几个类似的问题(
)
但是 none 并没有帮助我解决我的问题。
任何帮助或建议将不胜感激。
就我而言,我使用的是 TestNG。
我创建了 Main Class
package base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
public class MainClass {
public WebDriver driver;
public Properties CONFIG;
public Properties XPATH;
@BeforeSuite
public void initialization() throws IOException {
String chromeDriverLocation = System.getProperty("user.dir")+"/src/main/java/driverlocation/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
driver = new ChromeDriver();
System.out.println("Web Driver initilization has started...");
System.out.println("In the before suite...");
CONFIG = new Properties();
String configLocation = System.getProperty("user.dir")+"/src/main/java/base/Config.properties";
FileInputStream ip = new FileInputStream(configLocation);
CONFIG.load(ip);
XPATH = new Properties();
String xpathLocation = System.getProperty("user.dir")+"/src/main/java/base/xpath.properties";
FileInputStream xpathIp = new FileInputStream(xpathLocation);
XPATH.load(xpathIp);
}
@AfterSuite
public void tearDown() {
System.out.println("Web Driver is closing...");
System.out.println("it is epic...");
driver.quit();
}
}
我有 class 测试扩展 Main.Class
package Test.TileSearch;
import org.testng.annotations.Test;
import base.MainClass;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
public class Tile1City1 extends MainClass {
@BeforeMethod
public void setup() throws Exception {
driver.get(CONFIG.getProperty("urlcentura"));
driver.manage().window().maximize();
}
@AfterMethod
public void tearDown() {
driver.quit();
}
@Test
public void main1() throws Exception
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
if(!driver.findElements(By.partialLinkText("Tile")).isEmpty()){
System.out.println("Tile is available there");
}else{
System.out.println("Tile is not available there");
}
}
@Test
public void main1() throws Exception
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
if(!driver.findElements(By.partialLinkText("Tile")).isEmpty()){
System.out.println("Tile is available there");
}else{
System.out.println("Tile is not available there");
}
}
}
控制台出现错误
[RemoteTestNG] detected TestNG version 6.13.1
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 11302
Only local connections are allowed.
Feb 06, 2018 8:13:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Web Driver initilization has started...
In the before suite...
Abaco is available there
FAILED CONFIGURATION: @BeforeMethod setup
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'EARL', ip: '192.168.1.85', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at Test.CenturaSearch.AbacoMontreal.setup(AbacoMontreal.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:516)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED CONFIGURATION: @AfterMethod tearDown
PASSED: main
SKIPPED: main1
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'EARL', ip: '192.168.1.85', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at Test.CenturaSearch.AbacoMontreal.setup(AbacoMontreal.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:516)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
只有一个测试是 运行,其他总是跳过。
基本上问题是我正在为 WebDriver 使用静态属性,不应该在不同的测试运行之间共享。
有人知道如何解决这个问题吗?
您似乎要退出 Tile1City1 中的驱动程序
@AfterMethod
public void tearDown() {
driver.quit();
}
所以你在@BeforeSuite 中打开驱动程序,然后在一次测试后退出它并且永远不会重新打开它。我可能误读了代码,但这似乎是正在发生的事情。
如果您希望每个测试都有一个新的驱动程序,您还需要在@BeforeMethod 中打开每个测试的驱动程序。
如果您想为每个测试重复使用相同的驱动程序,请删除我从 Tile1City1 引用的代码
内部驱动程序
`public static void closeDriver() {
if (driver != null) { driver.quit(); driver = null; }
}`
并在@AfterMethod 中使用这个
Driver.closeDriver();
我在这里发现了几个类似的问题(
就我而言,我使用的是 TestNG。 我创建了 Main Class
package base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
public class MainClass {
public WebDriver driver;
public Properties CONFIG;
public Properties XPATH;
@BeforeSuite
public void initialization() throws IOException {
String chromeDriverLocation = System.getProperty("user.dir")+"/src/main/java/driverlocation/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
driver = new ChromeDriver();
System.out.println("Web Driver initilization has started...");
System.out.println("In the before suite...");
CONFIG = new Properties();
String configLocation = System.getProperty("user.dir")+"/src/main/java/base/Config.properties";
FileInputStream ip = new FileInputStream(configLocation);
CONFIG.load(ip);
XPATH = new Properties();
String xpathLocation = System.getProperty("user.dir")+"/src/main/java/base/xpath.properties";
FileInputStream xpathIp = new FileInputStream(xpathLocation);
XPATH.load(xpathIp);
}
@AfterSuite
public void tearDown() {
System.out.println("Web Driver is closing...");
System.out.println("it is epic...");
driver.quit();
}
}
我有 class 测试扩展 Main.Class
package Test.TileSearch;
import org.testng.annotations.Test;
import base.MainClass;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
public class Tile1City1 extends MainClass {
@BeforeMethod
public void setup() throws Exception {
driver.get(CONFIG.getProperty("urlcentura"));
driver.manage().window().maximize();
}
@AfterMethod
public void tearDown() {
driver.quit();
}
@Test
public void main1() throws Exception
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
if(!driver.findElements(By.partialLinkText("Tile")).isEmpty()){
System.out.println("Tile is available there");
}else{
System.out.println("Tile is not available there");
}
}
@Test
public void main1() throws Exception
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
if(!driver.findElements(By.partialLinkText("Tile")).isEmpty()){
System.out.println("Tile is available there");
}else{
System.out.println("Tile is not available there");
}
}
}
控制台出现错误
[RemoteTestNG] detected TestNG version 6.13.1
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 11302
Only local connections are allowed.
Feb 06, 2018 8:13:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Web Driver initilization has started...
In the before suite...
Abaco is available there
FAILED CONFIGURATION: @BeforeMethod setup
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'EARL', ip: '192.168.1.85', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at Test.CenturaSearch.AbacoMontreal.setup(AbacoMontreal.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:516)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED CONFIGURATION: @AfterMethod tearDown
PASSED: main
SKIPPED: main1
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'EARL', ip: '192.168.1.85', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at Test.CenturaSearch.AbacoMontreal.setup(AbacoMontreal.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:516)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
只有一个测试是 运行,其他总是跳过。 基本上问题是我正在为 WebDriver 使用静态属性,不应该在不同的测试运行之间共享。 有人知道如何解决这个问题吗?
您似乎要退出 Tile1City1 中的驱动程序
@AfterMethod
public void tearDown() {
driver.quit();
}
所以你在@BeforeSuite 中打开驱动程序,然后在一次测试后退出它并且永远不会重新打开它。我可能误读了代码,但这似乎是正在发生的事情。
如果您希望每个测试都有一个新的驱动程序,您还需要在@BeforeMethod 中打开每个测试的驱动程序。
如果您想为每个测试重复使用相同的驱动程序,请删除我从 Tile1City1 引用的代码
内部驱动程序
`public static void closeDriver() {
if (driver != null) { driver.quit(); driver = null; }
}`
并在@AfterMethod 中使用这个
Driver.closeDriver();