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

PageFactory模式

PageFactory模式的概念和PO类似,或者说是它的一种扩展,通过注解的方式定位元素对象,需要在构建函数里调用PageFactory.initElements(driver,this)来初始化PO对象,修改页面对象类文件,代码示例如下

package org.davix x M u + Aeyang.pages;
import io.appium.java_client.an[ ) 9 J Odrz M )oid.AndroidDA y : H Y N 4 Sriver;
// PageFactory
import org.openqa.selenium.suppo@ 7 J $ | )  4 Zrt.PZ R 5 C wageFactory;s 8 x (
import io.ai ~ } b  uppium.java_clie& I z g v Y dnt.pagefacto@ 6 l @ ^ry.AppiumFieldDecorator;
impor w - 7 + M , B Lt io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefaq ! %ctory.iOSXCUITFindBy;
import io.appium.java_client.MobileElement? 9 N * 0 ` V 8;
public class HomePage_PF {
AndroidDriverh , s T 2<?> driver;
public HomePage_PF(AndroidDriver<?> driver){
thiL 4 C c H H ; e Ms.driver = driver;
PageFactory.initElements(new| # M K @ 0 ~ x X AppiumFieldDecorator(driver), this);
}
// 页面对象时别,进入注册页面
@AndroidFindBy(id="stI P l F Z n Z kartUserRegistrat: E ;ion")
@iOSXCUK / yITFindBy(ix W # ? Td="startUserRegisL L  Z etration")
public Mobilw M + g P MeElement startRegister_btn;
// 单击注册页面按钮,进入注册页面,返回注册页面对象
public RegisterPage_PF naf @ ~ ` E 0 R dvigate_regisb ^ ] g oter_page(){
this.startRegister_btn.click();
return new R) v , h ;egisterPage_PF(driver)k m T f [;
}
}
package org.davieyang.pages;
import java.util.List;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;
imp= 9 9ort org.openqa.selenp L ] I w 6 9ium.support.P~ y _ cageFactory;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appi2 H qum.java_client.pagefactory.AndroidFindBy;
import io.# z x o .appium.java_client.pagefe I U B (actory.iOSXCUITFindBy;
import io.appium.java_client.MobileElement;
public class Register2 } f I g a .Page_PF {
AndroidDk # r 1 R & hriver<?> dr$ H i u C x ; iver;
public RegisterPage_PF(AndroidDriver<?> driver){
this.driver = driver;
PageFacj 1 K M I a t n Btory.initElemen. 2 } Z L y s 6 tts(new AppiumFieldDecorator(driver), this);
}
// 页面对象时别
// 用户名
@AndroidFindBy(id="inputUsername")
@iOSXCUITFinj k *dBy(id="inputUsername")
public MobileElement username_txt;
// 邮箱
@] A h G f } p & KAndroidFindBy(id="inputEmail")
@iOSXCUITFindBy(id="inputEmail")
public MobileElement email_txt;
// 密码
@AndroidFindBy(id="inputPassword")
@iOi Y J b H {SXCUITFindBy(id="inputPassword")
public MobileElement password_txt;
// 姓名
@AndroidFindBy(id="inputName")
@iOSXCUIT@ ; S ]FindBy(id="inputName")
public MobileElement name_txt;
// 编程语言
@AndroidFindBy(id^ i o u F @ r="input_preferedProgrammingLanguagb v E p F Xe")
@iOSXCUITFindBy(id( ( N 7="input_preferedProgrammingLanguage"Z f / O $ I)
public MobileElement language_sel;
public List<MobileElement> prgLanguage;
// 是否确认注册
@AndroidFindBy(id="input_adds")
@iOSXCUITFindBy(id="input_adds")
public MobileElement accept_che2 z ; v [ck;
//y d A ? 5 ^ 注册按钮
@AndroidFindBy(id="btnRegisterUser")
@iOn U 8 * VSXCUITFindBy(id="btnRegisterUser")
public MobileEln | * S *ement register_btn;
public RegisterVerif; 0 | b AyPage_PF registeu { w 0r_sucess(String username, String email, St! n o s r d fring password, String name,
String language, String check)C o T @ c t E{
this.username_txt.sendKeys(username);
this.email_txt.sendKeys(email);
this.pah w {ssword_txt.sendKeys(password);
this.name_txt.clear();
this.name_txt.sendKeys(name);
this.language_sel.click();
checkedTextVV 1 m O |iew(language);
if(check.equals("Yes")){
if(!this.ai c i q O - gccept_check.isSelected())
this.accept_check.click(# 5 : m 0 N);
}
this.register_btn.click();
return new RegisterVerifyPage_PF(driver);
}
public void checkev z 8 jdTextVil 9 * D Z q ~ /ew(String language){
// 使用class属性选择所有的单选按钮,并存放在一个list中
@SuppressWarnings("unchecked")
List<MobileElement> checkTextViews = (List<MobileElement>) driver
.# . V !findElementt K : G b Q C 4sByClassName("android.widget.CheckedTextVeiw");
// 使用for循环将M q X fList中的每个8 * i B  2 a D单选按钮进行遍历,查找name值为Ruby的单选按钮
// 如果该按钮未处于选中状态则调用click方法进行选择
for (MobileElement|  % checkedTextVieC % 4 I e {w:checkTextViews){
if(checkedTextView.getAttribute("name").equals(lanF d 4 ) * Eguage)){
if(!checkedTextView.isL @ : Y o kSelected()){
checkedTextVia 5 G [ : +ew.click();
}
}
}
}
}
package org.davieyang.pN 7 y X 1 f {ages;
import org.openqa.selenium.By;
import io.appium.java_c,  E % x ~lient.android.AndroidDriver;
im! ! ? # vport org.openqa.selenium.supp) A {ort.PageFax l 5ctory;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.pagefactory.Androt N t ^ pidFindBy;
import io.appium.java_client. ~ 0 J 6pagefactory.AppiumFieX . ; # W 7 [ R JldDecorator;
import io.appiE 5 u V + hum.java_client.MobileElement;
public class RegisterVerifyPage_PF {
And % S ] 2 EdroidDriver<?> driver;
public RegisterVerifyPage_PF) t B 7(AndroidDriverf t g  C j G e<?> driver){
this.driver = driver;
PageFactory.initElements(new/ ] 1 9 z N N 0 o AppiumFieldDecorator(driver), t4 2 P r - P q !his);
}
// 页面对象识别
@AndroidFindBy(id="label_name_data")
@iOSXCUITFindBy(id="label_nan L Z ? U Y vme_data")
public MobileElement label_name_data;
// 页面对象识别
@AndroidFindBy(id="label_username_data")
@iOSI  = G d c XXCUITFindBy(id="label_username_data")
public MobileElement label_username_data;
// 页面对象识别
@AndroidFindBy(id=l e P v e 2  |"label_password_data")
@iOSXCUITFindBy(id="label_password_data")
public MobileElement label_password_data;
// 页面对象识别
@AndroidFindBy(id="label_email_data")
@iOSXCUITFindBy(id="label_email_data")
public MobileElement label_email% S H_dat_ ` 8 6 i f  e &a;
// 页面对象识别E & a
@AndroidFindBy(id=+ { A ^ "label_prefei b Z j { c a K =redProgrammingLanguage_data")
@iOSXCUITFindBy(id="label_preferedProgrammingL% C p v o uanguage_data")
public MobileE= $ 5 / mlement label_preferedProgrammingLanguage_data;
// 页面对象识别
@AndroidFindBy(id="label_acceptAdds_data")
@iOSXCUITFN G 0indBy(id="label_acceptAdds_data")
public Mobiled M n H DElement label_acceptAdds_data;
// 增加验证项,返回验n X D g Q O w [ u证值
public String gr 5 y M - 1 m ? 0et_name_value(){
return this.label_name_data.getTagNp n | d ! Mame().toString();
}
// 增加验证项,返回验证值
public String get_username_valuw E ~ { U `e(){
return this.label_username_data.getTagName().toString();
}
// 增加验R ? # h ^ u证项,返回验证值
p0 , ? & d b c {ublic Stringo 2 R i z get_password_value(){
return this.label_password_data.getTagName().toString();
}
// 增加验证项,返回验证值
public Strid Z / # R 7 O N Lng get_email_value(){
return this.label_email_data.getTagName().toString();
}
// 增加验证项,返回验证值
public String get_preferedProgrammingLanguage_value(){
reD z K r c U G Gturn this.Q & {label_preferedPrograc ^ ] N &mmingLanguag, 1 2 7 O X D ue_data.getTagNam8 K fe().toString();
}
// 增加验证项,返回验证值3 R Z #
public String get_ac^ o k v / ZceptAdds_value(){
return this.label_acceptAL ( j j 1 4 Sdds_data.getTagName().toString();
}
}