Monday, 23 May 2016

Frame Handling

Working with Frames in Selenium Webdriver


What is iFrame ?

An iFrame (Inline Frame) is an HTML document embedded inside the current HTML document on a website. iFrame HTML element is used to insert content from another source, such as an advertisement, into a Web page. A Web designer can change an iFrame's content without making them reload the complete website. A website can have multiple frames on a single page. And a frame can also have inner frames (Frame in side a Frame).

In Selenium to work with iFrames, we have different ways to handle frame depending on the need. Please look at the below ways of handling frames
Select a frame by its (zero-based) index. That is, if a page has multiple frames (more than 1), the first frame would be at index "0", the second at index "1" and so on.
Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. i.e the driver focus will be now on the frame. What ever operations we try to perform on pages will not work and throws element not found as we navigated / switched to Frame.

Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found.


public void switchToFrame(int frame) { try { driver.switchTo().frame(frame); System.out.println("Navigated to frame with id " + frame); } catch (NoSuchFrameException e) { System.out.println("Unable to locate frame with id " + frame + e.getStackTrace()); } catch (Exception e) { System.out.println("Unable to navigate to frame with id " + frame + e.getStackTrace()); } }

Parameters: name Or Id - the name of the frame or the id of the frame element.
Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found


public void switchToFrame(String frame) { try { driver.switchTo().frame(frame); System.out.println("Navigated to frame with name " + frame); } catch (NoSuchFrameException e) { System.out.println("Unable to locate frame with id " + frame + e.getStackTrace()); } catch (Exception e) { System.out.println("Unable to navigate to frame with id " + frame + e.getStackTrace()); } }


Parameters: Index - (zero-based) index
Example: if iframe id=webklipper-publisher-widget-container-frame, it can be written as driver.switchTo().frame("webklipper-publisher-widget-container-frame"); Below is the code snippet to work with switchToFrame using frame id.


Sunday, 22 May 2016

Page Object Model (POM)

What is POM?

  • Page Object Model is a design pattern to create Object Repository for web UI elements.
  • Under this model, for each web page in the application there should be corresponding page class.
  • This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements.
  • Name of these methods should be given as per the task they are performing i.e., if a loader is waiting for payment gateway to be appear, POM method name can be waitForPaymentScreenDisplay().

Advantages of POM

  1. Page Object Patten says operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.
  2. Second benefit is the object repository is independent of testcases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional testing and at the same time with JBehave/Cucumber for acceptance testing.
  3. Code becomes less and optimized because of the reusable page methods in the POM classes.
  4. Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the home page, the method name will be like 'gotoHomePage()'.  

Why POM ?

Starting a UI Automation in Selenium WebDriver is NOT a tough task. You just need to find elements, perform operations on it . Script maintenance looks easy. But with time test suite will grow. As you add more and more lines to your code, things become tough.
The chief problem with script maintenance is that if 10 different scripts are using the same page element, with any change in that element, you need to change all 10 scripts. This is time consuming and error prone.
A better approach to script maintenance is to create a separate class file which would find web elements , fill them or verify them. This class can be reused in all the scripts using that element. In future if there is change in the web element , we need to make change in just 1 class file and not 10 different scripts.
This approach is called Page Object Model(POM). It helps make code more readable, maintainable, and reusable.

Monday, 16 May 2016

Navigation Methods in WebDriver with Examples


Navigate.To(URL)

Method Name: navigate.to(URL)
Syntax: driver.navigate().to(URL);
Purpose: This methods 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 – It should be a fully qualified URL.
Example:
@Test
 public void navigationToURLExample()
 {
  
  driver= new FirefoxDriver();
  driver.navigate().to("http://www.google.com");
 }

Navigate.To(String)

Method Name: navigate.to(String)
Syntax: driver.navigate().to(String);
Purpose: This methods Load a new web page in the current browser window. It is an Overloaded version of to(String) that makes it easy to pass in a URL.
Parameters: URL String
Example:
 
@Test
 public void navigationToStringExample()
       {
  driver= new FirefoxDriver();
  String URL="http://www.facebook.com";
  driver.navigate().to(URL);
 }

Navigate.Back()

Method Name: navigate().back()
Syntax: driver.navigate().back();
Purpose: To move back a single "item" in the web browser's history. And it will not perform any action if you are on the first page viewed.
Parameters: N/A
Example:
@Test
 public void navigationBackExample()
       {
  driver= new FirefoxDriver();
  String URL="http://www.facebook.com";
  driver.navigate().to(URL);
  driver.findElement(By.linkText("Forgot your password?")).click();
  driver.navigate().back();
 }
In the above example, to work with navigate back method, we atleast need to travel to one single page in the browser history.
The first page we have opened is facebook.com page and then traveled to forgot password page. Now the browser has two pages in the history. If we now use navigate.back(), it will redirect the user to facebook.com page.

Navigate.Forward()

Method Name: navigate().forward()
Syntax: driver.navigate().forward();
Purpose: To move a single "item" forward in the web browser's history. And it will not perform any action if we are on the latest page viewed.
Parameters: N/A
Example:
@Test
 public void navigationForwardExample()
       {
  driver= new FirefoxDriver();
  String URL="http://www.facebook.com";
  driver.navigate().to(URL);
  driver.findElement(By.linkText("Forgot your password?")).click();
  driver.navigate().back();
  Thread.sleep(1000);
  driver.navigate().forward();
 }
In the above example, to work with navigate forward method, we atleast need to travel to one or multiple pages in the browser history.
The first page we have opened is facebook.com page and then traveled to forgot password page. Now the browser has two pages in the history. If we now use navigate.back(), it will redirect the user to facebook.com page.
And now again if we navigate.forward(), it will redirect the user to forgot password page.

Saturday, 14 May 2016

What is Robot Class in Selenium WebDriver

Using Robot class, we can simulate keyboard event in Selenium.
To use keyboard event you have to use to method of Robot class.

Robot Class in Selenium Webdriver

keyPress()
keyRelease()
Each key has to be press and release respectively-

Scenario- Which cover enter key
1-Open Facebook.
2- Enter Username and password.
3- Using robot class press Enter button.

Program for  Robot Class in Selenium Webdriver