java - 配置失败 : @BeforeMethod setup org. openqa.selenium.NoSuchSessionException : Session ID is null. 调用 quit() 后使用 WebDriver?

标签 java variables selenium-chromedriver testng-eclipse

我在这里发现了一些类似的问题( SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)

org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? ) 但他们都没有帮助我解决我的问题。 任何帮助或建议将不胜感激。

就我而言,我使用的是 TestNG。 我已经创建了主类

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();
}
}

我有一个测试类,它扩展了 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 引用的代码

关于java - 配置失败 : @BeforeMethod setup org. openqa.selenium.NoSuchSessionException : Session ID is null. 调用 quit() 后使用 WebDriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48655414/

相关文章:

php - 如何在插入查询之前获取变量中的insert_id

python - 为什么我的 Selenium 不停止加载?

java - Selenium Chromedriver : Unable to find an exact match for CDP version 98, 所以返回找到的最接近的版本:97

java - 在 driver=new ChromeDriver(); 行上获取 "InvocationTargetException"异常;

java - openid 与 appengine

java - 有没有办法在 Maven 项目中仅使用枚举或接口(interface)创建 jar 文件?

java - 为什么java final变量显示无法访问的语句

variables - 批处理脚本未设置变量

MySQL 中的 Java BCryptPasswordEncoder 等价物

java - 点对点套接字