# Mobile Testing Tutorial Part 2: Page Objects
# Introduction
Very often we'll interact with the same controls on a page several times. If there's a small change in the page, you might have to update lots of different tests. The solution is to use Page Objects. In this tutorial we'll move the functionality from the check expense Test Case to a page object so that it can be reused easily.
# Prerequisites
- Complete part 1 of the Mobile Testing Tutorial
- Start Appium
- Start automatic compilation.
npm run compile -- -w
# Creating a New Page Object
- Right click on the
src
folder in the project root and selectNew Folder
. Name the folderpages
- Right click on the
pages
folder selectNew Gondola Page
, GTD will open.
# Starting TestBuilder
- Start your emulator or connect your device.
# Adding a method
- In the section
Methods
you should see blank method, enteraddExpense
in the name field (no spaces),add expense
in the keyword field, andadd an expense
in the description. - Start a new TestBuilder session by pressing the
TestBuilder icon. After starting TestBuilder the Create new session dialog will open:
- Select
gondola.android.json
as the capabilities file.
- Select
- In the Device Live Screen, right click on the
+
button. When the menu pops up, enteraddExpenseBtn
as the name and selectwait for element
as the action. Enter10
as the number of seconds to wait.
TIP
If you see a button that says Add Locator
, press the switch button to get the action menu.
You should see a new locator named addExpenseBtn
in the locators section, and an action line with the wait for element
action.
- Right click the
addExpenseBtn
again, and select the tap action. - Right click the
Description
field, enterdescription
as the name and selectwait for element
as the action. Enter10
as the number of seconds to wait. - Right click the
Description
field again, notice Gondola detects that you've already saved this locator, so you won't need to fill out the name field again. Selectenter
as the action andCoffee
as the text to enter. - Now do the same for the other fields
Field names:
- Date:
date
,2020-03-27
- Amount:
amount
,3.00
- Tap the "Add Expense" button and use
saveExpenseBtn
for its name (we already have a button with the nameaddExpenseBtn
)
When you've finished the Page Object should look like this:
# Using the method in your test case
- Open up the test from the previous tutorial
- Go the add expense test case, and delete all the rows
- Now in a blank row, type add expense. Auto-complete will assist you.
- Save and run the test
# Modifying the method for variable data
The add expense method above works fine, but what if we want to use it several times with different values for the description, date, and price? For that we can add arguments to the method.
- In a blank line above the
wait for element addExpenseBtn
action typeargument
. In the name field enterdescriptionTxt
. In the type field enterstring
and in the default value field entercoffee
. - Repeat this for the
Date
andPrice
fields - Then, go to the
enter
action for$description
, change the value to$descriptionTxt
, repeat this for all fields
# Using the modified method in your Test Case
- Go back to your Test Case and and delete the line with
add expense
, then add it again. This time you will see fields fordescriptionTxt
,dateTxt
, andpriceTxt
- Create 3 new rows and the use the
add expense
, add different data such ascoffee
,couch
,movie tickets
for the description fields and run your test again.