Few Basic Commands and Operations with Selenium 2.0

The biggest change in Selenium recently has been the inclusion of the WebDriver API.

It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just “WebDriver” or sometimes as Selenium 2.

Selenium 1.0 + WebDriver = Selenium 2.0

First of everything, decide if you are going to write your Selenium tests in a high-level language like Java, Ruby, Python, C#, and PHP.

A very simple Selenium test looks like this in Java:

There are too many command operations in webdriver and will look into them one by one. Lets cover a few right now with syntaxes followed by a very simple execution of these syntaxes at the end.

1. Creating New Instance Of Firefox Driver

WebDriver driver = new FirefoxDriver();

2.  Applying Implicit Wait in webdriver

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

3. Maximize Browser Window in webdriver

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

4. Navigate to URL or To Open URL in Browser

driver.get(“http://templates.fxserver.com/IBMdemo/?command=b3A9U0hPV19GT1JNJnVuaXRfaWQ9MTAwJmZvcm1fbGFuZz11bmc=”);

5. Get page title in selenium webdriver

driver.getTitle();

6.Typing text in textbox or text area

driver.findElement(By.xpath(“.//*[@id=’100′]/table/tbody/tr[2]/td/table[1]/tbody/tr[1]/td[2]/input”)).sendKeys(“Anisha”);

7. Selecting value from drop down in selenium webdriver.

  • Select By Visible Text

Select mydrpdwn = new Select(driver.findElement(By.name(“state”)));
mydrpdwn.selectByVisibleText(“Alabama”);

It will select value from drop down list using visible text value = “Alabama”.

  • Select By Value

Select listbox = new Select(driver.findElement(By.name(“country”)));
listbox.selectByValue(“AL”);

It will select value by value = “Albania”.

package dpckg;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Dclass {

 public static void main(String[] args) 
 {
 // TODO Auto-generated method stub
 
 //Creating New Instance Of Firefox Driver
 WebDriver driver = new FirefoxDriver();
 
 //Implicit wait of 10 seconds
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
 //Maximize browser window
 driver.manage().window().maximize(); 
 System.out.println("FireFox opening successful");
 
 //Navigate to URL or To Open URL In Browser
 driver.get("http://templates.fxserver.com/IBMdemo/?command=b3A9U0hPV19GT1JNJnVuaXRfaWQ9MTAwJmZvcm1fbGFuZz11bmc="); 
 System.out.println("The navigation is successful"); 
 
 //Get page title in webdriver
 String title = driver.getTitle();
 System.out.print(title);
 
 //Typing text in textbox or text area
 driver.findElement(By.xpath(".//*[@id='100']/table/tbody/tr[2]/td/table[1]/tbody/tr[1]/td[2]/input")).sendKeys("Anisha");
 System.out.println("\nThe first name is populated");
 
 //Selecting value from drop down in webdriver.
 
 //Select by visible text
 Select mydrpdwn = new Select(driver.findElement(By.name("state")));
 mydrpdwn.selectByVisibleText("Alabama");
 
 //Select by value
 Select listbox = new Select(driver.findElement(By.name("country")));
 listbox.selectByValue("AL");

In this way, in my first webdriver script, a very few basic commands to perform actions on web element of your web page have been covered. Remember this is just the beginning, lot more to come .. 🙂