Android Appium Server从启动到case完成的活动分析

来源:互联网 发布:大闹天宫进阶数据 编辑:程序博客网 时间:2024/06/05 19:04

此文的目的主要是通过分析Appium Server打印出来的log,加深对Appium Server所扮演角色的理解。

这整一个过程是由一个Test Case开始执行到结束,测试的对象是SDK自带的NotePad.apk。 Test Case很简单:打开Notepad程序,然后点击安卓的Menu Options按钮调出“Add Note”菜单按钮,然后点击该按钮,然后完成测试。

package majcit.com.AppiumDemo;

import io.appium.java_client.AppiumDriver;

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.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.*;

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”,”iPad Simulator”);
//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 addContact() throws InterruptedException{
driver.sendKeyEvent(82);

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

WebElement el = driver.findElement(By.name(“Add note”));
el.click();
try {
Thread.sleep(60000);
}catch(Exception e) {
System.out.println(e.getMessage());
}

}

}

下面我们就通过分析Log把Appium Server所做的事情分步骤描述一下。

1. 启动REST http服务器,默认监听本地4723端口,用于接收客户端(Test Case+Selenium/Appium Driver)发过来的JSON格式的命令指示。

2. 根据客户端提供的capabilities指示建立一个Android Sesision用于跟客户端保持后续通信

3. 通过”adb devices“命令检查安卓手机是否已经准备好

4.使用工具“aapt dump badging NotePad.apk”来获得Notepad的packageName和launchable activityName,注意示例代码中是没有指定这个两个capabilities的

5. 安卓手机shell调用命令获得机器的API Level是否已经超过16:”adb.exe -s HT21atd05099 shell getprop ro.build.version.sdk”

6. 通过adb执行相应的shell命令检查目标应用是否已经存在:“pm list packages -3 com.example.android.notepad”

7. 目标机器上清理目标应用运行环境:该停止就停止该清空数据就清空数据

8. 建立Appium Server到目标机器上的端口转发

9. 把AppiumBootstrap.apk push到目标设备:这是目标机器上通过uiautomator工具(框架)运行的服务端,用于接受处理client端发送过来的命令

10. 把settings_apk-debuug.apk和unlock_apk-debug.apk push到目标设别:TBD

11. 保证uiautomator没有已经在跑

12. 通过adb把目标机器上的AppiumBootStrap跑起来:”uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Rootstrap”.

13.通过adb在目标机器上 Launch notepad应用

14. 通知PC端目标应用已经在目标机器启动成功

15. 处理客户端发过来的”按下系统菜单“的命令:bootstrap把接受到的命令入队列,执行完成后通知客户端执行结果

16.定位”Add Note“菜单按钮:Bootstrap通过的UIAutomator的UISelector类根据Text获得菜单按钮的ID并返回给客户端

17. BootStrap执行”点击 Add Note菜单”命令

18. 测试完成,目标机器模拟点击Home按钮把目标应用放在后台

19. 关闭logcat

20. 关闭Uiautomator进程

0 0
原创粉丝点击