Page Object Model and Page Factory in Appium
On This Page Introduction to Page Object ModelIntroduction t
Page Object Model and Page Factory in Appium
In mobile trial automation, structure code for scalability and easy upkeep is as important as running the tests themselves. This is where thePage Object Model (POM) and Page Factoryarrive into play. When applied withAppium, these plan patterns simplify test automation by separating examination scripts from UI locators, reducing redundance, and making frameworks more long-lasting.
Overview
What is Page Object Model (POM) in Appium?
- Design patternthat separates screen scripts from UI locators.
- Each page has its ownpage classcontaining elements and methods.
- Benefits include:
- Cleaner test code and easier alimony.
- Single secretary for all page-level action.
- High reusability and legibility in multi-page mobile apps.
What is Page Factory in Appium?
- An optimized implementation of POM.
- Uses @ FindBy annotationsto initialize elements dynamically.
- Supports AjaxElementLocatorFactoryfor lazy loading of WebElements.
- Frequently integrated with model likeBDD, Data-Driven, and Keyword-Driven testing.
How to Implement Page Object Model in Appium
- Setup Desired Capabilities: Define app path, device, OS version, and BrowserStack credentials.
- Driver Class: Instantiate Appium driver objects for Android or iOS.
- Page Classes: Example →LoginPageclass with fields (username, word, login push) and methods (validate login, exam invalid certification).
- Test Classes: Call page methods (e.g.,LoginTests) to validate login flows.
- Update only thepage coursewhen UI changes, rather of rewriting all test cases.
This guide explores how POM and Page Factory employment in Appium, their advantages, and how to implement them effectively for racy tryout automation.
Introduction to Page Object Model
Page Object is a design pattern that has become democratic in trial automation for enhancing test maintenance and reducing code duplicate.
In this case, the tests use the page object class methods to interact with the UI of the page. If the page UI changes, then the tests involve not be modify, only the code within the page object want to be alter. All the changes that support the new UI are situate in one place. Hence, locators and test scripts are store separately.
Also Read:
The Page Object Design Pattern ply the following advantages:
- There is a clean separation between test codification and page-specific code such as locator.
- There is a single depository for the services or operations proffer by the page rather than having these services scattered throughout the tests.
- POM is best applicable for applications that incorporate multiple pages. Each of which has field that can be uniquely reference with esteem to the page.
- The Page Object approach makes the mechanisation framework in a testing & # 8211; programmer friendly, more durable, and comprehensive.
Introduction to Page Factory
Page Factory is an inbuilt and optimized concept of POM (Page Object Model). Page Factory is used in many frameworks like Data Driven, Behavior Driven, Keyword Driven, etc. By integrating POM and Page Factory with the Test Case Model, you focus more on how the code is structure to get the most benefits.
Page Factory pioneer the elements to make an object secretary with note@FindBy.It is an easy way of loading WebElements withAjaxElementLocatorFactory, when they are used in any operation.
Page Object Model Implementation with Appium
Let ’ s now realise how to apply the Page Object Model with Appium.
Prerequisites:You should cognise how to execute test cases using Appium Framework. Refer to the guide on.
Dependencies:
// Maven users can add this dependence to jut & # 8217; s POM
& lt; colony & gt;
& lt; groupId & gt; io.appium & lt; /groupId & gt;
& lt; artifactId & gt; java-client & lt; /artifactId & gt;
& lt; version & gt; 7.0.0 & lt; /version & gt;
& lt; /dependency & gt;
Core Class for Desired Capabilities-related setup
This class we can use to pen core setup-related code, which is common for executing each Appium test lawsuit.
parcel android;
importation java.net.URL;
importee java.util.List;
import java.util.function.Function;
import java.net.MalformedURLException;
importation io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class BrowserStackExample {
public static vacuum main (String [] args) throws MalformedURLException, InterruptedException {
For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.
DesiredCapabilities crest = new DesiredCapabilities ();
// Set your accession credentials
caps.setCapability (& # 8220; browserstack.user & # 8221;, & # 8220; your_username & # 8221;);
caps.setCapability (& # 8220; browserstack.key & # 8221;, & # 8220; your_password & # 8221;);
// Set URL of the application under test
caps.setCapability (& # 8220; app & # 8221;, & # 8220; bs: //app_url & # 8220;);
// Specify device and os_version for testing
caps.setCapability (& # 8220; device & # 8221;, & # 8220; Google Pixel 3 & # 8221;);
caps.setCapability (& # 8220; os_version & # 8221;, & # 8220; 9.0 & # 8221;);
// Set other BrowserStack capabilities
caps.setCapability (& # 8220; labor & # 8221;, & # 8221; AppiumProject & # 8221;);
caps.setCapability (& # 8220; make & # 8221;, & # 8220; browserstack-build-1 & # 8221;);
caps.setCapability (& # 8220; name & # 8221;, & # 8220; test_build & # 8221;);
assert (allProductsName.size () & gt; 0);
driver.quit ();
}
}
Driver Class
This class instantiate the driver target.
import io.appium.java_client.android.AndroidDriver;
public grade Driver extends DriverclassExample {
protected AndroidDriver driver;
public Driver () {
this.driver = super.getDriver ();
}
}
Once you have desired capabilities and the Driver class ready, the adjacent stride is creating a page class.
Page Class for Login Page
This is how a page class seem like.
signification org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
public form LoginPage cover Driver {
PageObjects loginPage;
String userName = & # 8220; & # 8221;;
String passWord = & # 8220; & # 8221;;
public LoginPage () {
super();
loginPage = new PageObjects ();
PageFactory.initElements (driver, loginPage);
}
public boolean validateLoginpage () {
boolean elements = false;
if (loginPage.userNameFld.isDisplayed ()) {
if (loginPage.passwordField.isDisplayed ()) {
if (loginPage.checkBox.isDisplayed ()) {
if (loginPage.loginBtn.isDisplayed ()) {
elements = true;
}
}
}
}
else{
elements = false;
}
homecoming elements;
}
public boolean testLoginWithoutCredentials () {
boolean loginStatus = false;
loginPage.loginBtn.click ();
if (loginPage.inputError.getText () .equalsIgnoreCase (& # 8220; Username is compulsory & # 8221;)) {
loginStatus = true;
}
loginPage.userNameFld.sendKeys (userName);
loginPage.loginBtn.click ();
if (loginPage.inputError.getText () .equalsIgnoreCase (& # 8220; Password is mandatory & # 8221;)) {
loginStatus = true;
}
return loginStatus;
}
class PageObjects {
@ CacheLookup
@ FindBy (id = & # 8220; login_username & # 8221;)
public WebElement userNameFld;
@ CacheLookup
@ FindBy (id = & # 8220; login_password & # 8221;)
public WebElement passwordField;
@ CacheLookup
@ FindBy (id = & # 8220; btnSignin & # 8221;)
public WebElement loginBtn;
@ CacheLookup
@ FindBy (name = & # 8220; Invalid ID or password. & # 8221;
public WebElement inputError;
@ CacheLookup
@ FindBy (id = & # 8220; checkBox & # 8221;)
public WebElement checkBox;
}
}
Test Case class for writing login page test event
significance org.junit.Test;
public course LoginTests {
@Test
public void testLogin () {
LoginPage loginPage = new LoginPage ();
if (loginPage.validateLoginpage () == true) {
loginPage.testLoginWithoutCredentials ();
System.out.println (& # 8220; passing & # 8221;);
} else {
System.out.println (& # 8220; Validation failed & # 8221;);
}
}
}
As you can see in the above example, we have created different classes for Page, login and test form. When any change is made to the page target, the entire code need not be changed. You can just modify the page aim and re-run the implementation. That ’ s how it works.
To do extensive cross-platform testing for roving apps, you can use cloud-based tools like which countenance you to run Appium tests on 3000+ real devices. Its comprehensive and reliable platform allows QAs to access real device with real operating systems in order to run automated tests via Appium.
To keep up with the requirement of fast and reliable examination that spans over various platforms, devices, and version, Appium mobile testing is essential. Preferred throughout the industry, Appium offers feasibility, flexibility, and cost-friendliness above early try method, enable teams to render great user experience within the continuous speech approach.
# Ask-and-Contributeabout this topic with our Discord community.
Related Guides
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free