【问题标题】:What are the best practices for appium framework using page opject modelappium框架使用页面对象模型的最佳实践是什么
【发布时间】:2019-04-16 19:59:24
【问题描述】:

我正在使用 Appium(在 java 中)为移动应用程序开发自动化框架。我开始为每个视图创建一个类,在每个类中,我找到并初始化该类的元素。现在过了一会儿,我明白我的框架配置不正确,我无法扩展它。如果有人能在 Github 上向我介绍一个已实现的框架,那就太好了。

这是我的配置类,我使用 setdriver() 在任何需要的地方设置驱动程序。

public class Config {

public AndroidDriver<AndroidElement> driver;

public static AndroidDriver<AndroidElement> SetDriver() throws MalformedURLException {

    File appPath= new File("src");
    File app = new File(appPath,"My-debug.apk");

    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(MobileCapabilityType.DEVICE_NAME, "myDevice");
    cap.setCapability(MobileCapabilityType.APP,app.getAbsolutePath());
    cap.setCapability("autoGrantPermissions",true);
    cap.setCapability("appWaitActivity","com.xxxx.xxxx.ui.launch.LaunchActivity");
    AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);

    return driver;
}

提前谢谢你。

【问题讨论】:

    标签: selenium testing automation appium


    【解决方案1】:

    首先您必须创建加载驱动程序的基类。 注意:如果您使用的是本地线程,我们可以毫无问题地实现并行执行

    package com.vg.ui.utils.mobile;
    
    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.android.AndroidDriver;
    
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class Mobile {
    
        public static ThreadLocal<AppiumDriver> driverThread = new ThreadLocal<AppiumDriver>();
    
        public void setDriver(String deviceName, String platformVersion)
                throws MalformedURLException, InterruptedException {
            // TODO Auto-generated method stub
    
            File classpathRoot = new File(System.getProperty("user.dir"));
            File appDir = new File(classpathRoot, "Apps");
            File app = new File(appDir, "android-debug.apk");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
            capabilities.setCapability("deviceName", deviceName);
            capabilities.setCapability("platformVersion", platformVersion);
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("app", app.getAbsolutePath());
            if (deviceName.equals("Nexus6")) {
                driverThread.set(new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities));
            } else if (deviceName.equals("Nexus7")) {
                driverThread.set(new AndroidDriver(new URL(
                        "http://127.0.0.1:4724/wd/hub"), capabilities));
            } else if (deviceName.equals("Lenovo")) {
                driverThread.set(new AndroidDriver(new URL(
                        "http://127.0.0.1:4723/wd/hub"), capabilities));
            } else {
                System.out.println("Check the device name and platformversion");
            }
    
        }
    
        public static AppiumDriver getDriver() {
            return driverThread.get();
        }
    
        public static void closeDriver() {
            if (!getDriver().equals(null)) {
                 getDriver().quit();
            }
        }
    }
    

    第二:使用 PageFactory 方法为特定页面创建一个对象类。

    package com.vg.ui.pageobjects;
    
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    
    public class LandingPageObject {
    
        @FindBy(xpath = "//*[@text='Account Create']")
        WebElement btn_accountCreate;
    
        public void click_AccountCreate(){
            /*driverUtil.clickandWait(btn_accountCreate);*/
            btn_accountCreate.click();
        }
    
    }
    

    第三:然后在您的步骤定义类中,使用相应屏幕的 PageFactory 扩展 Mobile 驱动程序类,如下所示。

    package com.vg.ui.stepdefinitions;
    
    
    import org.openqa.selenium.support.PageFactory;
    
    import com.vg.ui.pageobjects.LandingPageObject;
    import com.vg.ui.utils.mobile.Mobile;
    
    import cucumber.api.java.en.When;
    
    public class LandingPage extends Mobile{
    
        LandingPageObject lp        = PageFactory.initElements(getDriver(), LandingPageObject.class);
    
        @When("^click on the button account create\\.$")
        public void click_on_the_button_account_create() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
            lp.click_AccountCreate();
        }
    
    
    }
    

    【讨论】:

    • 请您详细解释一下 PageFactory,它是如何工作的?
    • 请参考此链接。 guru99.com/…
    【解决方案2】:

    首先定义你的页面如下:

    public class WelcomeScreen{
        @AndroidFindBy(accessibility = "") //you can use id, accessibility-id or xpath
        @iOSFindBy(accessibility = "")
        private MobileElement element1;
    
        @AndroidFindBy(accessibility = "")
        @iOSFindBy(accessibility = "")
        private MobileElement element2;
    
        AppiumDriver<MoblieElement> driver;
        public WelcomeScreen(AppiumDriver<MobileElement> driver) {
            this.driver=driver;
            PageFactory.initElements(new AppiumFieldDecorator(driver), this);
        }
    
        public void clickElement2(){
            element2.click()
        }
    }
    

    然后设置 DesiredCapabilities 和 AppiumDriver。

    之后在其他类中使用页面对象模型。

    WelcomeScreen screen=new WelcomeScreen(driver);
    screen.clickElement2();
    

    确保您的驱动程序是全球性的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-21
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多