Appium创建一个Note的实例

近来通过Appium,Robotium等几个框架去了解移动平台自动化测试。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());
        }
        
        
        
    }
    
}


当使用 Appium 编写第一个程序时,你需要完成以下步骤: 1. 安装 Appium:首先,确保你的计算机上已经安装了 Node.js。然后,在命令行中运行以下命令来安装 Appium: ``` npm install -g appium ``` 2. 安装 Appium Server:你可以通过命令行运行 `appium` 命令来启动 Appium 服务器。 3. 创建一个测试项目:在你选择的编程语言中,创建一个新的项目。你可以使用 Java、Python、C# 等编程语言来编写 Appium 测试脚本。 4. 配置测试环境:在你的项目中,添加 Appium 的相关依赖库或包。这些依赖库或包通常是由相应编程语言的测试框架提供的。例如,对于 Java,你可以使用 TestNG 或 JUnit。 5. 编写测试脚本:在你的测试项目中,编写测试脚本来模拟用户与应用程序的交互。你可以使用 Appium 提供的 API 来定位元素、执行操作和断言结果。 以下是一个使用 Java 和 TestNG 的示例代码: ```java import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.*; import java.net.URL; public class AppiumTest { private AppiumDriver<WebElement> driver; @BeforeTest public void setup() throws Exception { // 设置 Appium 配置 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "Android Emulator"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("platformVersion", "10.0"); capabilities.setCapability("app", "/path/to/your/app.apk"); // 初始化 AppiumDriver driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @Test public void testApp() { // 在此处编写你的测试步骤 // 例如:driver.findElement(By.id("elementId")).click(); } @AfterTest public void teardown() { // 关闭 AppiumDriver driver.quit(); } } ``` 请注意,上述代码仅供参考。你需要根据你的具体应用程序和测试需求进行适当的修改。 希望这个简单的示例能够帮助你开始使用 Appium 编写第一个程序!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值