appium录制脚本和回放
--------------------------------------------如何录制脚本-------------------------------
步骤如下:
(1) 点击录制按钮
![](https://img.haomeiwen.com/i3096511/a200ac0cd1af4a74.png)
(2)点击 【选择元素】按钮
![](https://img.haomeiwen.com/i3096511/d85e2b7027e51aa9.png)
(3)选择 我的柠檬-》右边点击
![](https://img.haomeiwen.com/i3096511/44d8e66a29a6af6f.png)
![](https://img.haomeiwen.com/i3096511/2fd959fc5a89eb17.png)
(4)选择头像-》右边点击
![](https://img.haomeiwen.com/i3096511/5a8b08deeafbd458.png)
(5)点击 用户名 -》点击 发送密钥 -》输入用户名
(6)点击 密码-》点击 发送密钥-》输入密码
(7) 选 登录元素-》右边点击 【点击】
(8)再次点击录制,即停止录制
如何查看全部的录制脚本
![](https://img.haomeiwen.com/i3096511/3ac3e0cd674078cd.png)
上图点击右上角,可以看到全部的脚本,全部脚本如下:
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SampleTest {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("appPackage", "com.lemon.lemonban");
desiredCapabilities.setCapability("appActivity", "com.lemon.lemonban.activity.WelcomeActivity");
URL remoteUrl = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
}
@Test
public void sampleTest() {
MobileElement el1 = (MobileElement) driver.findElementByXPath("//android.widget.FrameLayout[@content-desc=\"我的柠檬\"]/android.widget.ImageView");
el1.click();
MobileElement el2 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/fragment_my_lemon_avatar_title");
el2.click();
MobileElement el3 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/et_mobile");
el3.sendKeys("13323234545");
MobileElement el4 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/et_password");
el4.sendKeys("123456");
MobileElement el5 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/btn_login");
el5.click();
}
@After
public void tearDown() {
driver.quit();
}
}
-----------------------------------------------------如何回放脚本---------------------------------------
把上面的代码copy到eclispe执行,代码copy过去,发现有报错->这是因为需要导入依赖
![](https://img.haomeiwen.com/i3096511/96acedea32dc8017.png)
第一个依赖:java_client
![](https://img.haomeiwen.com/i3096511/7c27dfbdb4e54f74.png)
第二个依赖:Junit
![](https://img.haomeiwen.com/i3096511/e0f85f1bf7d140c8.png)
pom引入依赖之后,代码就不报错了,然后可以运行脚本,看一下效果-》
录制脚本改造如下(就引入了2个包,其他未做修改)
package com.lemon.day01;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
public class RecordTestShell {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("appPackage", "com.lemon.lemonban");
desiredCapabilities.setCapability("appActivity", "com.lemon.lemonban.activity.WelcomeActivity");
URL remoteUrl = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
}
@Test
public void sampleTest() {
MobileElement el1 = (MobileElement) driver
.findElementByXPath("//android.widget.FrameLayout[@content-desc=\"我的柠檬\"]/android.widget.ImageView");
el1.click();
MobileElement el2 = (MobileElement) driver
.findElementById("com.lemon.lemonban:id/fragment_my_lemon_avatar_title");
el2.click();
MobileElement el3 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/et_mobile");
el3.sendKeys("13323234545");
MobileElement el4 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/et_password");
el4.sendKeys("123456");
MobileElement el5 = (MobileElement) driver.findElementById("com.lemon.lemonban:id/btn_login");
el5.click();
}
@After
public void tearDown() {
driver.quit();
}
}
右键 Run as ->JUnit Test (因为脚本里用到的是Junit注解),运行发现脚本报错了
![](https://img.haomeiwen.com/i3096511/fe6412240b8f04f1.png)
这是因为app启动是需要一定时间的,而代码却执行很快-》所以需要加等待(所以录制的脚本不一定可以直接拿过来使用,还是需要进行优化的),优化的部分如下:
![](https://img.haomeiwen.com/i3096511/34f40c3fb7ce3c3d.png)
再重新执行脚本(RecordTestShell.java),成功执行。但是上述脚本中是没有断言的,没有断言的脚本是没有灵魂的
注意:
(1)eclipse执行脚本,夜神模拟器一定要打开,如下图即可
![](https://img.haomeiwen.com/i3096511/b8518b1a9a2198c9.png)
(2)adb connect 要连接上设备
![](https://img.haomeiwen.com/i3096511/fdd7052b330e1aa9.png)
(3)appium要启动(可以不用打开会话器),如下图即可
![](https://img.haomeiwen.com/i3096511/0d1a2ba3317026a4.png)
----------------------------------------Junit切换成Testng--------------------------------------
(1)pom依赖换成testng
![](https://img.haomeiwen.com/i3096511/c7faf13a9bd9e378.png)
(2)代码中的注解换成Testng的
![](https://img.haomeiwen.com/i3096511/d234caa5665be993.png)
(3)再Run as ->Testng test ,可以执行成功
总结:
(1)脚本的录制:使用appium检查器会话进行录制(可以录制java,也可以录制python)
![](https://img.haomeiwen.com/i3096511/dccf7899dc718b36.png)
(2)脚本回放(appium没有回放的功能,需要使用其他的工具,如eclipse):
1)复制录制的脚本
2) 脚本改后缀名.java
3)eclipse创建maven工程,复制java文件到工程当中
4)pom文件引入依赖,1.java-client 2.junit
(3) java-client引入不需要引入selenium
因为:引入java-client.jar(带有selenium-java.java,这里的版本是3.12.0)
![](https://img.haomeiwen.com/i3096511/e1bd304936f449dd.png)
java_client包 Compile Dependencies(编译依赖如下图:)
![](https://img.haomeiwen.com/i3096511/dcd55f54112a5418.png)
![](https://img.haomeiwen.com/i3096511/883bc9dfe5525cd8.png)
![](https://img.haomeiwen.com/i3096511/6cad9db9a1e21ef2.png)
下面开始看一下appium的高级设置
-----------------------------------appium的高级设置------------------------------
日志文件路径:appium服务启动时会有很多日志输出,如果你想将日志保存到一个文件了,在这里就可以设置
日志级别:根据设置的日志级别打印日志,比如设置debug,则会将debug级别以上的日志都会输出出来的
覆盖会话:比如你已经启动了一个会话,这时你又启动了第二个会话,如果勾选了这一项,第一个会话就会被停掉,而只启动第二个会话
记录时间戳:勾选,日志就会有时间戳
![](https://img.haomeiwen.com/i3096511/171d4776df853d85.png)
不勾选,日志就没有时间
![](https://img.haomeiwen.com/i3096511/ea4aad23a5ce4bd7.png)
禁用协议颜色:此项勾选,打印的日志协议紫色就是黑白显示
Android启动端口:
![](https://img.haomeiwen.com/i3096511/c04ed25a199ed185.png)
![](https://img.haomeiwen.com/i3096511/6a361067778bfa6f.png)
Selenderoid端口:已不用了,因为这个端口是只针对安卓4.2以下的版本用到的技术
现在安卓版本都已经5.0以上了
Chromediver端口:app有些页面是web页面,如微信里面的微信公众号,文章,新闻
![](https://img.haomeiwen.com/i3096511/2a1f15698ca043cd.png)
预设:
![](https://img.haomeiwen.com/i3096511/a02ff5e58c7d3a13.png)