Java TestNg多个浏览器的非并行执行;org.openqa.selenium.NoSuchSessionException:无效会话id“;

Java TestNg多个浏览器的非并行执行;org.openqa.selenium.NoSuchSessionException:无效会话id“;,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我正在尝试使用TestNg非并行地在Chrome和Firefox中执行我的代码。在第一个测试中,“Chrome”正常工作,第二个测试“Firefox”浏览器打开,但抛出以下异常“org.openqa.selenium.NoSuchSessionException:会话id无效” org.openqa.selenium.NoSuchSessionException: invalid session id Build info: version: '3.141.59', revision: 'e82

我正在尝试使用TestNg非并行地在Chrome和Firefox中执行我的代码。在第一个测试中,“Chrome”正常工作,第二个测试“Firefox”浏览器打开,但抛出以下异常“org.openqa.selenium.NoSuchSessionException:会话id无效”

org.openqa.selenium.NoSuchSessionException: invalid session id
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'thundurus', ip: '172.17.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-70-generic', java.version: '1.8.0_222'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 78.0.3904.108, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: /tmp/.com.google.Chrome.Ug9Qqh}, goog:chromeOptions: {debuggerAddress: localhost:37143}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 1340b5921cffb4a50d3542b199d22db6
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:618)
    at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:639)
    at modules.actions.moveOver(actions.java:11)
    at pageModels.homePage.hover_menu(homePage.java:22)
    at testCases.testClass.base(testClass.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
这是我在Chrome完成执行后得到的例外。 在上述异常中,通过第二次测试的浏览器参数仍然是“Chrome”

TestNg文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="2">

    <test thread-count="5" name="Chrome">
        <parameter name="browser" value="Chrome"/>
            <!-- <groups>
                <run>
                    <include name="exe.home"/>
                </run>
            </groups> -->
            <classes>
                <class name="modules.openBrowser" />
                <class name="testCases.testClass"/>
            </classes>
        </test> <!-- Test -->

        <test thread-count="5" name="FireFox">
    <parameter name="browser" value="Firefox"/>
        <!-- <groups>
            <run>
                <include name="exe.home"/>
            </run>
        </groups> -->
        <classes>
            <class name="modules.openBrowser" />
            <class name="testCases.testClass"/>
        </classes>
    </test> <!-- Test -->

</suite> <!-- Suite -->
我甚至尝试从WebDriver中删除“static”,但仍然是相同的例外


谢谢。

您的脚本中可能有两个问题

  • 您可能在脚本结束之前关闭或退出浏览器,这可能会导致会话无效。查看您的测试脚本可能会给出清晰的图片

  • 在第二种情况下,您可能使用静态变量来创建页面对象。当您并行运行并且使用静态页面对象时,内存将只分配给第一个创建的页面对象一次,并且当第一个脚本关闭时,会话将无效。 查看测试脚本可能会给出清晰的图片


  • 您的脚本中可能有两个问题

  • 您可能在脚本结束之前关闭或退出浏览器,这可能会导致会话无效。查看您的测试脚本可能会给出清晰的图片

  • 在第二种情况下,您可能使用静态变量来创建页面对象。当您并行运行并且使用静态页面对象时,内存将只分配给第一个创建的页面对象一次,并且当第一个脚本关闭时,会话将无效。 查看测试脚本可能会给出清晰的图片


  • 您已将WebDriver定义为静态

    public static WebDriver driver = null;
    

    删除static并重试。

    您已将WebDriver定义为static

    public static WebDriver driver = null;
    

    删除static并重试。

    我觉得这段代码很好。请确保您使用的是与selenium及其webdriver兼容的Firefox版本。我觉得这段代码不错。请确保您使用的是与selenium兼容的Firefox版本及其Web驱动程序

    将Firefox代码放在chrome之前,并检查chrome是否存在相同的异常。我尝试了这一点,我得到了此异常“org.openqa.selenium.NoSuchSessionException:尝试在不建立连接的情况下运行命令”将firefox代码放在chrome之前,并检查chrome是否出现相同的异常。我尝试了这一点,得到了这个异常“org.openqa.selenium.NoSuchSessionException:尝试在不建立连接的情况下运行命令”