Implementation
Now that you have configured ChromeDriver for your system, you can write a test case and run it using Google Chrome.
We will use the Eclipse IDE to run our Java Selenium test case on chrome using the ChromeDriver. Our test case should open Google Chrome, go to www.google.com, and enter Coding Ninjas in the search box.
Firstly you will need to add Selenium as a dependency to your project. You can do this by adding the .jar file to your project directory or adding Selenium as a dependency on your Maven or Gradle build files. The latter is the recommended approach.
pom.xml(Maven)
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.3</version>
</dependency>
build.gradle(Gradle)
dependencies{
compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-java’, version: 4.1.3
}
Now we can write the code for our test case. In the code, we will initialize the object of ChromeDriver using this command.
WebDriver driver = new ChromeDriver();

You can also try this code with Online Java Compiler
Run Code
We have also specified multiple custom options for running chrome and, lastly, to perform the principal task set earlier.
Program
package chromeselenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeTest {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
//adding options to use when running chrome.
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
// Instantiate a ChromeDriver class.
//Creating an object of ChromeDriver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//Deleting all the cookies
driver.manage().deleteAllCookies();
//Specifiying pageLoadTimeout and Implicit wait
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//launching the specified URL
driver.get("https://www.google.com/");
//Locating the elements using name locator for the text box
driver.findElement(By.name("q")).sendKeys("CodingNinjas");
//name locator for the google search button
WebElement searchIcon = driver.findElement(By.name("btnK"));
searchIcon.click();
}
}

You can also try this code with Online Java Compiler
Run Code
On running the above code as a Java Application using Eclipse, a new Google Chrome window should open automatically where Coding Ninjas should be searched on Google Search.
Output

Frequently Asked Questions
-
What is the difference between ChromeDriver and WebDriver?
WebDriver is an open-source tool for automated testing of web applications across different browsers. The WebDriver gives you the capability to navigate web pages, user input, JavaScript execution, etc. Whereas the ChromeDriver is a standalone server that implements the W3C WebDriver standard.
-
What is WebElement Selenium?
An HTML document has HTML elements; each consists of a start tag and an end tag between which the content lies. A Selenium WebElement is essentially an HTML element on a website. You can use WebElement to search for HTML tags and work with them or test them.
-
How do you assign a WebElement?
You can assign a WebElement by searching for an element by name, id, XPath, or other identifiers. We have set the google search button as a WebElement to perform a search in our example.
//name locator for the google search button
WebElement searchIcon = driver.findElement(By.name("btnK"))
searchIcon.click();
-
Why is Java used for Selenium?
Because of being such a prominent and old programming language, Java has an abundance of frameworks, plugins, APIs, and libraries supporting Java for test automation. That is why around 77% of Selenium Testers use Java which also makes knowledge sharing quick and easy.
Conclusion
This article extensively discussed running Selenium tests on Google Chrome using ChromeDriver and its implementation in Java using the Eclipse IDE. You can use the ChromeDriver to perform much more complex Selenium tests. The popularity and features of Google Chrome make ChromeDriver a vital tool for testing web applications. Moreover, Selenium provides cross-browser functionality allowing you to run your Selenium tests with ChromeDriver to execute all tests on the Chrome browser.
We hope that this blog has helped you enhance your knowledge regarding Selenium. If you would like to learn more, check out our articles on Selenium Interview Questions, Methods in Selenium, and Basics of Java. Do upvote our blog to help other ninjas grow. Happy Coding!