前面我已经搭建好了appium所需要的环境,本文我以系统自带的计算器app为测试对象,进行学习。
一、将测试机与PC连接
(本人使用的是真机)通过Usb接口将真机与PC端连接起来,在安装appium搭建环境中我们已经安装了SDK,所以打开CMD命令窗口,用adb devices
命令来查看是否可以检测到手机设备,如果如下图显示出设备,则证明我们可以对手机进行测试操作。
二、打开指定测试App的指定Activity
通过上一步操作,我已经看到我的设备是可以操作的了,那究竟应该如何让appium去自动找到指定的APP和指定的Activity呢?想要打开指定的App,需要知道App的包名,同样想要打开指定Activity也需要知道其名,如何获取?
1、问公司的开发人员,一定可靠。
2、通过命令获取:adb shell dumpsys window w |findstr \/ |findstr name=
(这条命令的作用是,获取当前app和当前Activity的名字,我现在手机已经打开了计算器应用)如图:
三、打开uiautomatorviewer获取元素
接下来需要用uiautomatorviewer
对测试app的元素(button、text Field)进行抓取,比如通过id、class和text等来确定具体的元素。 uiautomatorviewer是在android-sdk的一个工具,具体目录\android-sdk-windows\tools\uiautomatorviewer.bat
。
操作:
1、双击uiautomatorviewer.bat,打开UI Automator viewer 窗口,如图所示:
uiautomatorviewer.png
2、在手机与PC端连接的情况下,点击 “Device screenshot”, 连接手机应用的窗口将显示出来,如图:
Paste_Image.png3、测试“1+2=3”的用例,在uiautomatorviewer界面,点击“1”,选中该按钮,查看右边的panel来找到“text”或“resource-id” 或“class”的值,如图:
1按钮.png保存其:resource-id
+按钮.png保存其:resource-id
2按钮.png保存其:resource-id
![=按钮.png](https://img.haomeiwen.com/i1803308/6a2ad57f3cfef3c0.png?i
mageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
保存其:resource-id
四、创建java项目
我们已经获取到想要的元素,下一步就是用代码编写测试用例了,打开eclipse软件,创建一个java project ,导入依赖,如图:
Paste_Image.png五、测试源代码
代码实现1+2= 3的自动化测试
package com.occall.ui;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class loginOccall2 {
private AppiumDriver driver;
@BeforeClass
public void setup() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("platformName", "Android"); //指定测试平台
cap.setCapability("deviceName", "71MBBLG26R76"); //指定测试机的ID,通过adb命令`adb devices`获取
cap.setCapability("platformVersion", "5.1");
//将上面获取到的包名和Activity名设置为值
cap.setCapability("appPackage", "com.meizu.flyme.calculator");
cap.setCapability("appActivity", "com.meizu.flyme.calculator.Calculator");
//A new session could not be created的解决方法
cap.setCapability("appWaitActivity","com.meizu.flyme.calculator.Calculator");
//每次启动时覆盖session,否则第二次后运行会报错不能新建session
cap.setCapability("sessionOverride", true);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
@Test
public void plus(){
//获取1
driver.findElementById("com.meizu.flyme.calculator:id/digit1").click();
//获取+
driver.findElementById("com.meizu.flyme.calculator:id/plus").click();
//获取2
driver.findElementById("com.meizu.flyme.calculator:id/digit2").click();
//获取=
driver.findElementById("com.meizu.flyme.calculator:id/eq").click();
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}
六、启动Appium
手机设备连接到PC端,手机屏幕打开着。接着启动Appium,启动成功如下图:
Paste_Image.png七、运行测试类
Paste_Image.png八、大功告成,注意查看运行日志和appium控制台日志
Paste_Image.png Paste_Image.png可以看到,手机端的计算器app,也会自动出现1+2 = 3的自动化输入测试
网友评论
org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Command failed: C:\windows\system32\cmd.exe /s /c "D:\tools\android-sdk\android-sdk\platform-tools\adb.exe -s 3ee00cd2 install "D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk""
Failed to install D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]
) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9.10 seconds
Build info: version: '2.48.0', revision: 'b7b081a', time: '2015-10-07 15:48:56'
System info: host: 'P-V-12', ip: '192.168.1.111', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: io.appium.java_client.android.AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
......
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub";), cap);代码我是直接复制的,在new对象的时候这里有红线 这是提示:The type org.openqa.selenium.remote.http.HttpClient$Factory cannot be resolved. It is indirectly referenced from required .class files
我以为是build path方面的问题, 所以费劲周折导入了各个版本的jar包,然而这条红线纹丝不动,看到评论里有up发的git源码,就下载了看看,然而并没有jar包。。。。我的心情犹如拉了弦的手榴弹,瞬间爆炸!求指教
[TestNG] Running:
C:\Users\dongao\AppData\Local\Temp\testng-eclipse--462043372\testng-customsuite.xml
FAILED: plus
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {ELEMENT=1}
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46'
System info: host: 'dongao-PC', ip: '172.16.203.52', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_91'
Driver info: driver.version: AndroidDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:370)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:70)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementById(AndroidDriver.java:1)
at com.dongao.autoapptest.cc1.test.CaculatorTest.plus(CaculatorTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
我在网上查了一些,说和客户端Library有关,不知道你是不是用的 java-client-5.0.0-BETA6 这个版本,如果不是替换一下试试
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1551835753\testng-customsuite.xml
===============================================
Default test
Tests run: 0, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1d81bd16: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2bc758d7: 30 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@59c4fcd4: 4 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@23964a8d: 15 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@63f12af8: 4 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
为甚我console输出根本没运行到那个加的方法?
1、我看着你用的是junit,我不太熟悉,但是有点小毛病,before应该和after对应而不是afterclass~ 不知道是不是这里的原因!
2、我用的是testing会输出,但不知道junit是否会输出提示信息
3、你在plus方法里面System.out.println();一下,看看是否会有输出,如果有则表示这个方法已经走了,至于为什么不输出方法提示,也就无所谓了~
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
/**
* @Author xiaoxiao
*
*/
public class CalculatorTest {
private AppiumDriver driver;
@Before
public void setUp() throws IOException{
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability("paltformName", "Android");
cap.setCapability("paltformVersion", "4.2.0");//平台版本
cap.setCapability("", "");
cap.setCapability("deviceName", "2892d4ea");//被测试手机的名字
//获上面取到的包名和Activity设置为值
cap.setCapability("appPackage", "com.android.calculator2");
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
//A new Session could not be created解决方法
cap.setCapability("appWaitActivity", "com.android.calculator2.Calculator");
//每次启动覆盖session 否则第二次运行会报错不能新建session
cap.setCapability("sessionOverride", "true");
driver=new AndroidDriver(new URL("127.0.0.1:4723/wd/hub"), cap);
}
@test
public void plus(){//执行加法
driver.findElementById("com.android.calculator2:id/digit1").click();//1的ID
driver.findElementById("com.android.calculator2:id/plus").click();//+的ID
driver.findElementById("com.android.calculator2:id/digit2").click();//2的ID
driver.findElementById("com.android.calculator2:id/equal").click();//=的ID
}
@AfterClass
public void tearDown(){
driver.close();
}
}
代码贴上.........
Default test
Tests run: 0, Failures: 0, Skips: 0
看不到你的代码,我估计是你没有添加测试注释的原因。run = 0 说明你没有执行任何测试
Command duration or timeout: 0 milliseconds 报这个错是因为什么