SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.24k stars 8.15k forks source link

[🐛 Bug]: Getting error Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.http.HttpHeader with selenium v4.17.0 only #13568

Closed vikaskrsharma closed 7 months ago

vikaskrsharma commented 7 months ago

What happened?

I have a selenium test which is working fine till version v4.16.1 and is working fine with latest chrome version. but as soon as i update the selenium version to 4.17, Chrome is not getting launched and getting error SessionNotCreatedException with cause: Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/remote/http/HttpHeader. I also have to use chrome v4.17 as devtools protocol api support for chrome v121 is only available in v4.17.

Here is my browser initialization code:

   public Properties prop;
    public ChromeDriver driver;
    public HomePage homePage;
    public SoftAssert softAssert;
    public String browserName;
    public JavascriptExecutor js;
    public DevTools devTools;

    public void initializeDriver()  {
        softAssert = new SoftAssert();
        prop = new Properties();
        FileInputStream fis;
        try {
            fis= new FileInputStream(System.getProperty("user.dir")+"/src/main/java/config/config.properties");
            prop.load(fis);
        } catch (Exception e) {
            e.printStackTrace();
        }

        browserName = System.getProperty("browser") !=null ? System.getProperty("browser") : prop.getProperty("browser");
        String userid = System.getProperty("userid");
        System.out.println("userid from jenkins: "+ userid);

        // to resolve chrome issue (blocking incoming websocket connection) in latest version
        System.setProperty("webdriver.http.factory", "jdk-http-client");

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

//            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+ "/chromedriver");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
            devTools = driver.getDevTools();

        } else if(browserName.equalsIgnoreCase("chromeHeadless")) {
            WebDriverManager.chromedriver().setup();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200",
                    "--ignore-certificate-errors","--disable-extensions","--no-sandbox","--disable-dev-shm-usage");
            driver = new ChromeDriver(options);
            System.out.println("Chrome driver in headless mode started");

        }

        else if(browserName.equalsIgnoreCase("firefox")) {
            // Firefox driver
            WebDriverManager.firefoxdriver().setup();
//            driver = new FirefoxDriver();
        }
        else if(browserName.equalsIgnoreCase("edge")) {
            // Edge driver
        }
        else if(browserName.equalsIgnoreCase("safari")){
            WebDriverManager.safaridriver().setup();
//            driver = new SafariDriver();
        }
        js = (JavascriptExecutor) driver;
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
        driver.manage().window().maximize();
    }

    @BeforeMethod
    public void launchApp() throws IOException {
        initializeDriver();
        System.out.println("Before get: "+ prop.getProperty("url"));
        driver.get(prop.getProperty("url"));
        System.out.println("After get: "+prop.getProperty("url"));
        homePage = new HomePage(driver);
    }

    @AfterMethod
    public void teardown() {
        driver.close();
    }

How can we reproduce the issue?

I have following chrome initialization code. Use selenium version 4.17 specifically with chrome 121.

Relevant log output

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: 'MacBook-Pro.local', ip: '2401:4900:1c68:d0a0:2d16:2e81:13a6:568e%en0'
Build info: version: '4.17.0', revision: 'e52b1be057*'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '14.2.1', java.version: '17.0.6'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], extensions: []}}]}]

    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:537)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:233)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:162)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:114)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:89)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:84)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:52)
    at testcomponents.BaseTest.initializeDriver(BaseTest.java:74)
    at testcomponents.BaseTest.launchApp(BaseTest.java:107)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69)
    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:393)
    at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:326)
    at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:810)
    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:578)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:228)
    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:63)
    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:961)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:201)
    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:819)
    at org.testng.TestRunner.run(TestRunner.java:619)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1228)
    at org.testng.TestNG.runSuites(TestNG.java:1134)
    at org.testng.TestNG.run(TestNG.java:1101)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/remote/http/HttpHeader
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:110)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:95)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:162)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:519)
    ... 39 more
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.http.HttpHeader
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 46 more

Operating System

macOs Sonoma

Selenium version

Java 4.17.0

What are the browser(s) and version(s) where you see this issue?

Chrome 121

What are the browser driver(s) and version(s) where you see this issue?

Latest ChromeDriver

Are you using Selenium Grid?

No response

github-actions[bot] commented 7 months ago

@vikaskrsharma, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

krmahadevan commented 7 months ago

@vikaskrsharma - Can you please share your build file (pom.xml (or) build.gradle|build.gradle.kts) so that we know what your dependencies look like

vikaskrsharma commented 7 months ago

@krmahadevan Here is the dependencies snippet in pom file:

  <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>BuyerWeb</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.17.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-http-jdk-client</artifactId>
            <version>4.13.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.6.3</version>
        </dependency>

        <dependency>
            <groupId>com.github.mwiede</groupId>
            <artifactId>jsch</artifactId>
            <version>0.2.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.9.0</version>
<!--            <scope>test</scope>-->
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>5.1.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>

    </dependencies>
vikaskrsharma commented 7 months ago

@vikaskrsharma - Can you please share your build file (pom.xml (or) build.gradle|build.gradle.kts) so that we know what your dependencies look like

I have shared the pom file dependencies. Also fyi, if i just update selenium java version to 4.16.1, it working fine but 4.16.1 does not support devtools cdp for chrome 121 so updating it to 4.17.0 which is giving above issue

krmahadevan commented 7 months ago

@vikaskrsharma - Can you please retry after removing the below dependencies ?

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-http-jdk-client</artifactId>
            <version>4.13.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.6.3</version>
        </dependency>

By default selenium would use the jdk provided httpclient and so you don't need to bring it in explicitly. Selenium also has the capabilities to manage the driver binaries and so you don't need webdrivermanager. You can read more about this aspect in the official documentation here

I retried using the below example

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.time.Duration;

public class AppTest {

    private ChromeDriver driver;
    public void initializeDriver() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        driver = new ChromeDriver();

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
        driver.manage().window().maximize();
    }

    @BeforeMethod
    public void launchApp() {
        initializeDriver();
    }

    @Test
    public void testMethod() {
        String url = "https://selenium.dev";
        System.out.println("Before get: " + url);
        driver.get(url);
        System.out.println("After get: " + url);
    }

    @AfterMethod
    public void teardown() {
        driver.close();
    }
}
vikaskrsharma commented 7 months ago

@krmahadevan Thank you very much for the much needed help. It is working fine. Also for future Reference, if there is already a chromedriver binary present in /usr/local/bin folder for older chrome version, then have to delete it manually as selenium will be using that binary which will be not be compatible with latest version.

github-actions[bot] commented 6 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.