英文:
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'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("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);
}
}
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<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>
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: 'x', ip: 'x'
Build info: version: '4.8.3', revision: 'e5e76298c3'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
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.<init>(RemoteWebDriver.java:157)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(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.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论