Skip to content

Instantly share code, notes, and snippets.

@dino-su
Last active August 24, 2021 10:56
Show Gist options
  • Save dino-su/24df0a290daaefaa6f14 to your computer and use it in GitHub Desktop.
Save dino-su/24df0a290daaefaa6f14 to your computer and use it in GitHub Desktop.
Android UiAutomator in Page Object Pattern
package com.uia.example.my;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class AddPage extends UiAutomatorTestCase {
private final UiDevice device;
// locators
UiScrollable contactView = new UiScrollable(new UiSelector()
.className("android.widget.ScrollView"));
UiSelector nameLocator = new UiSelector().className("android.widget.EditText");
UiSelector photoLocator = new UiSelector().className("android.view.View");
UiSelector phoneLocator = new UiSelector().className("android.widget.EditText");
UiSelector emailLocator = new UiSelector().className("android.widget.EditText");
UiSelector addressLocator = new UiSelector().className("android.widget.EditText");
public AddPage(UiDevice device) throws UiObjectNotFoundException {
this.device = device;
// ensure we're in the right page
UiObject photo = contactView.getChildByDescription(photoLocator, "contact photo");
if(!photo.exists()) {
fail();
}
}
public HomePage create(String name, String phone, String email, String address) throws UiObjectNotFoundException {
typeName(name);
typePhone(phone);
typeEmail(email);
typeAddress(address);
return new HomePage(device);
}
private AddPage typeName(String name) throws UiObjectNotFoundException {
UiObject nameField = contactView.getChildByText(nameLocator, "Name");
nameField.setText(name);
return this;
}
private AddPage typePhone(String phone) throws UiObjectNotFoundException {
UiObject phoneField = contactView.getChildByText(phoneLocator, "Phone");
phoneField.setText(phone);
return this;
}
private AddPage typeEmail(String email) throws UiObjectNotFoundException {
UiObject emailField = contactView.getChildByText(emailLocator, "Email");
emailField.setText(email);
return this;
}
private AddPage typeAddress(String address) throws UiObjectNotFoundException {
UiObject addressField = contactView.getChildByText(addressLocator, "Address");
addressField.setText(address);
return this;
}
}
package com.uia.example.my;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class HomePage extends UiAutomatorTestCase {
private final UiDevice device;
// locators
UiScrollable pageView = new UiScrollable(new UiSelector()
.className("android.widget.ListView"));
UiSelector contactLocator = new UiSelector().className("android.widget.TextView");
public HomePage(UiDevice device) throws UiObjectNotFoundException {
this.device = device;
// ensure we're in the right page
if(device.getCurrentPackageName() != "com.android.contacts") {
device.pressHome();
UiObject pplButton = new UiObject(new UiSelector().text("People"));
pplButton.clickAndWaitForNewWindow();
}
}
public AddPage clickCreateContact() throws UiObjectNotFoundException {
UiObject addButton = new UiObject(new UiSelector().text("Create a new contact"));
if(!addButton.click()) {
fail();
}
return new AddPage(device);
}
public boolean exists(String name) throws UiObjectNotFoundException {
UiObject contact = pageView.getChildByText(contactLocator, name);
return contact.exists();
}
}
package com.uia.example.my;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class TestPeople extends UiAutomatorTestCase {
public void testCreateContact() throws UiObjectNotFoundException {
HomePage homePage;
AddPage addPage;
homePage = new HomePage(getUiDevice());
addPage = homePage.clickCreateContact();
homePage = addPage.create("Dino Su", "+8860000000000", "chinglong.su@gmail.com", "My address");
assertTrue(homePage.exists("Dino Su"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment