无法运行Selenium测试,“无法启动新会话”。

huangapple go评论67阅读模式
英文:

Can't run a Selenium test "could not start a new session"

问题

Here's the translation of the content you provided:

我的朋友给我发送了他的Selenium Grid测试代码,它在他的计算机上运行正常,但在我的计算机上不工作。以下是代码文件和pom.xml的部分内容。

Chrome:113.0.5672.93

Chrome驱动程序:113.0.5672.63

jdk:1.8

Selenium Grid:4.8.3

# 基础类

```java
package Base;

import Pages.Login.LoginPage;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

public class Base {

    protected WebDriver driver;
    LoginPage objLogin;

    public WebDriver initializeBrowser(String browserName) throws MalformedURLException {
        DesiredCapabilities dc = new DesiredCapabilities();

        if(browserName.equals("chrome")) {

            dc.setBrowserName("chrome");

        }else if(browserName.equals("firefox")) {

            dc.setBrowserName("firefox");

        }else if(browserName.equals("edge")) {

            dc.setBrowserName("MicrosoftEdge");

        }else if(browserName.equals("ie")) {

            dc.setBrowserName("Internet explorer");

        }else if(browserName.equals("opera")) {

            dc.setBrowserName("opera");

        }else if(browserName.equals("safari")) {

            dc.setBrowserName("safari");
        }
        driver = new RemoteWebDriver(new URL("http://localhost:4444"), dc);

        return driver;
    }

    private Map<String, Object> vars;

    public void setUp(String strBrowser, String strEnv ) throws MalformedURLException {
        driver = initializeBrowser(strBrowser);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        vars = new HashMap<>();

        driver.get(strEnv);

        driver.manage().window().maximize();
    }

    public void loginUser(String strUserName, String strPassword) throws InterruptedException {
        ResourceBundle bundleLogin = ResourceBundle.getBundle("PageConfigFile.Login.Login");

        objLogin = new Pages.Login.LoginPage(driver);

        objLogin.loginToUser(strUserName, strPassword);

        TimeUnit.SECONDS.sleep(5);
    }
}

测试类

import Tests.ProfilsTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.util.Map;
import java.util.ResourceBundle;

public class Test extends Base {

    ProfilsTest objProfilTest;

    private Map<String, Object> vars;

    @BeforeTest
    public  void start() throws MalformedURLException, InterruptedException {
        ResourceBundle bundleRec = ResourceBundle.getBundle("PageConfigFile.Env.Env");
        ResourceBundle bundleBrowser = ResourceBundle.getBundle("PageConfigFile.Browser.Browser");
        ResourceBundle bundleLogin = ResourceBundle.getBundle("PageConfigFile.Login.Login");


        String browser = "chrome";
        String env = bundleRec.getString("url");

        String username = bundleLogin.getString("test");
        String pwd = bundleLogin.getString("password");

        setUp(browser,env);
        loginUser(username,pwd);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>sgtp</groupId>
  <artifactId>Tests</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>Tests</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.apache</groupId>
    <artifactId>apache</artifactId>
    <version>8</version>
    <relativePath></relativePath>
  </parent>

  <dependencies>
   <!-- https://mvnrepository.com/artifact/org.testng/testng -->


 <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.8.3</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>com.nordstrom.tools</groupId>
      <artifactId>TestNG-Foundation</artifactId>
      <version>3.0.0-j8</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-firefox-driver</artifactId>
      <version>4.8.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>backport-util-concurrent</groupId>
      <artifactId>backport-util-concurrent</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>org.openqa.selenium</groupId>
      <artifactId>selenium-rc</artifactId>
      <version>1.0-20081010.060147</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

</project>

这是我在启动Test.java时收到的错误消息:

org.openqa.selenium.SessionNotCreatedException: 无法启动新会话。可能的原因是远程服务器的无效地址或浏览器启动失败。
主机信息:主机:'x',ip:'x'
构建信息:版本:'4.8.3',修订:'e5e76298c3'
系统信息:os.name:'Windows 10',os.arch:'amd64',os.version:'10.0',java.version:'1.8.0_171'
驱动程序信息:org.openqa.selenium.remote.RemoteWebDriver

<details>
<summary>英文:</summary>

My friend sent me his code for Selenium tests using Selenium Grid, they work on his computer but not on mine. Here&#39;s part of the code file and pom.xml.

Chrome : 113.0.5672.93

Chrome driver : 113.0.5672.63

jdk : 1.8

Selenium Grid : 4.8.3

# Base class

```java
package Base;

import Pages.Login.LoginPage;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

public class Base {

    protected WebDriver driver;
    LoginPage objLogin;

    public WebDriver initializeBrowser(String browserName) throws MalformedURLException {
        DesiredCapabilities dc = new DesiredCapabilities();

      if(browserName.equals(&quot;chrome&quot;)) {

            dc.setBrowserName(&quot;chrome&quot;);

        }else if(browserName.equals(&quot;firefox&quot;)) {

            dc.setBrowserName(&quot;firefox&quot;);

        }else if(browserName.equals(&quot;edge&quot;)) {

            dc.setBrowserName(&quot;MicrosoftEdge&quot;);

        }else if(browserName.equals(&quot;ie&quot;)) {

            dc.setBrowserName(&quot;Internet explorer&quot;);

        }else if(browserName.equals(&quot;opera&quot;)) {

            dc.setBrowserName(&quot;opera&quot;);

        }else if(browserName.equals(&quot;safari&quot;)) {

            dc.setBrowserName(&quot;safari&quot;);
        }
        driver = new RemoteWebDriver(new URL(&quot;http://localhost:4444&quot;), dc);

        return driver;
    }

    private Map&lt;String, Object&gt; vars;

    public void setUp(String strBrowser, String strEnv ) throws MalformedURLException {
        driver = initializeBrowser(strBrowser);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        vars = new HashMap&lt;&gt;();

        driver.get(strEnv);

        driver.manage().window().maximize();
    }

    public void loginUser(String strUserName, String strPassword) throws InterruptedException {
        ResourceBundle bundleLogin = ResourceBundle.getBundle(&quot;PageConfigFile.Login.Login&quot;);

        objLogin = new Pages.Login.LoginPage(driver);

        objLogin.loginToUser(strUserName, strPassword);

        TimeUnit.SECONDS.sleep(5);
    }
}

Test class

import Tests.ProfilsTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.util.Map;
import java.util.ResourceBundle;

public class Test extends Base {

    ProfilsTest objProfilTest;

    private Map&lt;String, Object&gt; vars;

    @BeforeTest
    public  void start() throws MalformedURLException, InterruptedException {
        ResourceBundle bundleRec = ResourceBundle.getBundle(&quot;PageConfigFile.Env.Env&quot;);
        ResourceBundle bundleBrowser = ResourceBundle.getBundle(&quot;PageConfigFile.Browser.Browser&quot;);
        ResourceBundle bundleLogin = ResourceBundle.getBundle(&quot;PageConfigFile.Login.Login&quot;);


        String browser = &quot;chrome&quot;;
        String env = bundleRec.getString(&quot;url&quot;);

        String username = bundleLogin.getString(&quot;test&quot;);
        String pwd = bundleLogin.getString(&quot;password&quot;);

        setUp(browser,env);
        loginUser(username,pwd);
    }
}

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;sgtp&lt;/groupId&gt;
&lt;artifactId&gt;Tests&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;Tests&lt;/name&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;maven.compiler.source&gt;1.7&lt;/maven.compiler.source&gt;
&lt;maven.compiler.target&gt;1.7&lt;/maven.compiler.target&gt;
&lt;/properties&gt;
&lt;parent&gt;
&lt;groupId&gt;org.apache&lt;/groupId&gt;
&lt;artifactId&gt;apache&lt;/artifactId&gt;
&lt;version&gt;8&lt;/version&gt;
&lt;relativePath&gt;&lt;/relativePath&gt;
&lt;/parent&gt;
&lt;dependencies&gt;
&lt;!-- https://mvnrepository.com/artifact/org.testng/testng --&gt;
&lt;!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
&lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
&lt;version&gt;4.8.3&lt;/version&gt;
&lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.nordstrom.tools&lt;/groupId&gt;
&lt;artifactId&gt;TestNG-Foundation&lt;/artifactId&gt;
&lt;version&gt;3.0.0-j8&lt;/version&gt;
&lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.testng&lt;/groupId&gt;
&lt;artifactId&gt;testng&lt;/artifactId&gt;
&lt;version&gt;7.5&lt;/version&gt;
&lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
&lt;artifactId&gt;selenium-firefox-driver&lt;/artifactId&gt;
&lt;version&gt;4.8.3&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;backport-util-concurrent&lt;/groupId&gt;
&lt;artifactId&gt;backport-util-concurrent&lt;/artifactId&gt;
&lt;version&gt;3.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.openqa.selenium&lt;/groupId&gt;
&lt;artifactId&gt;selenium-rc&lt;/artifactId&gt;
&lt;version&gt;1.0-20081010.060147&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;version&gt;4.12&lt;/version&gt;
&lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;version&gt;4.12&lt;/version&gt;
&lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;

Here's the error message I get when launching Test.java :

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Host info: host: &#39;x&#39;, ip: &#39;x&#39;
Build info: version: &#39;4.8.3&#39;, revision: &#39;e5e76298c3&#39;
System info: os.name: &#39;Windows 10&#39;, os.arch: &#39;amd64&#39;, os.version: &#39;10.0&#39;, java.version: &#39;1.8.0_171&#39;
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: \[null, newSession {capabilities=\[Capabilities {browserName: chrome}\], desiredCapabilities=Capabilities {browserName: chrome}}\]
Capabilities {browserName: chrome}
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
at org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:157)
at org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:139)
at Base.Base.initializeBrowser(Base.java:50)
at Base.Base.setUp(Base.java:59)
at Tests.Test.start(Test.java:35)
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.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:65)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:381)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:319)
at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:645)
at org.testng.TestRunner.beforeRun(TestRunner.java:634)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:429)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:423)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:383)
at org.testng.SuiteRunner.run(SuiteRunner.java:326)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1249)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.runSuites(TestNG.java:1092)
at org.testng.TestNG.run(TestNG.java:1060)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.of(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Lcom/google/common/collect/ImmutableMap;
at org.openqa.selenium.chrome.AddHasCasting.getAdditionalCommands(AddHasCasting.java:38)
at org.openqa.selenium.remote.Dialect.lambda$bindAdditionalCommands$1(Dialect.java:80)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.openqa.selenium.remote.Dialect.bindAdditionalCommands(Dialect.java:79)
at org.openqa.selenium.remote.Dialect.access$100(Dialect.java:29)
at org.openqa.selenium.remote.Dialect$2.getCommandCodec(Dialect.java:54)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:167)
at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
... 29 more

Before that, when I launched the test, I had a blank GoogleChrome page opening with only an url like "data/".
I have tried to run the tests on the firefox browser to make it work but nothing appeared.
I have also tried to only launch some random test without using the remote web driver and it works so I am guessing the problem comes from using the remote web driver but I can't tell what is actually wrong.

答案1

得分: 0

尝试使用较低版本的依赖项,并确保在工作完成或出现异常时关闭驱动程序。

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.1.1</version>
</dependency>
英文:

try lower version of dependency and make sure to close driver, whenever your works completed or in any exception.

&lt;dependency&gt;
&lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
&lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
&lt;version&gt;4.1.1&lt;/version&gt;
&lt;/dependency&gt;

答案2

得分: 0

问题的一部分来自一个依赖项,它并没有仅通过mvn clean install进行安装,因为存在某些缓存问题,为了避免这个问题,我不得不使用mvn clean install -U强制安装。
现在当我启动我的测试时,仍然出现相同的错误“无法启动新会话”,但我还有一个空白的Google Chrome页面打开,网址是“data:,”。

英文:

Part of the problem came from a dependency that wasn't getting installed only with mvn clean install because of some cache issue, to avoid that I had to force the installation by using mvn clean install -U.
Now when i launch my test, I still get the same error "could not start a new session." but I also have a blank google Chrome page opening with the url "data:,".

答案3

得分: 0

我试过几周后重新启动程序,它成功了。甚至没有改变任何代码,但无论如何,对我来说很好。

英文:

So i've tried to relaunch the program a couple of weeks later and it worked. Didn't even change anything in the code but anyways, good for me.

huangapple
  • 本文由 发表于 2023年5月11日 17:52:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定