Appium Appium 入门到原理之 Appium 创建一个 Note 的实例

TechoGoGoGo · 2015年02月04日 · 最后由 wanter 回复于 2016年03月09日 · 1930 次阅读

近来通过 AppiumRobotium等几个框架去了解移动平台自动化测试。 Appium 官方实例是使用 ContactManager.apk,而Robotium 使用的是 SDK 自带的 Notepad.apk,为了方便比较,在了解 Appium 的同时把实例修改成跟 Robotium 一致的 Notepad.apk 并记录下其中一个 Case 如下:

package majcit.com.AppiumDemo;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;

import java.io.File;
import java.net.URL;
import java.util.List;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Unit test for simple App.
 */
public class NoetPadTest {
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    private AppiumDriver driver;

    @Before
    public void setUp() throws Exception {
        // set up appium
        File classpathRoot = new File(System.getProperty("user.dir"));
        File appDir = new File(classpathRoot, "apps");
        File app = new File(appDir, "NotePad.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName","Android");
        //capabilities.setCapability("platformVersion", "4.2");
        capabilities.setCapability("platformName", "Android");
        //capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.example.android.notepad");
        capabilities.setCapability("appActivity", "com.example.android.notepad.NotesList");
        //capabilities.setCapability("appActivity", ".NotesList");
        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public void addNoteCNTitle() throws InterruptedException{
        //Evoke the system menu option
        driver.sendKeyEvent(82);


        try {
            Thread.sleep(1000);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }

        //Click on the "Add Note" menu entry
        WebElement el = driver.findElement(By.name("Add note"));
        el.click();

        //Enter the note info and save it
        WebElement text = driver.findElementByClassName("android.widget.EditText");
        text.sendKeys("Note 1");

        driver.sendKeyEvent(82);
        el = driver.findElement(By.name("Save"));
        el.click();

        //Find out the new added note entry
        List <WebElement> entries = driver.findElements(By.className("android.widget.TextView"));

        WebElement targetEntry = null;
        for(WebElement entry : entries) {
            if(entry.getText().equals("Note1")) {
                targetEntry = entry;
                break;
            }
        }

        //Long press on the men entry to call out the context menu options
        TouchAction action = new TouchAction(driver);
        action.longPress(targetEntry);
        //action.moveTo(we,240,10);
        action.perform();

        //Find out the menu entry of "Delete"
        WebElement optionListView = driver.findElement(By.className("android.widget.ListView"));
        List <WebElement> options = optionListView.findElements(By.className("android.widget.TextView"));
        WebElement delete = null;
        for (WebElement option:options) {
            if (option.getText().equals("Delete")) {
                delete = option;
                break;

            }
        }

        assertThat(delete,notNullValue());

        //Delete the menu entry
        delete.click();

        try {
            Thread.sleep(1000);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }



    }

}

作者:天地会珠海分舵
http://techgogogo.com
http://blog.csdn.net/zhubaitian

共收到 2 条回复 时间 点赞

你好,下面这行代码我总是报错。

import io.appium.java_client.AppiumDriver;
...

 driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); //总是报错

Eclipse 提示:Cannot instantiate the Type AppiumDriver

后来在网上查了下,发现 appium 升级到 2.0.0 后,原有的 AppiumDriver 函数变成抽象函数了。
需要更改为 AndroidDriver 或者 IOSDriver,同时不需要再增加 platformName

1 楼好人

恒温 Appium 入门到原理合集 中提及了此贴 03月16日 08:33
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册