Android basics of automated testing --Appium

table of Contents

Appium idea

Use Appium automated testing There are two advantages

Appium architecture

 

appium API


 

Appium idea

Appium is a mobile platform to meet the requirements of test automation concept design is based on the following four:

1) You do not need to be as automated test your application in any form and had to recompile or modify your app

2) You should not lock themselves in a particular language and a particular framework to achieve up and run your tests

3) When it comes to test automation APIs, and a mobile testing framework should not do things "reinvent the wheel", and

4) a mobile test automation framework should be open source, whether it is in spirit, in fact, still nominally!

Use Appium automated testing There are two advantages

Appium uses the standard automation APIs across platforms, so across platforms without recompiling or modify their own applications.

Appium support Selenium WebDriver all supported languages ​​such as java, Object-C, JavaScript, Php, Python, Ruby, C #, Clojure, or Perl language, but you can use Selenium WebDriver of Api. Appium support any testing framework .Appium to achieve a true cross-platform automated testing. (This paper describes the Python usage)

Appium architecture

Appium with Node.js is a written HTTP server, it creates and manages multiple WebDriver sessions to and interact with different platforms, such as iOS, Android and so on. 

After Appium start a test, will start on the device under test (cell phone) a server, listen for instructions from the Appium server. Each platform like iOS and Android have different operation and interaction. So Appium will use a pile program "invasion" of the platform, and receive instructions to complete the run test cases

 

It is tested app:
run in a real device or simulator. This test system up and running interactive relationship is this: test program ( the Test Scrip ), told  Appium Server  what to do operations. For example, we are going to click on an element of the interface. How to tell  Appium Server  ? Among them an  http  connection via  http  request command to the  Appium Server  , Appium Server  receiving the command of the test program, and then issue commands to the control program running on the device (which program consists Appium Server injected into the apparatus operation), library automation control program calls the appropriate method to perform inside the control interface.

appium API

# 元素定位
driver.find_element_by_id("id")   # id定位
driver.find_element_by_name("name")  # name定位
driver.find_element_by_link_text("text")  # 链接名定位
driver.find_element_by_partial_link_text("text")  # 通过元素部分可见链接文本定位
driver.find_element_by_tag_name("name")  # 通过查找html的标签名称定位元素
driver.find_element_by_xpath("xpath")  # 路径定位
driver.find_element_by_class_name("android.widget.LinearLayout")  # 类名定位
driver.find_element_by_css_selector("css") # css选择器定位
driver.find_element_by_android_uiautomator # uiautomator

# 元素集合复数定位
driver.find_elements_by_id("id")  # id元素集合
driver.find_elements_by_name("name") # name元素集合
driver.find_elements_by_link_text("text") # 链接名元素集合
driver.find_elements_by_partial_link_text("text") # 部分元素可见链接集合
driver.find_elements_by_tag_name("name")  # html标签名集合
driver.find_elements_by_xpath("xpath")  # 路径定位集合
driver.find_elements_by_class_name("android.widget.LinearLayout")  # 类名定位集合
driver.find_elements_by_css_selector("css") # css选择器定位集合

# 输入框输入
driver.element.send_keys("中英")

# 锁定屏幕
driver.lock(5)

# 把当前应用置于后台
driver.background_app(5)

# 收起键盘
driver.hide_keyboard()

# 打开一个应用或者activity,仅安卓端
driver.start_activity('com.example.android.apis', '.Foo')

# 打开下拉通知栏 仅Android
driver.open_notifications()

# 拖动元素,将元素origin_el拖到目标元素destination_el
driver.drag_and_drop(self, origin_el, destination_el):

# 检查app是否已安装
driver.is_app_installed('com.example.android.apis')

# 安装应用到设备
driver.install_app('path/to/my.apk')

# 删除应用
driver.remove_app('com.example.android.apis')

# 模拟设备摇晃
driver.shake()


# 关闭应用
driver.close_app()

# 启动 (Launch)
# 根据服务关键字 (desired capabilities) 启动会话 (session) 。请注意这必须在设定 autoLaunch=false 关键字时才能生效。
# 这不是用于启动指定的 app/activities ————你可以使用 start_activity 做到这个效果————
# 这是用来继续进行使用了 autoLaunch=false 关键字时的初始化 (Launch) 流程的。
driver.launch_app()

# 应用重置,相当于重新卸载安装
driver.reset()

# 可用上下文 (context) 列出所有的可用上下文
# 翻译备注:context可以理解为 可进入的窗口 。例如,对于原生应用,可用的context和默认context均为NATIVE_APP。
# 详情可查看对混合应用进行自动化测试
driver.contexts

# 列出当前上下文
driver.current_context

# 切换到默认的上下文 (context)
# 将上下文切换到默认上下文
driver.switch_to.context(None)

# 获取应用的字符串
driver.app_strings

# 按键事件 (Key Event)给设备发送一个按键事件
driver.keyevent(176)

# 获取当前的activity
driver.current_activity

# 触摸动作(TouchAction) / 多点触摸动作(MultiTouchAction)
action = TouchAction(driver)
action.press(element=el, x=10, y=10).release().perform()

# 滑动(Swipe)模拟用户滑动
# 注意:appium滑动规则是x从左到右变大,y从上到下变大
driver.swipe(start=75, starty=500, endx=75, endy=0, duration=800)

# 捏 (Pinch)捏屏幕 (双指往内移动来缩小屏幕)
driver.pinch(element=el)

# 放大 (Zoom)放大屏幕 (双指往外移动来放大屏幕)
driver.zoom(element=el)

# 滑动到某个元素 (Scroll To)
todo: python

# 从设备中拉出文件 (Pull File)
driver.pull_file('Library/AddressBook/AddressBook.sqlitedb')

# 推送文件到设备中去
data = "some data for the file"
path = "/data/local/tmp/file.txt"
driver.push_file(path, data.encode('base64'))

# 断言
Assert.assertEquals("I am a div", div.getText()); //跳转到指定页面并在该页面所以用元素id进行交互

# 检查文本是否符合预期
assertEqual('I am a div', div.text)

# 输入法是否有活动 返回真假
is_ime_active(self):

# 返回当前安卓设备可用的输入法
driver.available_ime_engines(self):

# 激活安卓设备中的制定输入法
driver.activate_ime_engine(self, engine):

# 关闭当前的输入法(android)
driver.deactivate_ime_engine(self):

# 打开安卓设备上的位置定位设置
driver.toggle_location_services()

 

Published 58 original articles · won praise 4 · views 40000 +

Guess you like

Origin blog.csdn.net/jackron2014/article/details/104062827