作者:刘潇
什么是Appium
Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. Native apps are those written using the iOS or Android SDKs. Mobile web apps are web apps accessed using a mobile browser (Appium supports Safari on iOS and Chrome or the built-in 'Browser' app on Android). Hybrid apps have a wrapper around a "webview" -- a native control that enables interaction with web content. Projects like Phonegap, make it easy to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app.
Importantly, Appium is "cross-platform": it allows you to write tests against multiple platforms (iOS, Android), using the same API. This enables code reuse between iOS and Android testsuites.
如上来自于官方的介绍,我这边整理下:Appium是一个可以用于iOS和Android平台上的自动化测试框架,它支持原生,移动Web以及混合型的应用程序的测试。注意上面一个比较重要的一点,Appium是一个跨平台的测试框架,也就是说它可以允许我们用同一套API完成对iOS,Android两个平台的测试,这就保证了我们测试脚本的重用性以及可维护性(当然,这个前提也是两个平台的UI界面比较接近才行)
翻译过来如下:
首先,为了能够实现上面哲学中提到的第1点,保证我们不用修改被测的App,Appium使用了官方提供的自动化测试框架:
其实,为了能够实现哲学里描述的第2条,让使用者可以根据自己的喜好或者擅长来编写脚本,Appium选择了C/S的架构,只要Client可以发送HTTP请求,它可以用任何语言来编写。
第3点,为了能够实现不要为了移动端的自动化测试而重新造轮子,Appium继续使用了WebDriver提供的API,同时又提供了更多更丰富的方法以供移动自动化使用。
第4点也很明显,Appium是完全开源的。
Appium is at its heart a webserver that exposes a REST API. It receives connections from a client, listens for commands, executes those commands on a mobile device, and responds with an HTTP response representing the result of the command execution. The fact that we have a client/server architecture opens up a lot of possibilities: we can write our test code in any language that has a http client API, but it is easier to use one of the Appium client libraries. We can put the server on a different machine than our tests are running on. We can write test code and rely on a cloud service like Sauce Labs to receive and interpret the commands.
Appium核心其实是一个暴露了REST API的WebServer。它接收Client端的连接,监听并在移动设备上执行Client发来的指令,再把结果通过HTTP请求发回给Client。这样看,C/S的架构的好处就特别明显了,由于通过HTTP来通信,Server端是可以完全不care Client端使用的语言,只能它能发送HTTP即可。Server端是Node.js编写的,而Client端的测试脚本选择的范围就比较多了,因此:
Automation is always performed in the context of a session. Clients initiate a session with a server in ways specific to each library, but they all end up sending a POST /session request to the server, with a JSON object called the 'desired capabilities' object. At this point the server will start up the automation session and respond with a session ID which is used for sending further commands.
Appium自动化测试基于Session,也就是会话。由Client初始化一个和Server的会话,通过发送一个POST /session
请求将'desired capabilities'传给Server端来开启会话,这样之后的所有请求都会挂上这个Session ID,也就是说Session ID就代表了每台移动设备和Server端的唯一标记。
Desired Capabilities就是键值对的集合(类似Java中的JSON,Ruby中的Hash等)。可以在测试启动时,将我们感兴趣或者说需要Appium知道的信息通过配置Capabilities,告知Appium Server。比如我们需要指定平台,那么就要将platformName
配置成Android
或者iOS
;如果我们想要在测试Safari时允许JS打开新窗口,那就需要将safariAllowPopups
设置为true
等等,详细的文档可以参考这里 capabilities doc
看了上面这些,你应该对Appium有了一个基本的印象:
网易云大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经作者刘潇授权发布