JavaApp自动化测试系列[v1.0.0][Page Object模式]


Page Object模式

在本章节开始前应熟练掌握PO模式的思想,请参考Java自动化测试系列[v1.0.0][PO设计模式]和Java自动化测试系列[v1.0.1][PO设计模式]
Page Object模式是Selenium中的一种设计模式,Appium也继承了这种设计模式

页面对象封装

在项目结构中新增页面元素对象的package,在该package下新增三个页面对象类,分别为HomePage,RegisterPage,RegisterVerifyPage,代码如下

package org.davieyang.pages;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;

public class HomePage {
AndroidDriver<?> driver;
public HomePage(AndroidDriver<?> driver){
this.driver = driver;
}

private static By startRegister_btn = By.id("startUserRegistration");
public RegisterPage navigate_register_page(){
driver.findElement(startRegister_btn).click();
// 返回RegisterPage页面对象
return new RegisterPage(driver);
}
}

package org.davieyang.pages;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;

public class RegisterPage {
AndroidDriver<?> driver;
public RegisterPage(AndroidDriver<?> driver){
this.driver = driver;
}

public static By username_txt = By.id("inputUsername");
public static By email_txt = By.id("inputEmail");
public static By password_txt = By.id("inputPassword");
public static By name_txt = By.id("inputName");
public static By language_sel = By.id("input_preferedProgrammingLanguage");
public static By prgLanguage = By.name("Scala");
public static By accept_check = By.id("input_adds");
public static By register_btn = By.id("btnRegisterUser");

public RegisterVerifyPage register_sucess(String username, String email, String password, String name){
driver.findElement(username_txt).sendKeys(username);
driver.findElement(email_txt).sendKeys(email);
driver.findElement(password_txt).sendKeys(password);
driver.findElement(name_txt).clear();
driver.findElement(language_sel).sendKeys(name);
driver.findElement(prgLanguage).click();
driver.findElement(accept_check).click();
driver.findElement(register_btn).click();
// 返回RegisterVerifyPage页面对象
return new RegisterVerifyPage(driver);
}

}

package org.davieyang.pages;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;

public class RegisterVerifyPage {
AndroidDriver<?> driver;
public RegisterVerifyPage(AndroidDriver<?> driver){
this.driver = driver;
}

public static By label_name_data = By.id("label_name_data");
public String get_name_value(){
return driver.findElement(label_name_data).getText().toString();
}
}


PO模式下的测试代码

修改之前的测试代码,如下所示

package org.davieyang.testscripts;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.android.AndroidElement;
import org.davieyang.pages.HomePage;
import org.davieyang.pages.RegisterPage;
import org.davieyang.pages.RegisterVerifyPage;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
public class TestRegister
{
AndroidDriver<AndroidElement> driver;

@BeforeMethod
public void setUp() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
// File appDir = new File("E:\\");
// File app = new File(appDir, "selendroid-test-app-0.17.0.apk");
capabilities.setCapability("deviceName", "Redmi 3");
capabilities.setCapability("udid", "claeae297d72");
capabilities.setCapability("platformVersion", "5.1.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", "app.getAbsolutePath()");
// capabilities.setCapability("automationName", AutomationName.APPIUM);
capabilities.setCapability("appPackage", "io.selendroid.testapp");
capabilities.setCapability("appActivity", ".HomeScreenActivity");
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("noSign", "True");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test_Register_success(){
HomePage homePage = new HomePage(driver);
RegisterPage registerPage = homePage.navigate_register_page();
RegisterVerifyPage registerVerifyPage = registerPage.register_sucess("davieyang",
"davieyang@qq.com","111111", "Daveiyang.D.Y");
Assert.assertEquals(registerVerifyPage.get_name_value(), "Daveiyang.D.Y");

}
@AfterMethod
public void tearDown(){
driver.quit();
}
}

原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。

专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说

JavaApp自动化测试系列[v1.0.0][Page Object模式]

免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。

您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源
www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com
未经允许不得转载:书荒源码源码网每日更新网站源码模板! » JavaApp自动化测试系列[v1.0.0][Page Object模式]
关注我们小说电影免费看
关注我们,获取更多的全网素材资源,有趣有料!
120000+人已关注
分享到:
赞(0) 打赏

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

您的打赏就是我分享的动力!

支付宝扫一扫打赏

微信扫一扫打赏