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

PageFactory模式

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

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

// PageFactory
import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.MobileElement;


public class HomePage_PF {
     
    AndroidDriver<?> driver;
    public HomePage_PF(AndroidDriver<?> driver){
     
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    // 页面对象时别,进入注册页面
    @AndroidFindBy(id="startUserRegistration")
    @iOSXCUITFindBy(id="startUserRegistration")
    public MobileElement startRegister_btn;

    // 单击注册页面按钮,进入注册页面,返回注册页面对象
    public RegisterPage_PF navigate_register_page(){
     
        this.startRegister_btn.click();
        return new RegisterPage_PF(driver);
    }
}
package org.davieyang.pages;
import java.util.List;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.MobileElement;

public class RegisterPage_PF {
     
    AndroidDriver<?> driver;
    public RegisterPage_PF(AndroidDriver<?> driver){
     
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }


    // 页面对象时别
    // 用户名
    @AndroidFindBy(id="inputUsername")
    @iOSXCUITFindBy(id="inputUsername")
    public MobileElement username_txt;

    // 邮箱
    @AndroidFindBy(id="inputEmail")
    @iOSXCUITFindBy(id="inputEmail")
    public MobileElement email_txt;

    // 密码
    @AndroidFindBy(id="inputPassword")
    @iOSXCUITFindBy(id="inputPassword")
    public MobileElement password_txt;


    // 姓名
    @AndroidFindBy(id="inputName")
    @iOSXCUITFindBy(id="inputName")
    public MobileElement name_txt;

    // 编程语言
    @AndroidFindBy(id="input_preferedProgrammingLanguage")
    @iOSXCUITFindBy(id="input_preferedProgrammingLanguage")
    public MobileElement language_sel;
    public List<MobileElement> prgLanguage;

    // 是否确认注册
    @AndroidFindBy(id="input_adds")
    @iOSXCUITFindBy(id="input_adds")
    public MobileElement accept_check;

    // 注册按钮
    @AndroidFindBy(id="btnRegisterUser")
    @iOSXCUITFindBy(id="btnRegisterUser")
    public MobileElement register_btn;

    public RegisterVerifyPage_PF register_sucess(String username, String email, String password, String name,
                                              String language, String check){
     
        this.username_txt.sendKeys(username);
        this.email_txt.sendKeys(email);
        this.password_txt.sendKeys(password);
        this.name_txt.clear();
        this.name_txt.sendKeys(name);
        this.language_sel.click();
        checkedTextView(language);
        if(check.equals("Yes")){
     
            if(!this.accept_check.isSelected())
                this.accept_check.click();
        }
        this.register_btn.click();
        return new RegisterVerifyPage_PF(driver);
    }

    public void checkedTextView(String language){
     
        // 使用class属性选择所有的单选按钮,并存放在一个list中
        @SuppressWarnings("unchecked")
        List<MobileElement> checkTextViews = (List<MobileElement>) driver
                .findElementsByClassName("android.widget.CheckedTextVeiw");
        // 使用for循环将List中的每个单选按钮进行遍历,查找name值为Ruby的单选按钮
        // 如果该按钮未处于选中状态则调用click方法进行选择
        for (MobileElement checkedTextView:checkTextViews){
     
            if(checkedTextView.getAttribute("name").equals(language)){
     
                if(!checkedTextView.isSelected()){
     
                    checkedTextView.click();
                }
            }
        }
    }
}


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

import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.MobileElement;

public class RegisterVerifyPage_PF {
     
    AndroidDriver<?> driver;
    public RegisterVerifyPage_PF(AndroidDriver<?> driver){
     
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    // 页面对象识别
    @AndroidFindBy(id="label_name_data")
    @iOSXCUITFindBy(id="label_name_data")
    public MobileElement label_name_data;

    // 页面对象识别
    @AndroidFindBy(id="label_username_data")
    @iOSXCUITFindBy(id="label_username_data")
    public MobileElement label_username_data;

    // 页面对象识别
    @AndroidFindBy(id="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_data;

    // 页面对象识别
    @AndroidFindBy(id="label_preferedProgrammingLanguage_data")
    @iOSXCUITFindBy(id="label_preferedProgrammingLanguage_data")
    public MobileElement label_preferedProgrammingLanguage_data;

    // 页面对象识别
    @AndroidFindBy(id="label_acceptAdds_data")
    @iOSXCUITFindBy(id="label_acceptAdds_data")
    public MobileElement label_acceptAdds_data;

    // 增加验证项,返回验证值
    public String get_name_value(){
     
        return this.label_name_data.getTagName().toString();
    }

    // 增加验证项,返回验证值
    public String get_username_value(){
     
        return this.label_username_data.getTagName().toString();
    }
    // 增加验证项,返回验证值
    public String get_password_value(){
     
        return this.label_password_data.getTagName().toString();
    }
    // 增加验证项,返回验证值
    public String get_email_value(){
     
        return this.label_email_data.getTagName().toString();
    }
    // 增加验证项,返回验证值
    public String get_preferedProgrammingLanguage_value(){
     
        return this.label_preferedProgrammingLanguage_data.getTagName().toString();
    }
    // 增加验证项,返回验证值
    public String get_acceptAdds_value(){
     
        return this.label_acceptAdds_data.getTagName().toString();
    }


}


你可能感兴趣的:(PageFactory,PF模式,PageFactory模式)