Showing posts with label Java for Selenium. Show all posts
Showing posts with label Java for Selenium. Show all posts

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




Saturday, March 10, 2018

Java for Selenium - Mindmap Object, Classes and Inheritance

In the above example. We create a class bicycle which is defined by it's state and behavior.
members and methods are defined in the class. We can defined multiple objects of the class using class methods and members. 

Bicycle itself belongs to two wheeler class. Similarly there can be other class for motor bike which can have its own member and methods defined. 

Both bicycle and two wheeler can have specific methods and members defined in the class. 

Bicycle extends two wheeler which in turn extends vehicle, bicycle can use members and methods defined in two wheeler as well as vehicle class.

Class Declaration

class Bicycle extends TwoWheeler {
    // field, method and constructordeclarations
}

class TwoWheeler extends Vehicle {
    // field, method and constructordeclarations
}


Variables in a class:

Fields - Member variables in class
Local variable - defined inside method or block of scope
Parameters - variables in method declaration

Variable declaration : [Modifier] Type variablename

Sunday, February 4, 2018

Adding dependencies to maven for selenium

To add dependencies in maven project for running selenium tests, we can add dependencies under dependencies section as shown below:





1. Go to https://mvnrepository.com

2. Add Below dependencies:

a. TestNG
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.13.1</version>
    <scope>test</scope>
</dependency>

b. Selenium-java
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.8.1</version>
</dependency>

c. Selenium chrome driver
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.8.1</version>
</dependency>

3. All the other dependencies related to selenium can be found at:
http://www.mvnrepository.com/artifact/org.seleniumhq.selenium

4. The dependencies are associated with the tests and downloaded at m2 folder in users folder

Sunday, December 17, 2017

Sending keys in selenium


In Selenium, we are send specific keys from the keyboard like Enter or F2 on WebElement using import org.openqa.selenium.Keys as shown below:


import org.openqa.selenium.Keys

WebElement.sendKeys(Keys.ENTER)

Below is the list of enum constants defined for enum Keys, which we can send on an element in selenium:


ADD 
ALT 
ARROW_DOWN 
ARROW_LEFT 
ARROW_RIGHT 
ARROW_UP 
BACK_SPACE 
CANCEL 
CLEAR 
COMMAND 
CONTROL 
DECIMAL 
DELETE 
DIVIDE 
DOWN 
END 
ENTER 
EQUALS 
ESCAPE 
F1 
F10 
F11 
F12 
F2 
F3 
F4 
F5 
F6 
F7 
F8 
F9 
HELP 
HOME 
INSERT 
LEFT 
LEFT_ALT 
LEFT_CONTROL 
LEFT_SHIFT 
META 
MULTIPLY 
NULL 
NUMPAD0 
NUMPAD1 
NUMPAD2 
NUMPAD3 
NUMPAD4 
NUMPAD5 
NUMPAD6 
NUMPAD7 
NUMPAD8 
NUMPAD9 
PAGE_DOWN 
PAGE_UP 
PAUSE 
RETURN 
RIGHT 
SEMICOLON 
SEPARATOR 
SHIFT 
SPACE 
SUBTRACT 
TAB 
UP 
ZENKAKU_HANKAKU 
For further details, please see the Page: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/Keys.html

Sunday, September 24, 2017

Arrays in Java basics for selenium


1. We can assign value to an element in array as shown below:

testing[3] = 43; or all the elements can be defined in a go like shown below:
int[] testing1= {10,20,30,40,50,60,70,80,90,100};

2. We can extract all the values in an array using for each loop as shown below:

 int[] testing1 = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
 for (int test : testing1) {
     System.out.println(test);
 }

3. The same information can be extracted using for loop can be found as shown below:

for (int i = 0; i < testing1.length; i++) {
     System.out.println(testing1[i]);
 }

4. Using length property, we can find the size of any array


e.g. System.out.println(testing1.length)

4. We can pass an array as a parameter to a method, and also return value from a method:

5. We can sort an array in ascending order using sort();

Arrays.sort(testing1);

6. We can fill values in all the array elements using fill() menthod 
Arrays.fill(testing1, 385);

7. We can compare two arrays using arrays.equals

boolean isArrSame = Arrays.equals(testing1, testing);
System.out.println(isArrSame);