org.openqa.selenium.NoSuchSessionException:会话ID为null.调用quit()后使用WebDriver吗? [英] org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?

查看:1593
本文介绍了org.openqa.selenium.NoSuchSessionException:会话ID为null.调用quit()后使用WebDriver吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进行了一些搜索,但是仍然遇到相同的问题.我相信这可能是因为我的网络驱动程序是静态的吗?我不太确定...

I've done a few searches already but I'm still encountering this same issue. I believe it might be caused by my webdriver being static? I'm not too sure...

在我的主要课堂上,我包括了@BeforeTest@AfterTest. @BeforeTest包括根据我的XML文件启动新的浏览器 @AfterTest包括driver.quit(),它将杀死会话/驱动程序,以便第二个测试可以从@BeforeTest使用干净的驱动程序,不是吗?

In my main class, I have included @BeforeTest and @AfterTest. @BeforeTest includes launching a new browser depending on my XML file @AfterTest includes driver.quit(), which should kill the session/driver so the second test can have a clean driver from @BeforeTest, no?

这是我的浏览器声明:

public class Browser {  
public static WebDriver driver;
//Variable initialization
public static String title;
public static String url;
public static String currentBrowser;
public static boolean jseWorkAround;

public Browser(){
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

//Browser launch
public static void launch(String browser){
    if(browser.equalsIgnoreCase("firefox")){
        driver = new FirefoxDriver();
        currentBrowser = "Firefox/";
        jseWorkAround = false;
        System.out.println("Firefox Selected");
    } else if (browser.equalsIgnoreCase("chrome")){
        driver = new ChromeDriver();
        currentBrowser = "Chrome/";
        jseWorkAround = false;
        System.out.println("Chrome Selected");
    } else if (browser.equalsIgnoreCase("edge")){
        driver = new EdgeDriver();
        currentBrowser = "Edge/";
        jseWorkAround = true;
        System.out.println("Edge selected");
    } else if (browser.equalsIgnoreCase("ie")){
        driver = new InternetExplorerDriver();
        currentBrowser = "IE/";
        jseWorkAround = true;
        System.out.println("IE Driver Selected");
    } else if (browser.equalsIgnoreCase("background")){
        driver = new PhantomJSDriver();
        currentBrowser = "Background/";
        jseWorkAround = false;
        System.out.println("Background selected");
    } else {
        throw new IllegalArgumentException("Invalid Browser");
    }
}

public static void quit(){
    driver.quit();
}

public static void goToPage(String pageurl){
    driver.get(pageurl);
}

这是一个随机样本测试:

Here's a random sample test:

    @Parameters({"browser"})
    @BeforeTest
    public void browserSelection(String browser){
        Browser.launch(browser);
    }

    @AfterTest
    public void cleanupAfterTest(){
        Print.line("Test complete. Cleaning up...");
        Browser.quit();
    }

    @Test
    public void Test1() {
        Browser.goToPage("http://www.google.com");
        Screenshot.page("Goes to google");

    }
    @Test
    public void Test2() {
        Browser.goToPage("http://www.yahoo.com");
        Screenshot.page("Goes to yahoo");

    }

我注意到,当事情开始失败并出现错误调用quit之后调用webdriver"时,它总是第二次测试的结尾. 浏览器之间的测试根据我的testng.xml文件中提到的顺序顺序进行. 如果我先运行Firefox,然后运行chrome,则chrome测试将在最后一个@Test上失败. 而如果我先运行chrome,然后运行Firefox,则Firefox在最后一个@Test上将失败.

I noticed that it is always the end of second test when things start failing with the error "Calling webdriver after calling quit". Tests between browsers happen sequentially based on the order mentioned on my testng.xml file. If I run firefox first then chrome, chrome test would fail on the last @Test. Whereas, if I run chrome first then firefox, firefox would fail on the last @Test.

下面是错误消息...

Below is the error message...

org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'DESKTOP-5ED0H7O', ip: '10.0.9.239', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:130)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
    at utility.Scroll.toText(Scroll.java:43)
    at tests.TestCases.TCID20(TestCases.java:390)
    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:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

这种随机失败严重损害了我的进步.如果我一次只在我的XML文件上启用一个浏览器,而没有其他浏览器排在一行,那么一切都不会失败.

This random failing is severely harming my progress. If I enable only one browser at a time on my XML file with no other browser in line, nothing fails.

推荐答案

正如我所看到的,您的问题是在加载浏览器之前@test运行(您必须增加等待时间).另一件事是您尚未添加任何优先级.毕竟,这是您获取浏览器的方式.您必须使用geckodriver,chromedriver等.

As I see your problem is @test run before load browser (you have to increase wait time). Another thing is you haven't added any priority levels. Before all, the way you get browser. you have to use geckodriver, chromedriver etc.

尝试获得如下所示的浏览器

Try to get browser like below

if (browser.equalsIgnoreCase("chrome")){
       System.setProperty("webdriver.chrome.driver", "/Path/ToChromeDriver/chromedriver.exe");
        driver = new ChromeDriver();
        System.out.println("Chrome Selected");
}

添加如下所示的优先级

Browser browser;

@Parameters({"browser"})
@BeforeTest
public void browserSelection(String browser){
    browser.launch(browser);
    thread.sleep(5000);//Not recommended but timeout also may be an issue if so increase implicitly wait time 
}

@AfterTest
public void cleanupAfterTest(){
    Print.line("Test complete. Cleaning up...");
    browser.quit();
}

@Test( priority = 1, description = "Test 1")
public void setUserName(){
    Browser.goToPage("http://www.google.com");
    Screenshot.page("Goes to google");
}

@Test(priority = 2,description = "Test 2")
public void setPassword(){
    Browser.goToPage("http://www.yahoo.com");
    Screenshot.page("Goes to yahoo");
}

这篇关于org.openqa.selenium.NoSuchSessionException:会话ID为null.调用quit()后使用WebDriver吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆