使用页面对象模型的appium框架的最佳实践是什么

时间:2018-11-13 22:11:10

标签: selenium testing automation appium

我正在使用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;
}

谢谢。

2 个答案:

答案 0 :(得分:3)

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

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();
    }


}

答案 1 :(得分: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();
  

确保您的驱动程序是全局的。