Sunday, 16 October 2022

Page Object Model in Selenium? How to implement Page Object Model in Automation Framework?

 

                                                                      Source - etechfactory

SDET Interview Question and Answers.  

Jenkins Interview Questions and Answers

Appium Interview Questions and Answers

Selenium Interview Questions and answers

GIT Interview Questions and Answers


Check Selenium Interview questions from 1 to 10 below:


Check Selenium-SDET-UIAutomation Questions set 11 to 20:
Qus Set - 11 to 20




Let's just know about Selenium before deep diving into the Page Object Model:

Webdriver:

WebDriver drives a browser natively, as a user would, either locally or on a remote machine using the Selenium server, marks a leap forward in terms of browser automation.

Selenium WebDriver refers to both the language bindings and the implementations of the individual browser control code. This is commonly referred to as just WebDriver and its not a class but an INTERFACE.

Now let's focus on our main topic on PageObjectModel ==>

Page object models

The page object model is a design pattern considered highly popular in Selenium test automation. It is generally used to design patterns in Selenium to enhance code maintenance and reduce duplicate codes. POM can be used in any kind of framework, either modular, keyword-driven, data-driven, hybrid, etc.

A page object is an object-oriented class that serves as an interface to the page of your application under test (AUT). The tests use methods of these page objects further when they want to interact with the UI of that particular page. The advantage is that if UI of that particular page changes, the tests themselves don’t need to change, but there is a need to change the code within the page object. Subsequently, all changes should support the new UI located in a single place.

Table of Contents:

  • Overview
  • Implementation
  • Demo Code
  • Advantages
  • Conclusion

Overview

Within your web app’s UI, there are areas where your tests interact with. A Page Object only models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix needs only to be applied in one place.

Page Object is a Design Pattern that has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT. The tests then use the methods of this page object class whenever they need to interact with the UI of that page. 

The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change. Subsequently, all changes to support that new UI are located in one place.


 What is page load strategy in Selenium?

Ans: Click Here For Answer In-Detail

Implementation

First, consider an example, typical of test automation, that does not use a page object:



To access the above code, check the GOOGLE DRIVE link.

There are two problems with this approach.

  • There is no separation between the test method and the AUT’s locators (IDs in this example); both are intertwined in a single method. If the AUT’s UI changes its identifiers, layout, or how a login is input and processed, the test itself must change.

  • The ID-locators will be spread across multiple tests, in all tests that have to use this login page.

Applying the page object techniques, this example could be rewritten like this in the following example of a page object for a Sign-in page.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * Page Object encapsulates the Sign-in page.
 */
public class SignInPage {
  protected WebDriver driver;

  // <input name="user_name" type="text" value="">
  private By usernameBy = By.name("user_name");
  // <input name="password" type="password" value="">
  private By passwordBy = By.name("password");
  // <input name="sign_in" type="submit" value="SignIn">
  private By signinBy = By.name("sign_in");

  public SignInPage(WebDriver driver){
    this.driver = driver;
  }

  /**
    * Login as valid user
    *
    * @param userName
    * @param password
    * @return HomePage object
    */
  public HomePage loginValidUser(String userName, String password) {
    driver.findElement(usernameBy).sendKeys(userName);
    driver.findElement(passwordBy).sendKeys(password);
    driver.findElement(signinBy).click();
    return new HomePage(driver);
  }
}

and the page object for the Home page could look like this:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * Page Object encapsulates the Home Page
 */
public class HomePage {
  protected WebDriver driver;

  // <h1>Hello userName</h1>
  private By messageBy = By.tagName("h1");

  public HomePage(WebDriver driver){
    this.driver = driver;
    if (!driver.getTitle().equals("Home Page of logged in user")) {
      throw new IllegalStateException("This is not Home Page of logged in user," +
            " current page is: " + driver.getCurrentUrl());
    }
  }

  /**
    * Get message (h1 tag)
    *
    * @return String message text
    */
  public String getMessageText() {
    return driver.findElement(messageBy).getText();
  }

  public HomePage manageProfile() {
    // Page encapsulation to manage profile functionality
    return new HomePage(driver);
  }
  /* More methods offering the services represented by Home Page
  of Logged User. These methods in turn might return more Page Objects
  for example click on Compose mail button could return ComposeMail class object */
}

So now, the login test would use these two page objects as follows.

Learn Eager and Lazy Initialization in Singleton Design Patterns with Examples


{>DEMO CODE<}




To get access of the above code, please refer to the google doc 


For any doubt or career guidance, reach out to me :


There is a lot of flexibility in how the page objects may be designed, but there are a few basic rules for getting the desired maintainability of your test code.

***************************************************
What are the new features in Selenium 4? VERY IMP 

Click Here For Detailed Answers

******************************************

Advantages

  • There is a clean separation between the test code and page-specific code, such as locators (or their use if you’re using a UI Map) and layout.
  • There is a single repository for the services and operations the page offers rather than having these services scattered throughout the tests.

In both cases, this allows any modifications required due to UI changes to all be made in one place. Helpful information on this technique can be found on numerous blogs, as this ‘test design pattern’ is becoming widely used. 

We encourage readers who wish to know more to search the internet for blogs on this subject. Many have written on this design pattern and can provide helpful tips beyond the scope of this user guide. To get you started, we’ll illustrate page objects with a simple example.



Conclusion

Page Objects and Page Factory make it easy to model web pages in Selenium and test them automatically. It makes the life of QA engineers and developers simpler. When done correctly, page object classes can be reused across the entire test suite and allow you to implement automated Selenium tests for the complete project early without making any compromise on agile development. By abstracting away user interactions in your page object model, which keeps test routines simple and light, you may adapt test suite based on changing requirement with minimum efforts. 


If you still have doubts about PageObjectModel, then go for the video sessions with live doubt clear, details can be found below.

*******************************************************************

For any doubts or career guidance, reach out to me 


********************************************************************

TOP 15 BDD - CUCUMBER Interview Q&A


************************************************

✍️AUTHORLinkedIn Profile

************************************************

Learn (API-Microservice-Automation)Testing + Selenium UI Automation + SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session. Check out the Training Page for Course Content or reach out @whatsapp +91-9619094122. 

This includes class-notes, 400+ interview questions, 3 projects, 20+ Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET. We provide full support in clearing doubts & it is paid with very minimal amount.

We will also help you to design the best utility package and framework for your next project. Any doubts then reach out to us at the below whatsapp link.

Learning is directly proportional to earning so keep learning....

For more details whatsapp: https://lnkd.in/dnBWDM33


Check below link for question and answers with the code:

Top API Interview Question 1-10

Top API INterview Questions 11-20 

Top API AUTOMATION Interview Q&A - 21-30

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python


 

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...