How to work with selenium-chrome-driver in Maven without ChromeDriver.exe

Viewed 93456

I add the below dependency and code for Opening Chrome,but browser is not opening.

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.50.0</version>
</dependency>

My code :-

package example;
import org.openqa.selenium.WebDriver;`
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class DepChrome {

    @Test
    public void testBrowser() {
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    }
}
5 Answers

There are two ways.

  1. Easiest way is to download chromedriver from the this location

    Download chrome web driver

    Then create a source folder in your project. (Ex : BrowserDrivers) and add downloaded library into this.

    Then set the chrome driver path in the automation script using setProperty command as follow.

            System.setProperty("webdriver.chrome.driver", "BrowserDrivers/chromedriver.exe"); 
    
  2. But there is another way. This is more suited for maven build. Add following dependencies in to the POM.xml file.

    There are 2 dependencies. One for Chrome Driver. But to use chrome driver dependency you have to add webdrivermanager dependency. It is compulsory dependency for browser driver. So always you have to add both of them. For more details refer this link Github Webdriver manager link

    <dependency>
         <groupId>io.github.bonigarcia</groupId>
         <artifactId>webdrivermanager</artifactId>
         <version>2.2.5</version>
    </dependency>
    

    And add chrome driver dependency also.

    <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-chrome-driver</artifactId>
         <version>2.50.0</version>//Your chrome driver version
    </dependency>
    

    Then in your automation script use this line instead of System.setProperty command to declare chrome driver.

    ChromeDriverManager.getInstance().setup();
    

    UPDATE: the use of ChromeDriverManager is deprecated, use this instead:

    import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    WebDriverManager.getInstance(CHROME).setup();
    

Check below code -

package example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class DepChrome  {
    @Test
    public void testBrowser() {
        WebDriver driver;
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();        
        driver.get("https://google.com");
        String title = driver.getTitle();
        System.out.println(title);      
        driver.quit();      
    }
}

With the following two maven dependencies, you do not need to set system properties at all, this should work

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdriver-manager.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>${selenium-chrome-driver}</version>
        </dependency>

WebDriver driver;

@BeforeSuite
public void setUp(){
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.get("http://www.ebay.in");
    driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
    }

In Maven, with the use of ChromeDriver.exe:

import static io.github.bonigarcia.wdm.DriverManagerType.CHROME;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

import org.testng.annotations.Test;

public class MavenTest {

    @Test
    public void TestMaven()
    {
    System.setProperty("webdriver.chrome.driver", "D:\\Sumit_Backup\\Automation\\Workspace\\Maven\\src\\Browser\\chromedriver.exe");
    WebDriver driver= new ChromeDriver();
    driver.get("http://testng.org/doc/maven.html");
    driver.manage().window().maximize();
}
}
Related