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

596 Views Asked by At

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

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.

3

There are 3 best solutions below

0
AptalKiz On BEST ANSWER

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.

2
Pratik On

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>
0
AptalKiz On

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:,".