Automation Using Selenium Webdriver
Showing posts with label Functional Implementation in Selenium Webdriver Exp. Show all posts
Showing posts with label Functional Implementation in Selenium Webdriver Exp. Show all posts

Tuesday 18 October 2016

Functional Implementation in Selenium Webdriver Exp

Functional Implementation:
In this implementation we will split up the page on the basis of functionalities and will have the methods like LoginToGmailAsValidUser and LoginToGmailAsInvalidUser.

See the below code snippet::

public class GmailLoginPage
 {
  private final WebDriver driver;
//Page Object constructor which passes the driver context forward
   public LoginPage(WebDriver driver) {
      this.driver = driver;
  }
  By usernameloc = By.id("Email");
  By passwordloc = By.id("Passwd");
  By loginButtonloc = By.id("signIn");
 
  public HomePage LoginToGmailAsValidUser(String username, String password) 
{
 driver.findElement(usernameloc ).sendKeys(username);
driver.findElement(passwordloc ).sendKeys(password);
driver.findElement(loginButtonloc   ).click();
    return new InboxPage(driver) 
  }
  public GmailLoginPage LoginToGmailAsInvalidUser(String username, String password) {
 driver.findElement(usernameloc ).sendKeys(username);
 driver.findElement(passwordloc ).sendKeys(password);
 driver.findElement(loginButtonloc   ).click();
    return this;
  }
}
The benefit of this approach is that the page structure is completely abstracted from the test layer. For example an extra checkbox “stay signed in” has been added on the login page and it also needs to be selected while logging a user in. So we simply need to add the extra code to handle this checkbox in the same method of the page class and it will not have any impact on the test layer, as the tests will still call the same method to login.

Structural Implementation:

In this approach the page is divided structurally depending upon the number of elements on the page
which we need to interact with. See the below code snippet:
public class GmailLoginPage
 {
  private final WebDriver driver;
//Page Object constructor which passes the driver context forward
  public LoginPage(WebDriver driver) 
{
      this.driver = driver;
  }
  By usernameloc = By.id("Email");
  By passwordloc = By.id("Passwd");
  By loginButtonloc = By.id("signIn");
 
  public HomePage typeUsername(String username) 
{
    driver.findElement(usernameloc).sendKeys(username);
    return this;
  }
public HomePage typePassword(String password)
 {
   driver.findElement(passwordloc ).sendKeys(password);
    return this;
  }
public HomePage clickOnSignin(String password) 
{
  driver.findElement(loginButtonloc   ).click();
   return new InboxPage(driver) 
  }
}


Challenges in Implementing Page Object Model:

Page object model is a very effective design pattern provided it is implemented correctly. I would like to cover some 
complex scenarios.

Whenever any pageObject service (method) results in to a new page navigation, then that new page should be returned by the method. Let’s take the above example of Gmail login and as we know the method LoginToGmail() will lead us to the Inbox page so this should have a return type of InboxPage. See the below code:

public HomePage LoginToGmail(String username, String password)
 {
    driver.findElement(usernameloc).sendKeys(username);
  driver.findElement(passwordloc ).sendKeys(password);
  driver.findElement(loginButtonloc   ).click();
    return new InboxPage(driver) 
  }
GmailLoginPage loginPage = new GmailLoginPage (driver);
InboxPage = loginPage.LoginToGmail(“username”,”password”);