使用Appium的页面对象工厂

时间:2017-05-04 20:53:42

标签: appium

我正在尝试将页面对象工厂用于Appium测试,并且似乎将两个页面链接在一起时出现问题。

下面的第一页

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import Test2.AbstractPageObject;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.remote.MobileCapabilityType;


public class Test1 {

    public static class PageObject {

        private final WebDriver driver;


        @AndroidFindBy(uiAutomator = "new UiSelector().text(\"Sign in\")")
        private WebElement element;

        public PageObject(WebDriver driver)

        {
            this.driver = driver;
            PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS), this);
        }

        public void click(){
            element.click();
        }
    }

    private PageObject po;

    @Before
    public void setup() throws Exception {
        File appDir = new File("src");
        File app = new File(appDir, "app.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());

        po = new PageObject(new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities));


    }

    @org.junit.Test
    public void test () {
        AbstractPageObject po = new AbstractPageObject (null);   
        po.test();
    }

第二页

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.remote.MobileCapabilityType;

public class Test2 {

     public static abstract class AbstractPageObject extends Test1 {

            protected final WebDriver driver;       
            public AbstractPageObject(WebDriver driver)
            {
                this.driver = driver;
                PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS), this);
            }
        }   


        public static class PageObject extends AbstractPageObject {

            public PageObject(WebDriver driver) {
                super(driver);
            }
            @FindBy(className="android.widget.ImageButton")
            private WebElement c;
            public void click1()
            {
                c.click();
            }

            @FindBy(id = "com.payment.pay:id/btn_sign_in")
            private WebElement d;
            public void click11()
            {
                d.click();
            }
            @FindBy(id="com.payment.pay:id/btn_forgotten_password")
            private WebElement e;
            public void click111()
            {
                e.click();
            }
            @FindBy(className= "android.widget.ImageButton")
            private WebElement f;
            public void click1111()
            {
                f.click();
            }

            @FindBy(id="com.payment.pay:id/btn_show_pass")
            private WebElement g;
            public void click11111()
            {
                g.click();
            }
        }
}

第二页看起来没问题,没有错误。该错误发生在第一页,无法导入AbstractPageObject。

1 个答案:

答案 0 :(得分:0)

在Java中,抽象类无法实例化,但它们可以是子类。您的第一页肯定会出错。有关详细信息,请查看此link