Showing posts with label Page object Model. Show all posts
Showing posts with label Page object Model. Show all posts

Friday, August 2, 2024

Automation Testing - Top interview question



In this post , we will be discussing answers to questions asked in interviews for automation


What is Automation Testing?

Automation testing is a software testing technique that uses automated tools and scripts to perform tests on software applications. The primary goal of automation testing is to increase the efficiency, effectiveness, and coverage of the testing process. This approach is especially beneficial for repetitive and regression testing tasks.

What is difference between automation and manual testing?

Automated testing uses scripts and tools to perform tests, whereas manual testing requires human intervention to execute tests. Automated testing is faster and more reliable for repetitive tasks, while manual testing is better for exploratory, usability, and ad-hoc testing

What are key features of automation?



What are different Types of tests that are automated?

Unit Tests
  • Validates: individual components or units of code.
  • Tools : junit, TestNG and microtests

Integration Tests

  • Validates: interaction between integrated units or components
  • Tools: junit, TestNG and microtests

Functional Tests

  • Validates: application functions as the specified requirements. Includes both UI and API workflows.
  • Tools: Selenium, Playwright, Rest Assured, Cucumber

Performance Tests

  • Validates: application's performance under various conditions. Includes load testing, stress testing.
  • Tools: Jmeter, LoadRunner, Locust

What are best suited tasks for automation?

Repetitive, time-consuming tasks, regression tests, smoke tests, performance tests, data-driven tests, and tasks that require precision and consistency are well-suited for automation.

What are most used automation tools and frameworks?

Popular tools and frameworks include Selenium, JUnit, TestNG, Cypress, Appium, Jenkins, Cucumber, Playwright and Robot Framework.

What is a test automation framework?

Definition: A structured set of guidelines and best practices designed to help testers and developers automate the testing of software applications. 

Key Components and Characteristics: 
  • Code Reusability: Encourages the reuse of code, reducing duplication and maintenance efforts. 
  • Scalability: Supports scaling of test scripts to handle larger and more complex applications.
  • Modularity: Test scripts and components are organized in a modular fashion for easier management. 
  • Maintainability: Provides clear guidelines and structure for easier maintenance and updates. 
  • Test Data Management: Offers mechanisms to handle test data efficiently. 
  • Reporting: Includes reporting tools for detailed test execution results and logs. 
  • Integration: Can integrate with various tools and systems (e.g., CI/CD, version control). 
  • Best Practices: Promotes best practices in coding, such as design patterns and naming conventions. 

Types of Automation Frameworks: 

  • Linear Scripting Framework: Record-and-playback approach.
  • Modular Testing Framework: Breaks down application into smaller, independent modules. 
  • Data-Driven Framework: Separates test scripts from test data. 
  • Keyword-Driven Framework: Uses keywords to represent actions for readability and maintenance. 
  • Hybrid Framework: Combines features of multiple frameworks. 
  • Behavior-Driven Development (BDD) Framework: Uses natural language constructs for test cases.

What are the merits and demerits of using Selenium for automation?

Merits of Selenium:

  • Open Source: Selenium is free to use, which makes it a cost-effective option for businesses of all sizes.

  • Language Support: Supports multiple programming languages.

  • Browser Compatibility: Works with all major browsers ensuring broad compatibility.

  • Platform Independence: Can run on various operating systems 

  • Framework Integration: Easily integrates with other tools and frameworks like TestNG, JUnit, Maven, Jenkins, and Docker, facilitating continuous integration and continuous deployment (CI/CD).

  • Community Support: Has a large and active community, providing extensive resources.

  • Flexibility: Offers flexibility in designing tests, such as using a wide range of locators for identifying web elements.

Demerits of Selenium:

  • Limited to Web Applications: doesn't support desktop or mobile application testing natively.

  • Steep Learning Curve: Requires knowledge of programming languages and frameworks

  • Maintenance Overhead: Tests can become fragile and require updates with UI changes.

  • No Built-in Reporting: use of third-party tools for generating test reports.

  • Complexity with Dynamic Elements: Handling dynamic web elements (such as AJAX-based elements) can be complex and may require additional coding.

  • Browser-Specific Issues: Tests might behave differently across different browsers.

  • Performance: Can be slower compared to other automation tools like playwright

How does a (CI/CD) pipeline integrate with automation testing?

CI/CD pipelines automatically run tests every time code is committed, ensuring new changes don't break existing functionality. They integrate with automation tools to execute tests, report results.

How do you handle dynamic elements in web automation?

We can handle dynamic elements using dynamic XPath/CSS selectors, waiting mechanisms (explicit, implicit, and fluent waits), and interacting with elements based on properties like text, class, or other attributes.

What is the difference between functional and non-functional automation?

Functional testing checks if the application functions according to requirements. Non-functional testing evaluates aspects like performance, usability, and security.

What is the Page Object Model (POM)?

The Page Object Model (POM) is a design pattern in Selenium that enhances test maintenance and reduces code duplication by encapsulating web elements and their interactions in separate classes.

What is data-driven testing?

Data-driven testing is a methodology in which test data is stored separately from test scripts, allowing tests to be run multiple times with different sets of data.

What is a headless browser?

A headless browser is a web browser without a graphical user interface (GUI). It is used for faster, resource-efficient testing where GUI interaction is not necessary.

How do you handle an element with locator dynamically changing?

Here are a few approaches to manage dynamically changing locators:
  • Relative locators can help you find elements based on their position relative to other stable elements.
  • XPath axes allow you to navigate to elements based on their relationships to other elements.
  • Use CSS selectors to navigate through the DOM based on the hierarchy of elements
  • Some locators allow the use of regular expressions to match dynamic parts of the locator.
  • Use of java script executor

What are  ‘flaky tests’?

Flaky tests are tests that produce inconsistent results, sometimes passing and sometimes failing without any changes in the codebase. They can be caused by timing issues, environmental dependencies, or unreliable locators. 

To fix flaky tests:
  • Add appropriate waits to handle timing issues.
  • Ensure test environments are stable and consistent.
  • Use more reliable locators and checking for element visibility or readiness.
  • Creating independent test to ensure no impact of a test on other
  • Running tests multiple times to identify and address flaky behavior.

Saturday, July 20, 2024

Understanding Page Object Model using workflows and examples

Page Object Model: Overview and Benefits

Page Object Model (POM) is a design pattern used in Selenium automation testing to enhance test maintenance and reduce code duplication.

Key concepts of Page Object Model:

Code Reusability:  Page Objects separate the test logic from the UI logic, promoting reuse of code across multiple tests.

Code Maintainability: Changes to UI elements identifiers are updated within the Page Object resulting minimal impact on test classes.

Improves Test Structure: Tests become more readable and easier to maintain as they interact with methods exposed by Page Objects rather than dealing with raw Selenium commands.

Reduces Duplication: Common interactions with elements (like clicking a button, entering text, etc.) are encapsulated within methods of Page Objects, eliminating redundant code in tests.

Scalability: Supports scaling of automation efforts by providing a structured approach to managing page interactions.


Page Object Model - Workflow



Components of Page Object Model (POM)

Page Object Model


Page Objects 

  • Classes that encapsulate the behavior and structure of a web page.
  • Each page in the application under test has its own corresponding Page Object class
  • Page Objects interact with the page elements (like button, input) and provide methods to perform actions on these elements
  • A Page class has:
    • Identifiers of elements in the Page.
    • methods to work with the identified elements in the page

Page Object Factory

  • Page Factory is a concept in POM that initializes elements of a Page Object.
  • It uses annotations (@FindBy) to locate elements on the web page.
  • These annotations are used in Page Object classes to find and initialize WebElement objects.

BasePage Class

  • This class serves as a foundational class that other Page Objects inherit from. 
  • Examples of common functionality that are shared across multiple Page Objects and should be defined in BasePage class includes:
    • WebDriver Initialization - Initialize the WebDriver instance in the base Class, so that it is used in the page Objects
    • Common Page Actions - Define reusable methods for common actions that occur across multiple pages.
    • Common Page Interactions: methods for handling common interactions  across different pages like handling alerts, executing JavaScript.

    • Wait Mechanisms: Implement methods for waiting and waiting strategies to be used across pages

Any additional method that might be beneficial across multiple Page Objects.

Relation between Test Class and Page Class



Base Class with logic for common implementations


Code example base class


Creation of Page class extending the Base class




Implementation of the Page Class in Test class




Monday, August 22, 2016

Tips for xpath identifcation for selenium

  • Importance of XPath in Selenium?

Selenium WebDriver identifies element based on locators. XPath is one of the most important locator used for identifying an element or group of elements. XPath uses path expressions to work with element in the application.

  •  Tools for identifying element using Xpath?

An element attribute can be identified by accessing developer toolbar (pressing F12 in browser windows) by in each of the browser.  There are add-ons/extensions which can be added in the browser.  One of the useful extensions for Firefox browser is downloading firebug followed by firepath and identifying elements using Xpath.



firepath for xpath

  • Xpath for the element in case the Id of the element is available
Xpath = “.//*[@id='gb_testing123']”

This will search for any element with id as 'gb_testing123'. In case there are various element in 
the page with id dynamically changing on each page load ,e.g: Id changes from gb_testing123 to 
gb_testing224. In such cases , we can have expression as :

Xpath = “.//*[contains(@id,'gb_testing')]” 

There may be multiple elements of different element type, Suppose there are different element type
and we want to restrict our xpath to a button element, we can modify the xpath in above expression 
as:

Xpath = “.//button[contains(@id,'gb_testing')]”

  • Xpath for the element in case the class of the element is available. 
Xpath = “//*[@class='gb_testing123']”
Or 
Xpath = “.//*[contains(@class,'gb_testing')]” 
Or 
Xpath = “.//div[contains(@class,'gb_testing')]”

  • Xpath for element in case of attribute value is available 
Xpath = “//*[@activated='1']”
Or
Xpath = “.//*[contains(@activated,'1')]” 
Or  
Xpath = “.//div[contains(@activated,'1')]”

  • Starts with Prefix for attribute value

//div[starts-with(@class,"gb_")]


  • Based on text displayed of the element

//*[text()='Software Testing Tutorial']


  •  Identifying element based on multiple attribute value


a. Xpath for element using combination of different attribute in AND condition
.//div[contains(@id,'hdtb')][@class='hdtbna notl']
Or
.//div[contains(@id,'hdtb')  and @class='hdtbna notl']


b. Xpath for element using combination of different attribute in OR condition
.//*[contains(@id,'hdtb') or @class='hdtbna notl']


  • Identifying xpath using different element types matching either A or B.


//a[contains(@id,'hdtb')]|//div[@class='hdtbna notl']

This will return element which are either links containing id as hdtb or 
div objects with class as 'hdtbna notl'


  • Child element of object type


//div[@id='viewport']/div
 This will return the immediate div elements in the div with id as viewport
//div[@id='viewport']//div
 This will return all the  div elements inside the div with id as viewport
//div[@id='viewport']/div[5]
 This will return the fifth div element immediate child inside the div with id as viewport


  • Parent element


//div[@class='rc']/.. 
 This will return the parent element
//div[@class=’rc’]/../a 
 Sibling link element to the div with class as rc.

Saturday, March 12, 2016

Page Object Model pattern in Selenium?

Page Object Model in selenium is a design Pattern (Design Pattern is a general reusable solution(best practices) to a commonly occurring problem within the context in software design) implemented using selenium Web Driver. Page Object Model is used to create object repository for elements in the web application used in test flows.


Page Object Model Pattern serves following purposes:


  • Segregating objects definition from test scripts in separate classes making test scripts independent of page elements and locators, thus reducing the maintenance cost in case of changes in elements locator in the Application under test.
  • Defining Page elements and page methods in Page classes help in re-usability of the page elements and methods in different tests reduces the line of code and makes it more maintainable as changes in application needs to be implemented at Page classes only.
  • Make code more readable in the test scripts.
  • Easier for testers with less knowledge of selenium/Java to create test scripts once the Page classes are prepared.

Page Object Model - Do not see too seriously

Understanding how to automate a web application using selenium Web Driver using Page Object Model design pattern


Let us try to understand how Page object model pattern is implemented with an example. 


Suppose there are number of pages in an Web Application and within each page there are multiple elements in each page. Let us assume linear approach for creating automated test cases. Suppose there are ~100 test scripts(workflows) to test the application. Each of the workflow starts with login into application. In each of the test cases, we would have defined the locators for input fields for username and password and login button elements and interact(perform action) with the element. 


Creating test using above approach will result in following problems:
  • In case of changes in an element locator, changes needs to be implemented at multiple tests
  • Readability of test will be poor due to locators defined at test level. 
  • Lines of code will be higher and approach for creating test scripts will vary from one test developer to another.
  • Requires script developer to have good selenium knowledge for test scripts creation. 

Object repository structure in QTPA better approach would be to define elements locators at a different location from test scripts. Also methods for working with elements should be defined, separate of the test scripts.


If you had worked with QTP before, the object repository used to be maintained in tree structure. In Page Object model design pattern,Web Pages are represented by classes with elements in the Page defined as members of the class. Actions on the elements are implemented as methods in the class very much similar to QTP approach.



Implementing Page Object Model:


  • Create a class for login Page (similarly create class for different pages).
  • Define the objects locator in the Page as member of the class.
  • In the constructor method for the Page class, load or intialise the elements defined in the Page using Page Object Factory. 
  • Create user defined methods in the class to input data in the input box, clicking the login button, and methods for successful login or failure. 
  • An object of the page class will be created in the test scripts to interact with the objects and methods for Page class.
    Page Object model pattern implementation

I have not added any code snippet in this article, but below reference article have nice examples explaining the code and concept of Page Object model pattern in selenium.


Useful References for code and concept of Page Object Model

Hope you find this post informative or atleast the references useful.