Appium安卓驱动程序

时间:2015-03-04 15:25:07

标签: android appium

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import android.provider.Browser;
import android.test.ActivityInstrumentationTestCase2;

import com.example.secretsanta.MainActivity;

public class AppiumExampleTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private RemoteWebDriver driver;

    public AppiumExampleTest(Class <MainActivity> activityClass) {
        super(activityClass);
    }

    @Before
    public void setup() throws MalformedURLException {
        File appDir = new File("../secret_santa//bin");
        File app = new File(appDir, "secret_santa.apk");

        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("version", "5");
        caps.setCapability("device", "emulator-5554");
        caps.setCapability("platform", "ANDROID");
        caps.setCapability("browser", "CHROME");
        caps.setCapability("app-package", "com.example.secretsanta");
        caps.setCapability("app-activity", "MainActivity.class");
        caps.setCapability("app", app.getAbsolutePath());


            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);


        driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

    }

    @Test
    public void testAppiumExample() throws Exception {

        // find button with label or content-description "Button 1"

        driver.findElement(By.id("createListButton")).click();

        // click on button and start second Activity

        // we are on second screen now
        // check if second screen contains element with text “Activity2”
        driver.findElement(By.id("createListView"));

        // click back button
        HashMap<String, Integer> keycode = new HashMap<String, Integer>();
        keycode.put("keycode", 4);
        ((JavascriptExecutor) driver).executeScript("mobile: keyevent", keycode);
        //
        // // we are again in main activity
        driver.findElement(By.id("createListButton"));
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

}

我试图按下按钮,然后转到下一个活动,然后返回主要活动。目前我有一些错误,一个是actvityinstrumentationtestcase2,然后是构造函数,另一个是元素。

5 个答案:

答案 0 :(得分:1)

您是否尝试使用

private RemoteWebDriver driver;

而不是

private WebDriver driver;

就像它对我有用。

答案 1 :(得分:0)

尝试使用Appium驱动程序:

protected AppiumDriver driver;

答案 2 :(得分:0)

您还可以尝试在Test Base(父级)类中进行以下操作:

    public static AppiumDriver getDriver() {
    return driver;
}

答案 3 :(得分:0)

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

适合我。

答案 4 :(得分:0)

尝试设置类似以下内容的功能:

  public class AbstractTest{

    public static AndroidDriver driver;
    private static String appFileName = "";
    private static String appDirName = "";
    public static String appPack = "";

    static File app;
    static File appDir;

    static DesiredCapabilities cap = new DesiredCapabilities();

    @BeforeSuite
    public void setUp() throws MalformedURLException {

        try {
            appFileName = PropertyLoader.loadProperty("appFileName");
            appDirName = PropertyLoader.loadProperty("appDirName");
            appPack = PropertyLoader.loadProperty("appPack");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        appDir = new File(appDirName);
        app = new File(appDir, appFileName);

        cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");

        cap.setCapability(MobileCapabilityType.PLATFORM_NAME,
                MobilePlatform.ANDROID);
//      For Android Emulator
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        cap.setCapability("app","C:\\secret\\secret\\secret\\secret.apk");

        cap.setCapability("appPackage","secret");

        cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
        cap.setCapability("unicodeKeyboard", "True");


        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        PageFactory.initElements(new AppiumFieldDecorator(driver),new LoginPage());


    }

您应该提供实际的文件名,目录名,包名等