Thursday, October 23, 2014




WebDriver is an interface in Selenium. WebDriver provides a set of methods that we use to automate web application for testing purpose.


1. get(String URL) - This method will load a web page of given URL in the current browser.

Example: driver.get(“www.google.com”);


2. getCurrentUrl() – This method will return a string representing URL of the current web page that the browser is looking at.

Syntax: driver.getCurrentUrl();


3. getTitle() - This method will return title of the current web page.

Syntax: driver.getTitle();


4. getPageSource() - This method will provide you the source of the last loaded page.

Syntax: driver.getPageSource();


5. close() - If there is multiple windows opened, this method will close the current window and quit the browser, if the current window is the last window opened.

Syntax: driver.close();


6. quit() - Quits all the windows opened.

Syntax: driver.quit();


7. getWindowHandles() - Return a set of string representing window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo().WebDriver.Options.window().

Syntax: driver.getWindowHandles();


8. getWindowHandle() - Return a string representing window handle of the main window that uniquely identifies it within this driver instance.

Syntax: driver.getWindowHandle();





9. manage() - Gets the Option interface.

Example: driver.manage().addCookie(cookie);


Selenium WebDriver Commands Example


package testng;
 
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class SeleniumDriver {
 
    public static void main(String[] args) {
 
        System.setProperty("webdriver.ie.driver",
                "C:\\Program Files\\Internet Explorer\\iexplore.exe");
 
        WebDriver driver = new FirefoxDriver();
 
        driver.get("http://www.google.com");
 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
        WebElement element = driver.findElement(By.name("q"));
 
        System.out.println("URl of current web page -" + " "
                + driver.getCurrentUrl());
 
        element.sendKeys("Selenium!");
 
        System.out.println("Title of current web page -" + " "
                + driver.getTitle());
 
        System.out.println("Window Handle -" + " " + driver.getWindowHandle());
 
        System.out.println("Page Source -" + " " + driver.getPageSource());
 
        driver.close();
 
    }
}