greenlt
greenlt

Reputation: 1

Issue With Selenium and ChromeDriver in Intellij

I've added the selenium jar files and everything seems to be fine. When I run the program I get

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
    at org.openqa.selenium.remote.service.DriverService$Builder.<init>(DriverService.java:259)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.<init>(ChromeDriverService.java:101)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at sneakerbot.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 5 more

Please help me. I am stuck and new to using selenium. I'm on a mac and I'm pretty sure everything is up to date.

Upvotes: 0

Views: 11520

Answers (3)

Jack T
Jack T

Reputation: 335

You can create a new Selenium project with no worries also as simple Java program.

The needed libraries for Selenium are on their site, go to Java and press the Download link.

Then, put all the .jar files (they are 7: 2 in the root folder and 5 in libs folder) in another folder in your project.
Once done, go to FileProject Structure...LibrariesNew Project LibraryJava → select your folder with the 7 libs and you're done.

Talking about the ChromeDriver, in your case, on MacOS you don't have to specify .exe extension. This is needed only in Windows.

For doing so, you have to write:

System.setProperty("webdriver.chrome.driver","drivers/chromedriver");

(I put all the drivers in the folder "drivers" in my project, at the same level of the folder "src".)


That's all.
Cheers.

Upvotes: 0

Yevhen Danchenko
Yevhen Danchenko

Reputation: 1099

You definitely should start by setting your project up using any modern build tool such as Maven or Gradle. Of course, it is possible to add every dependency manually by downloading jar and attaching it to the project. However, it is a very inconvenient and complicated way. Build tools will manage your dependencies for you as well as run your tests and create reports.

You can use Maven, as @nugbe already suggested. Personally I'd prefer Gradle.

Here is a simple and straightforward solution:

  • Create a new Gradle project in IDEA, see this instruction for details. You should get a project skeleton for tests.
  • Now, it's time for your dependencies. The main source of the common java libraries is Maven Repository. Open it, search for the Selenium, then select Selenium Java in the results, then click on the desired version (3.141.59 is the most recent and stable). On its page, look for Gradle tab, click it and copy artifact URI: compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
  • Next, open your build.gradle file, look for dependencies { ... } section and paste your dependency.
  • Btw, you may also orchestrate browser drivers with WebDriverManager. Add this dependency to your project and you'll be able to get actual driver directly from your code with only two lines:
     WebDriverManager.chromedriver().setup();
     ChromeDriver driver = new ChromeDriver();
  • After adding all desired dependencies, update your Gradle project: click on the Gradle icon on the right side of the main window, and click Reimport All Gradle Projects:

enter image description here

  • Now, you should be able to create your test. So, create new java class in src/test/java folder, e.g. FirstTest.java
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class FirstTest {
    private static ChromeDriver driver;

    @BeforeClass
    public static void init() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @AfterClass
    public static void tearDown() {
        driver.close();
    }

    @Test
    public void firstTest() {
        driver.get("https://www.google.com");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.clear();
        searchBox.sendKeys("Selenium");
        searchBox.submit();
    }
}
  • And finally, you can run your test. You can either click on the green triangle next to the test method name in IDEA editor or open the terminal (look at the bottom of the IDEA window) and type: gradle clean test and here we go.

Upvotes: 0

nugbe
nugbe

Reputation: 149

This is the simplest code to use Selenium with Java (Working in Intellyj). Take a look on the firefox driver path "geckodriver".

First you need to create a new maven project and put this pom.xml:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>eus.ehu.selenium.tutorial</groupId>
  <artifactId>TestingIzapide</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
            <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13</version>
          <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Next create a Java class with this code:

import static org.junit.Assert.assertEquals;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.logging.Logger;

public class GoogleSearchTest {
    private WebDriver driver;
    private static final Logger LOGGER = Logger.getLogger(GoogleSearchTest.class.getName());

    @Before
    public void setUp() {
        System.setProperty("webdriver.gecko.driver", "./src/test/resources/firefoxdriver/geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
    }

    @Test
    public void testGooglePage() {
        String searchWord="Hello world";
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.clear();
        searchBox.sendKeys(searchWord);
        searchBox.submit();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        LOGGER.info("Alert: TITLE: "+ driver.getTitle());
        assertEquals("Google", driver.getTitle());
        LOGGER.info("Alert: TITLE: "+ driver.getTitle());
    }
}

Hope this code helps to start a new project. If you are new in Selenium and comfortable with python I'd recommend you to use Python, but is a personal choice...

cheers

Upvotes: 1

Related Questions