51Testing软件测试论坛
标题:
第一个Appium(java)例子
[打印本页]
作者:
马琰的春天
时间:
2018-4-4 17:11
标题:
第一个Appium(java)例子
第一个Appium(java)例子
环境搭建好了后,可以开始写测试脚本了,这里以知乎的客户端为测试对象:
1. 新建Java工程,名字为Appium_zhihu_demo,因为我是用zhihu.apk做测试的
[attach]112628[/attach]
2. 在工程下新建apps和libs文件夹,apps文件夹用来放apk包
[attach]112629[/attach]
[attach]112630[/attach]
3. 配置build path,选择project->右键build path->Configure Build Path…或者直接选择Add
ExternalArchives…
[attach]112631[/attach]
[attach]112632[/attach]
注:我学习的时候参考的资料上是添加的java-client-1.2.1.jar,但后来运行一些脚本的时候有问题,自己又
添加了高版本java-client-3.1.0.jar包。
4. 添加jar包完成后,就可以开始新建包->新建java class了
[attach]112633[/attach]
[attach]112634[/attach]
5. 开始编写脚本,这里我们测试知乎的注册功能,代码如下(手机号码,登录密码等注册信息输入真实
的即可运行成功):
右键Run as TestNG,运行截图如下:
[attach]112635[/attach]
[attach]112636[/attach]
eclipse 控制台信息:
[attach]112637[/attach]
至此,第一个Appium 自动化测试用例完成,测试功能为注册界面的交互。
代码如下:
[java] view plain copy
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
public class AppiumZhihuRegister {
public AppiumDriver driver;
private boolean isInstall = false;
public void startRecord() throws IOException {
Runtime rt = Runtime.getRuntime();
// this code for record the screen of your device
rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");
}
@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
//启动appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","lge-nexus_4-005475cbccd279d4");
capabilities.setCapability("automationName","Appium");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("platformVersion","4.4.2");
//配置测试apk
capabilities.setCapability("appPackage", "com.zhihu.android");
capabilities.setCapability("appActivity", ".app.ui.activity.MainActivity ");
capabilities.setCapability("sessionOverride", true); //每次启动时覆盖session,否则第二次后运行会报错不能新建session
capabilities.setCapability("unicodeKeyboard", true); //设置键盘
capabilities.setCapability("resetKeyboard", false); //设置默认键盘为appium的键盘
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//如果真机设备已经安装知乎,则不需要重新安装
if (isInstall) {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "zhihu.apk");
capabilities.setCapability("app", app.getAbsolutePath());
}
startRecord();
}
@Test
public void loginWithMicroBlog() throws InterruptedException {
// swipe to right
driver.findElementById("com.zhihu.android:id/register_btn").click();
driver.findElementById("com.zhihu.android:id/username").sendKeys("手机号");
driver.findElementById("com.zhihu.android:id/password").sendKeys("登录密码");
driver.findElementById("com.zhihu.android:id/fullname").sendKeys("用户昵称");
WebElement NxtBtn=driver.findElementById("com.zhihu.android:id/btn_progress");
Assert.assertEquals(NxtBtn.isEnabled(), true);
Thread.sleep(2000);
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
}
}
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2