In this post we will discuss how to create and work with Page Object Model (POM) in Appium. We have already discuss page POM in greater detail while discussion Selenium framework design and development articles and videos, hence we will change our gears here to focus directly on the coding part of Page Object Model in appium rather talking about its theories.
Here is the complete code for Page Object Model (POM) in appium
Code for Page
public class CalcAppPage {
public CalcAppPage(AppiumDriver driver) {
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
@AndroidFindBy(id = "edtno1")
public WebElement txtNum1;
@AndroidFindBy(id = "edtno2")
public WebElement txtNum2;
@AndroidFindBy(id = "txtResult")
public WebElement txtResult;
public void Add(String num1, String num2) {
txtNum1.sendKeys(num1);
txtNum2.sendKeys(num2);
}
public boolean VerifyResult(String result) {
if (txtResult.getText().equals(result))
return true;
else
return false;
}
}
Code for Test
@Test
public void SimpleTest() {
CalcAppPage calPage = new CalcAppPage(driver);
calPage.Add("20", "30");
if (calPage.VerifyResult("50"))
System.out.println("PASSED Test");
else
System.out.println("FAILED Test");
}
Here is the complete video of the above discussion
Thanks for reading the post and watching the video !!!
Please leave your comments and let me know if there is anything I should update in this post.
Thanks,
Karthik KK
Post Author:
Karthik kk
9 Replies to “Page Object model with Appium”
Nice and very helpful tutorials… Please add some more videos for automation selenium code on Android and iOS native apps using appium together.
Thank you, POM seems an interesting framework.
Too bad we can’t use variables in AndroidFindBy with xpath to have dynamics object.
Like getting the x textfield, or to get the textfield with the text “xxx”. Constants only are allowed.
Hi,
I have implement POM with Appium in java. My code structure follows as below :
page object classes : LoginPageObjectRepo() and HomePageObjectRepo()
testng classes : RegisterAndLoginTCs() and HomePageTCs()
RegisterAndLoginTCs() :
@BeforeTest
public void setup() throws Exception{
//initial setup and capabilities
}
@AfterTest
public void stopServer() throws IOException{
//code
}
@Test
public void TC1_login() throws Exception{
LoginPageObjectRepo objDriver = new LoginPageObjectRepo(driver);
objDriver.Login1().click();
objDriver.Emaild().sendKeys(“rspinnov.qm@gmail.com”);
objDriver.Password().sendKeys(“abctesting”);
objDriver.Login2().click();
WebDriverWait wait = new WebDriverWait(driver,300);
try{
assertNotNull(wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath(“//*[@text=’Login successful’]”))));
log.info(“User log in successfull”);
}catch(Exception e){
log.error(“User log in failed”);
}
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(“com.ptmobile.catalogapp:id/ab_custom_search”)));
}
//other many test methods
*************************************************************************************************
HomePageTCs :
AndroidDriver driver;
@Test
public void TC5_HomePageFormat() throws Exception{
HomePageObjectRepo objdriver = new HomePageObjectRepo(driver);
objdriver.SearchOption().click();
}
Now when i run this project i get nullpointer exception at HomePageTCs.TC5_HomePageFormat(HomePageTCs.java:25) ie. at line objdriver.SearchOption().click();
I feel it is because driver is defined in RegisterAndLoginTCs class.
How can i get access to same driver which is defined in setup() method so that once driver is defined with required capabilities, it can be used across multiple testng classes.
Nice and very helpful tutorials… Please add some more videos for automation selenium code on Android and iOS native apps using appium together.
Thanks a lot Karthik…
Very Helpful. Thanks a lot. Can you please share some more videos considering complex android native / hybrid applications.
Hi Karthik,
Nice tutorial.
Can you please post the framework diagram of the approach.
Thanks
Thank you, POM seems an interesting framework.
Too bad we can’t use variables in AndroidFindBy with xpath to have dynamics object.
Like getting the x textfield, or to get the textfield with the text “xxx”. Constants only are allowed.
Very nice explanation , could you please tel me how to write POM for dynamic objects/elements
Hi Rashmi,
You should probably look @this course https://www.udemy.com/framework-development-with-selenium-csharp-advanced/ for the answer of your question.
Thanks,
Karthik KK
Hi,
I have implement POM with Appium in java. My code structure follows as below :
page object classes : LoginPageObjectRepo() and HomePageObjectRepo()
testng classes : RegisterAndLoginTCs() and HomePageTCs()
RegisterAndLoginTCs() :
@BeforeTest
public void setup() throws Exception{
//initial setup and capabilities
}
@AfterTest
public void stopServer() throws IOException{
//code
}
@Test
public void TC1_login() throws Exception{
LoginPageObjectRepo objDriver = new LoginPageObjectRepo(driver);
objDriver.Login1().click();
objDriver.Emaild().sendKeys(“rspinnov.qm@gmail.com”);
objDriver.Password().sendKeys(“abctesting”);
objDriver.Login2().click();
WebDriverWait wait = new WebDriverWait(driver,300);
try{
assertNotNull(wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath(“//*[@text=’Login successful’]”))));
log.info(“User log in successfull”);
}catch(Exception e){
log.error(“User log in failed”);
}
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(“com.ptmobile.catalogapp:id/ab_custom_search”)));
}
//other many test methods
*************************************************************************************************
HomePageTCs :
AndroidDriver driver;
@Test
public void TC5_HomePageFormat() throws Exception{
HomePageObjectRepo objdriver = new HomePageObjectRepo(driver);
objdriver.SearchOption().click();
}
**********************************************************************************************
Now when i run this project i get nullpointer exception at HomePageTCs.TC5_HomePageFormat(HomePageTCs.java:25) ie. at line objdriver.SearchOption().click();
I feel it is because driver is defined in RegisterAndLoginTCs class.
How can i get access to same driver which is defined in setup() method so that once driver is defined with required capabilities, it can be used across multiple testng classes.
I am also facing same problem.
Isn’t it a Page Factory what you have explained?