今天用了将近一整天的时间研究Appium,还算是有一点收获,现在将这个过程记录一下。
环境:win10;Python27
一:环境准备
1. 安装Node.js;直接从官网下载安装适配自己系统的版本,一路默认安装即可;注意:默认安装时会设定Path路径;
2. 安装JDK;同样默认安装即可,安装完之后设置JAVA_HOME变量为JDK目录,如:C:\Program Files\Java\jdk1.8.0_131,后将%JAVA_HOME%\jre\bin;%JAVA_HOME%\bin;添加到Path中;(如何添加Java环境变量);
3. 安装Android sdk;详情查看第三步、下载并安装AndroidSDK 注意:将其中的ANDROID_SDK_HOME改成ANDROID_HOME;
4. 安装Appium;选择以*.exe结尾的文件,默认安装即可;最后在CMD 窗口运行Appium-doctor,如下图示则成功的安装了依赖环境;
二:客户端安装
说是说客户端,其实就是一个Python API。直接用pip装;
pip install Appium-Python-Client
到这里,其实整个Appium的环境准备就算是全部准备好了,接下来就是跑一个小demo然后调试;
三 : Demo
- 首先双击打开Appium;Host设置为127.0.0.1,Port号不变;
- 写一个Python脚本如下:
import os
import pytest
from appium import webdriver
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
APPIUM_LOCAL_HOST_URL = 'http://localhost:4723/wd/hub'
PLATFORM_VERSION = '6.0'
class TestWebViewAndroid():
@pytest.fixture(scope="function")
def driver(self, request):
desired_caps = {
'appPackage': 'com.example.android.contactmanager',
'appActivity': '.ContactManager',
'platformName': 'Android',
'platformVersion': PLATFORM_VERSION,
'deviceName': 'Android Emulator',
'app': PATH('../../../../sample-code/apps/ContactManager/ContactManager.apk')
}
driver = webdriver.Remote(APPIUM_LOCAL_HOST_URL, desired_caps)
def fin():
driver.quit()
request.addfinalizer(fin)
return driver # provide the fixture value
def test_add_contacts(self, driver):
el = driver.find_element_by_accessibility_id("Add Contact")
el.click()
textfields = driver.find_elements_by_class_name("android.widget.EditText")
textfields[0].send_keys("Appium User")
textfields[2].send_keys("someone@appium.io")
assert 'Appium User' == textfields[0].text
assert 'someone@appium.io' == textfields[2].text
driver.find_element_by_accessibility_id("Save").click()
# for some reason "save" breaks things
alert = driver.switch_to_alert()
# no way to handle alerts in Android
driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)').click()
driver.press_keycode(3)
上述代码需做相应的修改,具体参照appium简明教程(7)——Desired Capabilities详解
元素定位可以借助uiautomatorviewer查看;
四 : 直接运行脚本
连接好手机之后,直接运行Python脚本即可;
详情请参考:Appium 官方Github https://github.com/appium/appium
注意:上述脚本我还没有调试成功,主要是元素定位有问题,但是不会报错,我会继续想办法解决这个问题。