0

I am facing a weird issue in my project while running it as I am coming across an issue org.openqa.selenium.chrome.ChromeOptions.setExperimentalOption. Actually, I had first stuck with NoClassDefError. To solve that I had put httpclient5 dependency explicitly in pom. Once I had put that, I started getting java.lang.NoSuchMethodError:org.openqa.selenium.chrome.ChromeOptions.setExperimentalOption

Below is my 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>

    <artifactId>corporate-action-tests-ui</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <artifactId>corporate-action-parent</artifactId>
        <groupId>com.mst.indexes</groupId>
        <version>0.5.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <suitefile>SmokeSuite_stg.xml</suitefile>
        <env>stg</env>
        <browser>localChrome</browser>
        <suite>Smoke</suite>
        <test_region>us-west-2</test_region>
        <updateBugInJira>false</updateBugInJira>
        <updateTestRail>true</updateTestRail>
        <updateReportPortal>false</updateReportPortal>
        <quitBrowserAfterExecution>true</quitBrowserAfterExecution>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.mst.indexes</groupId>
            <artifactId>ui-automation-base</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>tech.grasshopper</groupId>
            <artifactId>extentreports-cucumber6-adapter</artifactId>
            <version>2.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mst.indexes</groupId>
            <artifactId>corporate-action-dto</artifactId>
            <version>0.5.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/cisuite/${suitefile}</suiteXmlFile>
                    </suiteXmlFiles>
                    <systemPropertyVariables>
                        <env>${env}</env>
                        <browser>${browser}</browser>
                        <suitefile>${suitefile}</suitefile>
                        <suite>${suite}</suite>
                        <test_region>${test_region}</test_region>
                        <updateBugInJira>${updateBugInJira}</updateBugInJira>
                        <updateTestRail>${updateTestRail}</updateTestRail>
                        <rp.enable>${updateReportPortal}</rp.enable>
                        <quitBrowserAfterExecution>${quitBrowserAfterExecution}</quitBrowserAfterExecution>
                        <webdriver.chrome.driver.cachePath>C:/Users/ppathar/.cache/selenium/chromedriver/win64/126.0.6478.126
                        </webdriver.chrome.driver.cachePath>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

We use a common framework ui automation base and there we have selenium and webdrivermanager dependencies defined

<dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.6.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

I tried hardcoding webdriver.chrome.driver.cachePath as shown to let webdriver use the chromedriver from my cache path but that too is not working. Even tried to hardcode browserversion with setbrowserversion("") method but then too I am getting the same error. Also tried excludting httpclient5 dependency from each of the dependencies.Even I updated the selenium version and webdrivermanager version. Still no success. Then too getting the same error. Can I have some solution for this? Nothing seems to work for us.

Below is the error stack trace

2024-07-10 07:50:22,622 level="INFO" class="i.g.bonigarcia.wdm.WebDriverManager" env="stg" description="Using chromedriver 126.0.6478.126 (resolved driver for Chrome 126)" 2024-07-10 07:50:22,667 level="INFO" class="i.g.bonigarcia.wdm.WebDriverManager" env="stg" description="Exporting webdriver.chrome.driver as C:\Users\ppathar.cache\selenium\chromedriver\win64\126.0.6478.126\chromedriver.exe"

java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.setExperimentalOption(Ljava/lang/String;Ljava/lang/Object;)Lorg/openqa/selenium/chrome/ChromeOptions;

at com.morningstar.indexes.ui.test.Base.initializeBrowserSetup(Base.java:87)
at com.morningstar.indexes.ui.test.Base.setUp(Base.java:173)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:366)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:320)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:701)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:527)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:65)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)

Below is the code snippet used for initializing webdriver

case "localChrome":
            WebDriverManager.chromedriver().setup();
            ChromeOptions cp = new ChromeOptions();
            Map<String, Object> prefs = new HashMap<String, Object>();
            prefs.put("download.default_directory", downloadsPath);
            cp.setExperimentalOption("prefs", prefs);
            if (headlessModeOn.equalsIgnoreCase("true")) {
                cp.addArguments("--headless");
            }
            driver.set(new ChromeDriver(cp));

            break;
7
  • Can you update the question and add the stacktrace of the exception that you're getting along with a snippet of the code that you've written to initialize the webdriver? Commented Jul 10 at 7:49
  • Done @Techrookie89 Commented Jul 10 at 7:53
  • Can you check the output of mvn dependency:tree command and see if there are any other selenium dependency along with 3.141.59 ? Going by the looks of it, there could be a conflicting dependency of the selenium webdriver Commented Jul 10 at 7:57
  • I checked the mvn dependency tree verbose and there I saw this | +- org.seleniumhq.selenium:selenium-java:jar:4.1.4:compile (version managed from 3.141.59) [INFO] | | +- org.seleniumhq.selenium:selenium-api:jar:4.1.4:compile [INFO] | | +- org.seleniumhq.selenium:selenium-chrome-driver:jar:4.1.4:compile [INFO] | | | +- com.google.auto.service:auto-service-annotations:jar:1.0.1:compile [INFO] | | | +- com.google.auto.service:auto-service:jar:1.0.1:compile Commented Jul 10 at 8:05
  • Great, The parent of this selenium 4.1.4 dependency is the problem that you'll need to resolve. Basically, there are two dependencies in the class path one with 4.1.4 and another one that is 3.141.59 which are conflicting with each other. Commented Jul 10 at 8:18

1 Answer 1

0

In cases when you receive NoSuchMethodException or NoClassDefFound exception and its confirmed that the dependencies of the right version are present in your pom.xml. This is generally in case where there is a difference in the runtime and compile time classpaths.

You can resolve this by going through the dependencies in your project which can be retrieved using the dependency:tree goal.

mvn dependency:tree

The output of which would be similar to the one below:

[INFO] +- google-api-translate-java:google-api-translate-javar:jar:0.97:system
[INFO] +- org.elasticsearch.client:elasticsearch-rest-high-level-client:jar:7.17.6:compile
[INFO] |  +- org.elasticsearch.client:elasticsearch-rest-client:jar:7.17.6:compile
[INFO] |  |  +- org.apache.httpcomponents:httpasyncclient:jar:4.1.4:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore-nio:jar:4.4.12:compile
[INFO] |  +- org.elasticsearch.plugin:mapper-extras-client:jar:7.17.6:compile
[INFO] |  +- org.elasticsearch.plugin:parent-join-client:jar:7.17.6:compile
[INFO] |  +- org.elasticsearch.plugin:aggs-matrix-stats-client:jar:7.17.6:compile
[INFO] |  +- org.elasticsearch.plugin:rank-eval-client:jar:7.17.6:compile
[INFO] |  \- org.elasticsearch.plugin:lang-mustache-client:jar:7.17.6:compile
[INFO] |     \- com.github.spullara.mustache.java:compiler:jar:0.9.6:compile

Post this you'll need to see if there are two versions of the same library in the dependencies. If yes, you'll need to remove either of the dependencies in order to fix the exception. Alternatively, you can consider excluding the dependency in your pom.xml file. Eg:

          <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-tools-api</artifactId>
            <version>2.5.1</version>
            <exclusions>
              <exclusion>
                <groupId>jetty</groupId>
                <artifactId>jetty</artifactId>
              </exclusion>
            </exclusions>
          </dependency>

Reference: How to exclude dependency in a Maven plugin?

Not the answer you're looking for? Browse other questions tagged or ask your own question.