August 19, 2016

Selenium Web Driver

Selenium WebDriver is an interface and it is a web automation framework that allows you to execute your tests against different browsers.
We've the following browser support
  • Firefox
  • Internet Explorer
  • Safari
  • Google Chrome
  • Opera Browser
  • HtmlUntilDriver (No User Interface)
Using WebDriver we develop the test cases using various programming languages.
Following programming languages are supported by WebDriver
  • Java
  • .Net 
  • PHP
  • Python
  • Perl
  • Ruby
We've several methods in Selenium WebDriver, few of them are listed below
  1. get()
  2. getCurrentUrl();
  3. getTitle()
  4. findElements()
  5. findElement()
  6. getPageSource()
  7. close()
  8. quit()
  9. getWindowHandles()
  10. getWindowHandle()
  11. manage()
  12. navigate()
Let’s look into the detail for each of the above methods.
Method Name: - get()
Syntax: get(url)
Purpose: It will load a new web page in the current browser window. This is done using an http get operation, and the method will block until the load is complete.
Parameters: URL - The URL to load and it should be a fully qualified URL
Example:
@Test
Public void testGetMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
}
Method Name: getCurrentUrl()
Syntax: getCurrentUrl()
Purpose: Gets a string representing the current URL that the browser is opened.
Returns: The URL of the page currently loaded in the browser
Example:
@Test
Public void testGetCurrentURLMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentURL = driver. getCurrentUrl();
System.out.println(currentURL);
}
Method Name: getTitle()
Syntax: getTitle();
Purpose: Gets the title of the current web page.
Returns: The title of the current page, with leading and trailing white space stripped, or null if one is not already set
Example:
@Test
Public void testGetTitleMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String pageTitle = driver. getTitle ();
System.out.println(pageTitle);
}
Method Name: findElements()
Syntax: findElements(By byLocator);
Purpose: Find all elements within the current page using the given criteria.
Parameters: By - The locating technique (Like id, className, xPath,cssSelector, linkText etc.,)
Returns: A list of all WebElements, or an empty list if nothing matches
Example:
@Test
Public void testFindElementsMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
List<WebElement> listElements = driver. findElements(By byLocator);
}
Method Name: findElement ()
Syntax: WebElement findElement(By by);
Purpose: Find the first WebElement using the given criteria.
Parameters: By - The locating technique (Like id, className, xPath,cssSelector, linkText etc.,)
Returns: The first matching element on the current page
Throws: NoSuchElementException - it will return exception if no matching elements are found
Example:
@Test
Public void testFindElementMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
WebElement element = driver. findElement();
}
Method Name: getPageSource()
Syntax: getPageSource();
Purpose: Get the source of the currently loaded page. If the page has been modified after loading (for example, by JavaScript) there is no guarantee that the returned text is that of the modified page.
Returns: The source of the current page
Example:
@Test
Public void testGetPageSourceMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String pageSource = driver. getPageSource ();
System.out.println(pageSource);
}
Method Name: close()
Syntax: void close();
Purpose: Close the current window, if there are multiple windows, it will close the current window which is active and quits the browser if it's the last window opened currently.
Example:
@Test
Public void testDriverCloseMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
driver. close();
}
Method Name: quit()
Syntax: void quit();
Purpose: Quits this driver instance, closing every associated window which is opened.
Example:
@Test
Public void testDriverQuitMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
driver. quit();
}
Method Name: getWindowHandles()
Syntax: Set getWindowHandles();
Purpose: Return a set of window handles which can be used to iterate over all the open windows of this Webdriver instance by passing them to switchTo().window() method
Returns: A set of window handles which can be used to iterate over all the open windows.
Example:
@Test
Public void testGetWindowHandlesMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
List<String> pageHandles = driver. getWindowHandles ();
For(String pageHandle: pageHandles)
System.out.println(pageHandle);
}
Method Name: getWindowHandle()
Syntax: String getWindowHandle();
Purpose: To get the current window handle, this is used to navigate back to the parent window
Returns: Return a string which represent the current window handle
Example:
@Test
Public void testGetWindowHandleMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String pageHandle = driver. getWindowHandle();
System.out.println(pageHandle);
}
Method Name: switchTo
Syntax: driver.switchTo()
Purpose: To perform the next future commands on different frame, alert or window.
Returns: A Target Locator which can be used to switch or select a frame or window
Example:
@Test
Public void testSwitchToWindowMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentWindowName = driver.getWindowHandle();
driver. switchTo ().window(<window name>/<window id>);
System.out.println(pageTitle);

// To close the current window
Driver.close();

//To switch to parent window
Driver.switchTo.window(currentWindowName);
}

@Test
Public void testSwitchToAlertMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentWindowName = driver.getWindowHandle();
driver. switchTo ().alert();

//To Accept the alert
driver.accept();

//To dismiss the alert
driver. dismiss();

//To switch to parent window
Driver.switchTo.window(currentWindowName);
}

@Test
Public void testSwitchToFrameMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentWindowName = driver.getWindowHandle();
driver. switchTo ().Frame(<FrameWebElement>/<Frame ID> / <Frame Name>);

//To switch to parent Frame
Driver.switchTo.defaultContent();}
Method Name: manage()
Syntax: driver.manage()
Purpose: To manage the driver (Maximize, minimize, implicitlyWait etc.)
Returns: Returns an instance of underlying implementation of Interface Options which could be EventFiringOptions / RemoteWebDriverOptions.
Example:
@Test
Public void testManageWindowMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
//To Maximize the window
driver. manage().window().maximize();

//To get Size of the window
Dimension d= driver. manage().window().getSize();

//To set Size of window
driver.manage().window().setSize(new Dimension(width, height));

//To set wait time to the window
driver.manage().timeouts().implicitlyWait(<integer value>, TimeUnit.SECONDS)
}
Method Name: navigate()
Syntax: WebDriver.Navigation navigate()
Purpose: An abstraction allowing the driver to access the browser's history and to navigate to a given URL.
Returns: A WebDriver.Navigation that allows the selection of what to do next
Example:
@Test
public void navigationToURLExample()
{
driver= new FirefoxDriver();
driver.get(URL);
System.out.println(driver.getTitle());
driver.navigate().to("http://www.google.com");
System.out.println(driver.getTitle());
}

@Test
public void navigationBackExample()
{
driver= new FirefoxDriver();
String URL="http://www.facebook.com";
driver.navigate().to(URL);
System.out.println(driver.getTitle());

driver.findElement(By.linkText("Forgot your password?")).click();
System.out.println(driver.getTitle());

driver.navigate().back();
System.out.println(driver.getTitle());
}

@Test
public void navigationForwardExample()
{
driver= new FirefoxDriver();
String URL="http://www.facebook.com";
driver.navigate().to(URL);
System.out.println(driver.getTitle());
driver.findElement(By.linkText("Forgot your password?")).click();
System.out.println(driver.getTitle());
driver.navigate().back();
System.out.println(driver.getTitle());
driver.navigate().forward();
System.out.println(driver.getTitle());
}

@Test
public void navigationRefreshExample()
{
driver= new FirefoxDriver();
String URL="http://www.facebook.com";
driver.navigate().to(URL);
driver.findElement(By.linkText("Forgot your password?")).click();
driver.findElement(By.id("identify_email")).sendKeys("sample@email.com");
driver.navigate().refresh();
}