Write first automation script using selenium webdriver

How to write your first automation script using selenium web driver and chrome webdriver. In this selenium automation tutorial, we are going to cover the initial requirement to start automation with selenium webdriver using java and the chrome browser.

In the last tutorial, we have seen how to configure Java and eclipse at your machine to start the automation with selenium web driver using java.

Pre requirement

Configure Java(JDK) and Eclipse for automation using selenium web driver with java 

If your Java platform and eclipse are ready to create to new java project, then let’s continue the next step to create an automation script using selenium web driver with java.

Download Selenium web driver

Download the latest version of selenium API form https://docs.seleniumhq.org/download/

Download Selenium Web driver

Download the latest version of the chrome driver

  https://chromedriver.storage.googleapis.com/index.html?path=2.40/

Download Chrome web driver

Note: Choose all the API according to your system configuration Like if you are using window download the ZIP for window and so on.

Add selenium web driver API in your project

 After download all the API now you need to configure these API inside your working project. Below are the simple steps that you need to follow.

Create a new java project in eclipse.

File->new->java project

Right-click on the project and click on “properties ” at the bottom.

automation-script-using-selenium-web-driver

Add Selenium web driver API as external JARs from your download folder.

automation-script-using-selenium-web-driver

Test your configuration Create a class and try to execute the below script.

Test script with selenium web driver.

package com.SeleniumEasyTestCases;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumWithCodebun {
 
 static WebDriver driver;
 
 public static void main(String args[]){
  
  System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
  driver=new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("http://www.seleniumeasy.com/test/");
  driver.close();
 }

}

NOTE: In System.setProperty(“webdriver.chrome.driver”, “C:\\chromedriver.exe”);

You need to set the path of chromedriver.exe according to the location In my case its in C drive.

Code Line by line

 Inside this System.setProperty() we need to pass browser driver accordingly in as a first parameter and the path of the driver in the second parameter.

System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

Launch Browser Using Selenium Web driver

Create a new instance of Chrome Driver here I am going to create a script for chrome browser you can change this instance depends on the browser you are going to use.

WebDriver driver=new ChromeDriver();

Maximize the browser window using selenium web driver.

driver.manage().window().maximize();

Navigate to URL using selenium web driver

For the target URL pass URL in the get method, it will open the browser and pass the link in the URL.

driver.get("http://www.seleniumeasy.com/test/");

Close browser using selenium web driver

driver.close();

Step by step complete Automation tutorial using selenium web driver with Java

Leave a Comment

Your email address will not be published.