JavaAppium实例代码
我们先看一段真实的java代码,我将他放在了TestNG框架的@BeforeTest里,执行这段即可启动安卓手机上的APP“com.tencent.Q108” 后边一一详述
1 @BeforeTest
2 public void setUp() throws MalformedURLException{
3 DesiredCapabilities caps = new DesiredCapabilities();
4 // des.setCapability("app", "c:\\");
5 caps.setCapability("automationname", "Appium");
6 caps.setCapability("platformName", "Android");
7 caps.setCapability("platformVersion", "23");
8 caps.setCapability("udid", "6EJ4C18A18000811");
9 caps.setCapability("deviceName", "Honor");
10 caps.setCapability("appPackage", "com.tencent.Q108");//com.android.contacts
11 caps.setCapability("appActivity", "com.tencent.Q108.SplashActivity");//.activities.PeopleActivity
12 caps.setCapability("appWaitActivity", "com.tencent.Q108.SplashActivity");
13 caps.setCapability("unicodeKeyboard", "True");
14 caps.setCapability("resetKeyboard", "True");
15 caps.setCapability("newCommandTimeout", "15");
16 caps.setCapability("nosign", "True");
17 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19 }
Desired Capabilities
测试脚本到驱动移动端的整个运转过程,Desired capability是以一个JSON对象形式传递的,包含一组key和value值。它由客户端发送给服务端,告诉服务端期望的Capabilities(可以理解为一种能力)有哪些,然后服务端根据这些capabilities创建自动化会话(session)
- IOS
- Android
获取Desired Capabilities
获取Desired Capabilities需要使用工具adb
和**aapt
,这两项都来自SDK,配置好环境变量后便可以直接在命令行使用
获取 udid
手机连接电脑或模拟器开启,并确定已经开机手机的USB调试,CMD下输入adb devices
,如果出现以下提示,则大概率上是因为移动端没有成功开启USB调试
确认USB调试开启后,再次输入命令,则
所获取的串6EJ4C18A18000811
就是udid
配置aapt
环境变量
将aapt.exe在安卓SDK的路径添加的系统环境变量的Path中去,例如
如果没有build-tools那说明还没有下载,可打开SDK Manager自行下载,下载完成后,添加Path路径即可
aapt环境验证
CMD下运行aapt,如下图所示则表明环境可以了
获取appPackage和appActivity
将准备测试的 APK 放到某个目录,在相同的目录下启动CMD,并执行如下命令:aapt dump badging tencentmobilemanager.apk
(根据各自的apk包名来)
如图所示,能看到package:name/launchable activity:name, 这两项即使我们要获取的"appPackage" 和 “appActivity”
获取webdriver.Remote地址
注意这个地址可以自定义,例如你的手机连接在别的机器上通过这个IP的配置,一样可以实现驱动,并非只能驱动链接到本机的移动设备
启动Appium,如图所示设置能看到IP和端口
其他的参数均来自你实际的环境,根据实际环境配置即可
关于Desired Capabilities
io.appium.java_client 也提供了MobileCapabilityType和AndroidMobileCapabilityType,看如下代码,也可以设置Capabilities
- org.openqa.selenium.remote.DesiredCapabilities;
- io.appium.java_client.remote.AndroidMobileCapabilityType;
- io.appium.java_client.remote.MobileCapabilityType;
1 package testscript;
2 import io.appium.java_client.TouchAction;
3 import io.appium.java_client.android.AndroidDriver;
4 import io.appium.java_client.remote.AndroidMobileCapabilityType;
5 import io.appium.java_client.remote.MobileCapabilityType;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.concurrent.TimeUnit;
9 import org.openqa.selenium.remote.DesiredCapabilities;
10 import org.testng.annotations.AfterClass;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.Test;
13
14 public class Test_Scroll_Swipe {
15 AndroidDriver driver;
16 @BeforeClass
17 public void setUp() throws MalformedURLException{
18 DesiredCapabilities caps = new DesiredCapabilities();
19 caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
20 caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
21 caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
22 caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
23 caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
24 driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
25 driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
26 }
在Desired Capabilities的设置上Java和Python是同理的
Python环境
在CMD中运行 pip install Appium-Python-Client
Microsoft Windows [版本 10.0.19045.3693]
(c) Microsoft Corporation。保留所有权利。
C:\Users\Administrator>pip install Appium-Python-Client
Collecting Appium-Python-Client
Downloading Appium-Python-Client-3.1.0.tar.gz (3.4 MB)
---------------------------------------- 3.4/3.4 MB 297.9 kB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: selenium~=4.12 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from Appium-Python-Client) (4.15.2)
Requirement already satisfied: urllib3<3,>=1.26 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from urllib3[socks]<3,>=1.26->selenium~=4.12->Appium-Python-Client) (2.0.7)
Requirement already satisfied: trio~=0.17 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from selenium~=4.12->Appium-Python-Client) (0.23.1)
Requirement already satisfied: trio-websocket~=0.9 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from selenium~=4.12->Appium-Python-Client) (0.11.1)
Requirement already satisfied: certifi>=2021.10.8 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from selenium~=4.12->Appium-Python-Client) (2023.7.22)
Requirement already satisfied: attrs>=20.1.0 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (23.1.0)
Requirement already satisfied: sortedcontainers in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (2.4.0)
Requirement already satisfied: idna in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (3.4)
Requirement already satisfied: outcome in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (1.3.0.post0)
Requirement already satisfied: sniffio>=1.3.0 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (1.3.0)
Requirement already satisfied: cffi>=1.14 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (1.16.0)
Requirement already satisfied: wsproto>=0.14 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio-websocket~=0.9->selenium~=4.12->Appium-Python-Client) (1.2.0)
Requirement already satisfied: pysocks!=1.5.7,<2.0,>=1.5.6 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from urllib3[socks]<3,>=1.26->selenium~=4.12->Appium-Python-Client) (1.7.1)
Requirement already satisfied: pycparser in c:\users\administrator\appdata\roaming\python\python312\site-packages (from cffi>=1.14->trio~=0.17->selenium~=4.12->Appium-Python-Client) (2.21)
Requirement already satisfied: h11<1,>=0.9.0 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from wsproto>=0.14->trio-websocket~=0.9->selenium~=4.12->Appium-Python-Client) (0.14.0)
Building wheels for collected packages: Appium-Python-Client
Building wheel for Appium-Python-Client (pyproject.toml) ... done
Created wheel for Appium-Python-Client: filename=Appium_Python_Client-3.1.0-py3-none-any.whl size=337588 sha256=d29b618102d0b617d4b42076d3b21df506d701b10f3669ae5079861b7261e24a
Stored in directory: c:\users\administrator\appdata\local\pip\cache\wheels\f9\96\78\c77d7a11d58afb91cf95104558a38e4e56745a8590177f71da
Successfully built Appium-Python-Client
Installing collected packages: Appium-Python-Client
Successfully installed Appium-Python-Client-3.1.0
示例代码
'''
@Time : 2018/11/12 13:37
@Author : davieyang
@Software: PyCharm
@Description:
'''
# encoding = utf-8
from appium import webdriver
import time
import unittest
class test_ClickButon(unittest.TestCase):
def setUp(self):
desired_caps = {
'platformName': 'Android',
'deviceName': '30d4e606',
'platformVersion': '5.0',
'appPackage': 'com.tencent.qqpimsecure',
'appActivity': 'com.tencent.server.fore.QuickLoadActivity'
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
def test_ClickButton(self):
driver = self.driver
driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click()
JavaAppium实例代码
我们先看一段真实的java代码,我将他放在了TestNG框架的@BeforeTest里,执行这段即可启动安卓手机上的APP“com.tencent.Q108” 后边一一详述
1 @BeforeTest
2 public void setUp() throws MalformedURLException{
3 DesiredCapabilities caps = new DesiredCapabilities();
4 // des.setCapability("app", "c:\\");
5 caps.setCapability("automationname", "Appium");
6 caps.setCapability("platformName", "Android");
7 caps.setCapability("platformVersion", "23");
8 caps.setCapability("udid", "6EJ4C18A18000811");
9 caps.setCapability("deviceName", "Honor");
10 caps.setCapability("appPackage", "com.tencent.Q108");//com.android.contacts
11 caps.setCapability("appActivity", "com.tencent.Q108.SplashActivity");//.activities.PeopleActivity
12 caps.setCapability("appWaitActivity", "com.tencent.Q108.SplashActivity");
13 caps.setCapability("unicodeKeyboard", "True");
14 caps.setCapability("resetKeyboard", "True");
15 caps.setCapability("newCommandTimeout", "15");
16 caps.setCapability("nosign", "True");
17 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19 }
Desired Capabilities
测试脚本到驱动移动端的整个运转过程,Desired capability是以一个JSON对象形式传递的,包含一组key和value值。它由客户端发送给服务端,告诉服务端期望的Capabilities(可以理解为一种能力)有哪些,然后服务端根据这些capabilities创建自动化会话(session)
- IOS
- Android
获取Desired Capabilities
获取Desired Capabilities需要使用工具adb
和**aapt
,这两项都来自SDK,配置好环境变量后便可以直接在命令行使用
获取 udid
手机连接电脑或模拟器开启,并确定已经开机手机的USB调试,CMD下输入adb devices
,如果出现以下提示,则大概率上是因为移动端没有成功开启USB调试
确认USB调试开启后,再次输入命令,则
所获取的串6EJ4C18A18000811
就是udid
配置aapt
环境变量
将aapt.exe在安卓SDK的路径添加的系统环境变量的Path中去,例如
如果没有build-tools那说明还没有下载,可打开SDK Manager自行下载,下载完成后,添加Path路径即可
aapt环境验证
CMD下运行aapt,如下图所示则表明环境可以了
获取appPackage和appActivity
将准备测试的 APK 放到某个目录,在相同的目录下启动CMD,并执行如下命令:aapt dump badging tencentmobilemanager.apk
(根据各自的apk包名来)
如图所示,能看到package:name/launchable activity:name, 这两项即使我们要获取的"appPackage" 和 “appActivity”
获取webdriver.Remote地址
注意这个地址可以自定义,例如你的手机连接在别的机器上通过这个IP的配置,一样可以实现驱动,并非只能驱动链接到本机的移动设备
启动Appium,如图所示设置能看到IP和端口
其他的参数均来自你实际的环境,根据实际环境配置即可
关于Desired Capabilities
io.appium.java_client 也提供了MobileCapabilityType和AndroidMobileCapabilityType,看如下代码,也可以设置Capabilities
- org.openqa.selenium.remote.DesiredCapabilities;
- io.appium.java_client.remote.AndroidMobileCapabilityType;
- io.appium.java_client.remote.MobileCapabilityType;
1 package testscript;
2 import io.appium.java_client.TouchAction;
3 import io.appium.java_client.android.AndroidDriver;
4 import io.appium.java_client.remote.AndroidMobileCapabilityType;
5 import io.appium.java_client.remote.MobileCapabilityType;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.concurrent.TimeUnit;
9 import org.openqa.selenium.remote.DesiredCapabilities;
10 import org.testng.annotations.AfterClass;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.Test;
13
14 public class Test_Scroll_Swipe {
15 AndroidDriver driver;
16 @BeforeClass
17 public void setUp() throws MalformedURLException{
18 DesiredCapabilities caps = new DesiredCapabilities();
19 caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
20 caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
21 caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
22 caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
23 caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
24 driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
25 driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
26 }
在Desired Capabilities的设置上Java和Python是同理的
Python环境
在CMD中运行 pip install Appium-Python-Client
Microsoft Windows [版本 10.0.19045.3693]
(c) Microsoft Corporation。保留所有权利。
C:\Users\Administrator>pip install Appium-Python-Client
Collecting Appium-Python-Client
Downloading Appium-Python-Client-3.1.0.tar.gz (3.4 MB)
---------------------------------------- 3.4/3.4 MB 297.9 kB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: selenium~=4.12 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from Appium-Python-Client) (4.15.2)
Requirement already satisfied: urllib3<3,>=1.26 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from urllib3[socks]<3,>=1.26->selenium~=4.12->Appium-Python-Client) (2.0.7)
Requirement already satisfied: trio~=0.17 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from selenium~=4.12->Appium-Python-Client) (0.23.1)
Requirement already satisfied: trio-websocket~=0.9 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from selenium~=4.12->Appium-Python-Client) (0.11.1)
Requirement already satisfied: certifi>=2021.10.8 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from selenium~=4.12->Appium-Python-Client) (2023.7.22)
Requirement already satisfied: attrs>=20.1.0 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (23.1.0)
Requirement already satisfied: sortedcontainers in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (2.4.0)
Requirement already satisfied: idna in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (3.4)
Requirement already satisfied: outcome in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (1.3.0.post0)
Requirement already satisfied: sniffio>=1.3.0 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (1.3.0)
Requirement already satisfied: cffi>=1.14 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio~=0.17->selenium~=4.12->Appium-Python-Client) (1.16.0)
Requirement already satisfied: wsproto>=0.14 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from trio-websocket~=0.9->selenium~=4.12->Appium-Python-Client) (1.2.0)
Requirement already satisfied: pysocks!=1.5.7,<2.0,>=1.5.6 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from urllib3[socks]<3,>=1.26->selenium~=4.12->Appium-Python-Client) (1.7.1)
Requirement already satisfied: pycparser in c:\users\administrator\appdata\roaming\python\python312\site-packages (from cffi>=1.14->trio~=0.17->selenium~=4.12->Appium-Python-Client) (2.21)
Requirement already satisfied: h11<1,>=0.9.0 in c:\users\administrator\appdata\roaming\python\python312\site-packages (from wsproto>=0.14->trio-websocket~=0.9->selenium~=4.12->Appium-Python-Client) (0.14.0)
Building wheels for collected packages: Appium-Python-Client
Building wheel for Appium-Python-Client (pyproject.toml) ... done
Created wheel for Appium-Python-Client: filename=Appium_Python_Client-3.1.0-py3-none-any.whl size=337588 sha256=d29b618102d0b617d4b42076d3b21df506d701b10f3669ae5079861b7261e24a
Stored in directory: c:\users\administrator\appdata\local\pip\cache\wheels\f9\96\78\c77d7a11d58afb91cf95104558a38e4e56745a8590177f71da
Successfully built Appium-Python-Client
Installing collected packages: Appium-Python-Client
Successfully installed Appium-Python-Client-3.1.0
示例代码
'''
@Time : 2018/11/12 13:37
@Author : davieyang
@Software: PyCharm
@Description:
'''
# encoding = utf-8
from appium import webdriver
import time
import unittest
class test_ClickButon(unittest.TestCase):
def setUp(self):
desired_caps = {
'platformName': 'Android',
'deviceName': '30d4e606',
'platformVersion': '5.0',
'appPackage': 'com.tencent.qqpimsecure',
'appActivity': 'com.tencent.server.fore.QuickLoadActivity'
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
def test_ClickButton(self):
driver = self.driver
driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click()