How to start automation with selenium webdriver

Share:
In last article on selenium we discussed basics of selenium. How selenium works and what are different ways to automate with selenium. (read here What is selenium? How selenium works ?). we discussed selenium IDE and selenium webdriver in that post. In this article will learn How to start automation with selenium webdriver. 

This post is for beginners to right hello world script with selenium webdriver.  Here we will discuss setting up java, Start with eclipse, create new project, Set up java build path for selenium webdriver, a sample script.

How to start automation with selenium webdriver


Setup needed for writing Automation script with selenium webdriver

  •  Latest Java setup. Install JDK and JRE and set the Environment variables properly.  (to check java is properly set or not, OPEN Command Prompt, type "java -version")
  • Download Eclipse. Open Eclipse and create work space.  
  • Download selenium standalone server jar (click here to download. Download it for java)
  • Download chromedriver.exe (to automate in chrome) Click here to download 

Writing first script with Selenium webdriver with Java


  • First you need to create new project in eclipse

    For this, Go to File>New>Project. Select java Project. Go next Give project name and  Finish.  You will see a folder with given project name in Project Explorer
  • Create a package inside the project

    For this, right click on project,  select new, package. Give package name and finish.
  • Create a class file inside project.

    for this, right click on package, select new, class. Give proper class name and finish.

    How to start automation with selenium webdriver
    Setting up Eclipse

  • Add Selenium standalone server jar to libs.

    For this right click on Project, Go to Build path>Configure build path. Go to Libraries, click on Add External Jars and jar you downloaded.

    here you can see added selenium server standalone jar. Now go to Order and Export and select added jar, Apply and you are done. 
    configure build path for selenium webdriver
    Java Build path

Here is the simple automation code you can add in to class and run. This code will open Bing URL, search query and press Go Button. This is only sample code like Hello word script for selenium webdriver automation for beginners. 


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

public class addMeFast {
public WebDriver driver;
String winHandleBefore = driver.getWindowHandle();

 public static void main(String args[]) throws Exception {
     
       
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver.exe");//add path of your chromedriver.exe

        WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.bing.com");

        Thread.sleep(3000);

driver.findElement(By.name("q")).clear(); 
        driver.findElement(By.name("q")).sendKeys("software testing tips and tricks");
         driver.findElement(By.name("go")).click();   
 
}
}

So First you need to  define webdriver 
       
        public WebDriver driver;
String winHandleBefore = driver.getWindowHandle();

Based on browser (chrome, mozila, safari or IE ) you need to set the property. For chrome you need to pass driver id and path where it is located
System.setProperty("webdriver.chrome.driver","C:\\Automation\\chromedriver.exe")

 WebDriver driver = new ChromeDriver();

To open chrome browserdriver.manage().window().maximize();

To hit URL
driver.get("http://www.bing.com");


To find element driver.findElement is method to find the element. as shown in code, you can find element by Name, Class or othe Xpath.

To find Name,Class,Id for element 


  • right click on element you want to find
  • click on inspect
  • you can elements as shown in below screenshot. 
Here is the detailed post How to find Xpaths in Selenium Webdriver
finding web element xpath for seleniumfinding Finding web element's xpath for selenium


Now this code will type in textbox and press go   
driver.findElement(By.name("q")).clear(); 
driver.findElement(By.name("q")).sendKeys("software testing tips and tricks w");
driver.findElement(By.name("go")).click();