Skip to content

Commit

Permalink
Merge pull request #1 from osandadeshan/page-factory-pattern
Browse files Browse the repository at this point in the history
Added page factory design pattern.
  • Loading branch information
osandadeshan committed Oct 11, 2020
2 parents 6909e4e + a4451ea commit 991c184
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 0 deletions.
25 changes: 25 additions & 0 deletions page-factory-pattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Page Factory Design Pattern

## About Page Factory Design Pattern

Page-Factory allows you to write autotests in a human-readable language, thereby lowering the entry threshold for test developers and increasing their readability by untrained users. Page-factory uses the Cucumber-JVM framework, but unlike pure use, in which a fairly large part of the architecture is occupied by steps (stepdefs) , here the emphasis is on getting rid of the need to write them yourself, or reducing the number of self-written steps (stepdefs) focusing on describing the code of pages using the PageObject pattern .
Page-Factory has already implemented many standard steps , which are enough to start developing automated tests.
Page-Factory is a cross-platform framework that allows you to run tests on all popular browsers, because Selenium WebDriver is used to run them. Page-Factory can also work with an Android application using Appium.

## Requirements

To work Page-Factory you need:
1. Java 8 or higher


## Applicability

Use the Page Factory pattern when,

* You are writing automated tests for your web application and you want to separate the UI manipulation required for the tests from the actual test logic.
* Make your tests less brittle, and more readable and robust

## Credits

* [Martin Fowler - PageObject](http://martinfowler.com/bliki/PageObject.html)
* [Selenium - Page Objects](https://github.com/SeleniumHQ/selenium/wiki/PageObjects)
89 changes: 89 additions & 0 deletions page-factory-pattern/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.osanda.javadesignpatterns</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.osanda.javadesignpatterns</groupId>
<artifactId>page-factory-pattern</artifactId>
<version>1.0-SNAPSHOT</version>
<name>page-factory-pattern</name>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.osanda.pagefactorypattern.page;

import org.openqa.selenium.WebDriver;

public class CommonPage {

private final WebDriver driver;

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

/**
* Defining all the user actions (Methods) that can be performed in the all pages
*/
// This method to get the tab title of the current browser tab
public String getBrowserTabTitle() {
return driver.getTitle();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.osanda.pagefactorypattern.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class HomePage {

private final WebDriver driver;
/**
* Using FindBy for locating elements
*/
@FindBy(how = How.XPATH, using = "//a[@class='account']/span")
private WebElement profileNameLabel;
@FindBy(how = How.XPATH, using = "//a[@class='logout']")
private WebElement logoutLink;

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

/**
* Defining all the user actions (Methods) that can be performed in the home page
*/
// This method to get the username of the logged-in user
public String getLoggedInUsername() {
return profileNameLabel.getText();
}

// This method to click on Logout link
public void clickOnLogoutLink() {
logoutLink.click();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.osanda.pagefactorypattern.page;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class LoginPage {

private final WebDriver driver;
/**
* Using FindBy for locating elements
*/
@FindBy(how = How.ID, using = "email")
private WebElement emailTextBox;
@FindBy(how = How.ID, using = "passwd")
private WebElement passwordTextBox;
@FindBy(how = How.XPATH, using = "//p[@class='submit']//span[1]")
private WebElement signInButton;

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

/**
* Defining all the user actions (Methods) that can be performed in the login page
*/
// This method is to set Email in the email text box
public void setEmail(String email) {
emailTextBox.sendKeys(email);
}

// This method is to set Password in the password text box
public void setPassword(String password) {
passwordTextBox.sendKeys(password);
}

// This method is to click on Sign In Button
public void clickOnSignInButton() {
signInButton.click();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.osanda.pagefactorypattern.util.driver;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverFactory {

// Get a new WebDriver Instance.
public static WebDriver getDriver() {
WebDriverManager.chromedriver().setup();
return new ChromeDriver();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.osanda.pagefactorypattern.test.base;

import com.osanda.pagefactorypattern.util.driver.DriverFactory;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class TestBase {

protected static WebDriver driver;

// Initialize a driver instance of required browser
@BeforeMethod
public static void initializeDriver() {
driver = DriverFactory.getDriver();
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
}

// Close all the driver instances
@AfterMethod
public static void closeAllDrivers() {
if (driver != null) {
driver.quit();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.osanda.pagefactorypattern.test.login;

import com.osanda.pagefactorypattern.page.CommonPage;
import com.osanda.pagefactorypattern.page.HomePage;
import com.osanda.pagefactorypattern.page.LoginPage;
import com.osanda.pagefactorypattern.test.base.TestBase;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class LoginTest extends TestBase {

@Test
public void verifyValidUserLogin() {
LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);
HomePage homepage = PageFactory.initElements(driver, HomePage.class);
CommonPage commonPage = PageFactory.initElements(driver, CommonPage.class);

// Verify the login page tab title
assertEquals(commonPage.getBrowserTabTitle(), "Login - My Store");

// Input email address
loginpage.setEmail("osanda@mailinator.com");

// Input password
loginpage.setPassword("1qaz2wsx@");

// Click on SignIn button
loginpage.clickOnSignInButton();

// Verify the my store page tab title
assertEquals(commonPage.getBrowserTabTitle(), "My account - My Store");

// Verify the username of the logged-in user
assertEquals(homepage.getLoggedInUsername(), "Osanda Nimalarathna");

// Click on Logout link
homepage.clickOnLogoutLink();

// Verify the login page tab title
assertEquals(commonPage.getBrowserTabTitle(), "Login - My Store");
}

}
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@
</pluginManagement>
</build>

<modules>
<module>page-factory-pattern</module>
</modules>

</project>

0 comments on commit 991c184

Please sign in to comment.