1、环境配置
1.1打开夜神模拟器,将下载好的apk文件拖拽至模拟器首页面 在浏览器可下载 1.2打开Appium服务 1.3打开dos窗口,输入命令,查看连接的设备,确保模拟器成功连接
2、在Eclipse中编写代码
2.1创建项目,引入jar包 2.2编写代码 在存放测试用例的类中(主要测试在推荐页面上下滑动)
package com.bili;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
public class TestCase{
DesiredCapabilities dc;
AppiumDriver driver;
TouchAction action;
@Before
public void setUp(){
action = new TouchAction(driver);
dc = new DesiredCapabilities();
dc.setCapability("deviceName", "127.0.0.1:62001");
dc.setCapability("platformName", "Android");
dc.setCapability("appPackage", "tv.danmaku.bili");
dc.setCapability("appActivity", ".MainActivityV2");
}
@Test
public void testBIili() throws Exception{
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),dc);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("要开始刷视频啦");
driver.findElement(By.id("tv.danmaku.bili:id/agree")).click();
System.out.println("开始滑动啦");
Dimension dimension = driver.manage().window().getSize();
int width = dimension.getWidth();
int height = dimension.getHeight();
for(int i=1;i<=5;i++){
driver.swipe(width/2, height * 3/4 , width/2, height/4, 1000);
}
}
@After
public void tearDown(){
driver.quit();
}
}
3、结果截图:
等待展示页面 滑动过程中: 左右滑动和上下滑动道理一样,就是固定y坐标或固定x坐标 在执行测试类中
package com.bili;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestCase1.class);
for(Failure failure:result.getFailures()){
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
} }
另外点击热门,查看下面的小菜单类似banner图,是左右滑动
driver.findElementByAndroidUIAutomator("new UiSelector().text(\"热门\")").click();
Thread.sleep(1000);
Dimension dimension = driver.manage().window().getSize();
int width = dimension.getWidth();
int height = dimension.getHeight();
driver.swipe(width*3/4, 300, width/4, 300, 1000);
Thread.sleep(1000);
driver.swipe(width/4, 300, width*3/4, 300, 1000);
Thread.sleep(1000);
滑动功能,做个记录:
Dimension dimension = driver.manage().window().getSize();
int width = dimension.getWidth();
int height = dimension.getHeight();
driver.swipe(width*3/4, 300, width/4, 300, 1000);
|