Using Page Object Model With Appium And Java
Page Object Model is undoubtedly one of the best routes to go if you want to create manageable and reusable tests, avoid unneeded code duplication, and make your testing life a lot simpler! Before heading into it, let’s understand what is Appium and Page Object Model.
Appium
In order to simplify UI automation of multiple app platforms, including mobile (iOS, Android, Tizen), browser (Chrome, Firefox, Safari), desktop (MacOS, Windows), TV (Roku, tvOS, Android TV, Samsung), and more, Appium is an open-source project and ecosystem of related applications.
Appium is mostly used in the realm of software test automation to assist assess whether a given app's functionality is operating as intended. Unlike other forms of software testing, UI automation enables testers to create code that iteratively runs through user scenarios in an application's real user interface (UI), as closely as possible simulating real-world behavior while maximizing the speed, scale, and consistency advantages of automation.
Have a look at its official documentation to find out more about how Appium achieves this objective and the many elements needed.
Page Object Model
Within the test code, a Page Object simply represents these as objects. As a result, there is less redundant code, and if the user interface changes, the patch only needs to be implemented once.
Because it improves test maintenance and minimizes code duplication, the Page Object Design Pattern has gained popularity in test automation. An object-oriented class known as a page object acts as an interface for a page of your AUT. When the tests need to interact with the UI of that page, they use the methods of this page object class. The advantage is that only the code included within the page object needs to update if the user interface (UI) for the page changes.
This strategy suffers from two issues.
With Page Object Model V/s Without Page Object Model
The difference between using Appium with the Page Object Model (POM) and without POM lies in the organization and maintainability of your test automation code.
Using Appium without POM:
Using Appium with POM:
Recommended by LinkedIn
Assumptions
Implementing Page Object Model in Appium
The Page Object Model (POM) is a popular design pattern for automating tests on mobile apps using Appium and Java. It helps to organize your code and make it more maintainable. Here's how you can implement the POM with Appium and Java for mobile app test automation:
Here's an example of how your code might look like:
To configure the pom.xml file for using Appium with the appropriate drivers, you'll need to add the necessary dependencies and plugins. Here's an example of what the pom.xml file could look like:
<properties>
<appium.version>7.3.0</appium.version> <testng.version>7.8.0</testng.version>
</properties>
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId> <version>${appium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version>
</dependency>
<!-- Other dependencies you might need (e.g., Selenium) --> </dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
In this example, we have added the Appium Java Client dependency with version 7.5.1 to the dependencies section. You may also need to add other dependencies if your project requires them.
// LoginPage.java
public class LoginPage {
@AndroidFindBy(id = "usernameField")
private WebElement usernameField;
@AndroidFindBy(id = "passwordField")
private WebElement passwordField;
@AndroidFindBy(id = "loginButton")
private WebElement loginButton;
public LoginPage() {
PageFactory.initElements(new AppiumFieldDecorator(driver), this); }
public void enterUsername(String username) {
usernameField.sendKeys(username);
}
public void enterPassword(String password) {
passwordField.sendKeys(password);
}
public void clickLoginButton() {
loginButton.click();
}
}
// TestLogin.java
public class TestLogin {
private LoginPage loginPage;
@Test
public void testLogin()
{
loginPage = new LoginPage(); loginPage.enterUsername("myusername"); loginPage.enterPassword("mypassword"); loginPage.clickLoginButton();
// Add assertions or verifications here to validate the login functionality
}
}
This is just a simple example, and you can extend it as needed for your specific mobile app. Remember to update the @AndroidFindBy annotations with the appropriate locators for your elements.
I hope this helps you get started with using the Page Object Model for mobile app test automation with Appium and Java.
Thank you!!
Knoldus Inc | part of NashTech NashTech Learning Express