前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html

                    知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html

appium实现截图和清空EditText:http://www.cnblogs.com/tobecrazy/p/4592405.html


Appium 处理滑动

appium 处理滑动的方法是 swipe(int start-x, int start-y, int end-x, int end-y, int during) - Method in class io.appium.java_client.AppiumDriver
此方法共有5个参数,都是整形,依次是起始位置的x y坐标和终点位子的x y坐标和滑动间隔时间,单位毫秒
坐标是指:屏幕左上角为坐标系原点(0,0),屏幕分辨率为终点坐标,比如你手机分辨率1080*1920,那么该手机坐标系的终点是(1080*1920)
,每个元素都在坐标系中,可以用坐标系定位元素。
          
比如这个登陆按钮在坐标系中的位置是起始点 + 终点(32,1040) (351,1152)

向上滑动

向上滑动,就是从下到上,那么怎么进行从下到上的滑动呢?
按照之前理解的坐标系,从上到下滑动,一般是垂直滑动,也就是X轴不变,Y轴从大变小的过程
我们可以采取Y轴固定在屏幕正当中,Y轴滑动屏幕的1/2,Y轴的起始位置为屏幕的3/4,滑动终点为Y轴的1/4(尽量避免使用坐标系的起点和终点),滑动时间为1s,如果滑动时间过短,将会出现一次滑动多页。
需要提醒的是,很多app运行的时候并不是全屏,还有系统自带工具栏和边框,一定要避免!!!

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        // find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
snapshot((TakesScreenshot) driver, "zhihu_before_swipe.png");
int width=driver.manage().window().getSize().width;
int height=driver.manage().window().getSize().height;
driver.swipe(width/2,height*3/4, width/2,height/4, 1000);
//wait for page loading snapshot((TakesScreenshot) driver, "zhihu_after_swipe.png");
 可以看出 ,为了让apppium 更好的兼容不同分辨率的设备,在执行滑动前先获取屏幕的分辨率
接下来就可以封装四个滑动的方法:
/**
* This Method for swipe up
*
* @author Young
* @param driver
* @param during
*/
public void swipeToUp(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);
// wait for page loading
} /**
* This Method for swipe down
*
* @author Young
* @param driver
* @param during
*/
public void swipeToDown(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
// wait for page loading
} /**
* This Method for swipe Left
*
* @author Young
* @param driver
* @param during
*/
public void swipeToLeft(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
// wait for page loading
} /**
* This Method for swipe Right
*
* @author Young
* @param driver
* @param during
*/
public void swipeToRight(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
// wait for page loading
}

最后来测试一下这几个滑动方法:
package com.dbyl.core;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
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.android.AndroidDriver; import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit; public class zhiHu {
private AndroidDriver driver;
private boolean isInstall = false; /**
* @author Young
* @throws IOException
*/
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 {
// set up appium DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
// if no need install don't add this
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());
}
capabilities.setCapability("appPackage", "com.zhihu.android");
// support Chinese
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
// no need sign
capabilities.setCapability("noSign", "True");
capabilities.setCapability("appActivity", ".ui.activity.GuideActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
startRecord();
} public void login() { WebElement loginButton;
if (isLoginPresent(driver, 60)) {
loginButton = driver.findElement(By
.id("com.zhihu.android:id/login"));
loginButton.click();
} else {
Assert.assertTrue(false);
} // wait for 20s
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // find login userName and password editText
List<WebElement> textFieldsList = driver
.findElementsByClassName("android.widget.EditText");
textFieldsList.get(0).sendKeys("seleniumcookies@126.com");
textFieldsList.get(1).sendKeys("cookies123");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // find ok button byName
driver.findElementById("android:id/button1").click();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS); // find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed()); } public boolean isLoginPresent(AndroidDriver driver, int timeout) {
boolean isPresent = new AndroidDriverWait(driver, timeout).until(
new ExpectedCondition<WebElement>() {
public WebElement apply(AndroidDriver d) {
return d.findElement(By
.id("com.zhihu.android:id/login"));
} }).isDisplayed();
return isPresent;
} @Test(groups = "swipeTest", priority = 1)
public void swipe() { // find login button
if (isInstall) {
login();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
snapshot((TakesScreenshot) driver, "zhihu_before_swipe.png");
swipeToUp(driver, 500);
snapshot((TakesScreenshot) driver, "zhihu_after_swipe.png"); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); swipeToDown(driver, 1000);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> titles = driver
.findElementsById("com.zhihu.android:id/title");
titles.get(0).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); //swipe to right
swipeToRight(driver, 100); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
} /**
* This Method for swipe up
*
* @author Young
* @param driver
* @param during
*/
public void swipeToUp(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);
// wait for page loading
} /**
* This Method for swipe down
*
* @author Young
* @param driver
* @param during
*/
public void swipeToDown(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
// wait for page loading
} /**
* This Method for swipe Left
*
* @author Young
* @param driver
* @param during
*/
public void swipeToLeft(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
// wait for page loading
} /**
* This Method for swipe Right
*
* @author Young
* @param driver
* @param during
*/
public void swipeToRight(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
// wait for page loading
} @Test(groups = { "profileSetting" }, priority = 2)
public void profileSetting() { driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed()); driver.swipe(100, 400, 100, 200, 500);
WebElement myButton = driver.findElement(By
.className("android.widget.ImageButton"));
myButton.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.swipe(700, 500, 100, 500, 10);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> textViews = driver
.findElementsByClassName("android.widget.TextView");
textViews.get(0).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.findElementById("com.zhihu.android:id/name").click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // wait for 60s if WebElemnt show up less than 60s , then return , until
// 60s By by = new By.ById("com.zhihu.android:id/showcase_close"); snapshot((TakesScreenshot) driver, "zhihu_showClose.png");
if (isElementPresent(by, 30)) {
driver.findElement(by).click();
} Assert.assertTrue(driver
.findElementsByClassName("android.widget.TextView").get(0)
.getText().contains("selenium")); driver.findElementById("com.zhihu.android:id/menu_people_edit").click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement intro = driver
.findElementById("com.zhihu.android:id/introduction");
intro.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement content = driver
.findElementById("com.zhihu.android:id/content");
String text = content.getAttribute("text");
content.click();
clearText(text);
content.sendKeys("Appium Test. Create By Young"); driver.findElementById("com.zhihu.android:id/menu_question_done")
.click(); WebElement explanation = driver
.findElementById("com.zhihu.android:id/explanation");
explanation.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
content = driver.findElementById("com.zhihu.android:id/content");
text = content.getAttribute("text");
content.click();
clearText(text);
content.sendKeys("Appium Test. Create By Young. This is an appium type hahahahah"); driver.findElementById("com.zhihu.android:id/menu_question_done")
.click();
snapshot((TakesScreenshot) driver, "zhihu.png"); } /**
* This method for delete text in textView
*
* @author Young
* @param text
*/
public void clearText(String text) {
driver.sendKeyEvent(123);
for (int i = 0; i < text.length(); i++) {
driver.sendKeyEvent(67);
}
} @AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
} /**
* This Method create for take screenshot
*
* @author Young
* @param drivername
* @param filename
*/
public static void snapshot(TakesScreenshot drivername, String filename) {
// this method will take screen shot ,require two parameters ,one is
// driver name, another is file name String currentPath = System.getProperty("user.dir"); // get current work
// folder
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy
// somewhere
try {
System.out.println("save snapshot path is:" + currentPath + "/"
+ filename);
FileUtils
.copyFile(scrFile, new File(currentPath + "\\" + filename));
} catch (IOException e) {
System.out.println("Can't save screenshot");
e.printStackTrace();
} finally {
System.out.println("screen shot finished, it's in " + currentPath
+ " folder");
}
} /**
*
* @param by
* @param timeOut
* @return
*/
public boolean isElementPresent(final By by, int timeOut) {
try {
new AndroidDriverWait(driver, timeOut)
.until(new ExpectedCondition<WebElement>() {
public WebElement apply(AndroidDriver d) {
return d.findElement(by);
} });
return true;
} catch (Exception e) {
return false;
} }
}

整个过程会按照先向下,向上,向右滑动。

有了这个滑动方法,能不能做一些更高级的事?

答案将在明天揭晓,swipe

appium 滑动的更多相关文章

  1. appium滑动操作(向上、向下、向左、向右)

    appium滑动操作(向上滑动.向下滑动.向左滑动.向右滑动) 测试app:今日头条apk 测试设备:夜游神模拟器 代码如下: 先用x.y获取当前的width和height def getSize() ...

  2. Python Appium 滑动、点击等操作

    Python Appium 滑动.点击等操作 1.手机滑动-swipe # FileName : Tmall_App.py # Author : Adil # DateTime : 2018/3/25 ...

  3. Appium 滑动界面swipe用法

    Appium 滑动API:Swipe(int start x,int start y,int end x,int y,duration) 解释:int start x-开始滑动的x坐标, int st ...

  4. appium滑动

    在app应用日常使用过程中,会经常用到在屏幕滑动操作.如刷朋友圈上下滑操作.浏览图片左右滑动操作等.在自动化脚本该如何实现这些操作呢? 在Appium中模拟用户滑动操作需要使用swipe方法,该方法定 ...

  5. Appium滑动函数:Swipe()

    Appium处理滑动方法是swipe 滑动API:Swipe(int start x,int start y,int end x,int y,duration) 解释: int start x-开始滑 ...

  6. Appium 滑动踩坑记

    前言 对于不同java-client版本,很多的API已经产生大的变化,所以一些API大家会发现已经失效或者使用方式发生了变化,滑动就是其中一项,这篇文章对滑动在不同的java-client版本以及不 ...

  7. appium 滑动封装

    #获得机器屏幕大小x,y def getSize():     x = dr.get_window_size()['width']     y = dr.get_window_size()['heig ...

  8. Appium for iOS setup

    windows下appium设置 之前研究了一段时间的appium for native app 相应的总结如下:                                           ...

  9. appium for hybrid app 处理webview

    之前研究了一段时间的appium for native app 相应的总结如下:                                            appium测试环境搭建 :ht ...

随机推荐

  1. iOS开发中遇到的错误整理 - 集成第三方框架时,编译后XXX头文件找不到

    iOS编译报错-XXX头文件找不到 错误出现的情况: 自己在继承第三方的SDK的时候,明明导入了头文件,但是系统报错,提示头文件找不到 解决方法 既然系统找不到,给他个具体路径,继续找去! 路径就填写 ...

  2. ES6深入学习记录(一)class方法相关

    今天学习class相关的一些使用方法,着重在于class extends class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要清晰和方便很多. 上面的代码定义了一 ...

  3. [HTML5] ArrayBuffer与类型化数组

    写在前面 这是关于JS二进制操作的第三篇博客,前两篇详见: [HTML5] Blob对象 [HTML5] FileReader对象 此前从宏观角度介绍了如何通过JS创建一个二进制对象,并介绍了如何将本 ...

  4. 逻辑回归 Logistic Regression

    逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...

  5. Javascript动态执行JS(new Function与eval比较)

    new Function与eval可以动态执行JS,只要把拼接好的JS方法,然后以字符串的形式传入到这两个函数,可以执行,其中new Function用在模板引擎比较多. 用 Function 类直接 ...

  6. 简析TCP的三次握手与四次分手

    TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文章时,我想你也知道TCP的概念了,想要更深入的了解TCP的工作,我们就继续.它只是一个超级麻烦的协议,而它又是互联网的基础,也 ...

  7. linux 查找文件或目录

    find / -maxdepth 2 -name "vmware*"在根目录/ 2层深度下搜索以vmware打头的文件或者目录

  8. C# HttpWebRequest获取COOKIES

    C# HttpWebRequest获取COOKIES byte[] bytes = Encoding.Default.GetBytes(_post); CookieContainer myCookie ...

  9. 关于LESS

    LESS 是动态的样式表语言,通过简洁明了的语法定义,使编写 CSS 的工作变得非常简单. 翻译成大白话:写CSS算是体力活,并没有编程的感觉,不给前端人员装逼的机会,于是就搞了这玩意,相当于编程写C ...

  10. 第二章 --- 关于Javascript 设计模式 之 策略模式

    这一章节里面,我们会主要的针对JavaScript中的策略模式进行理解和学习 一.定义 策略模式: 定义一系列的算法,把他们封装起来,并且是他们可以相互替换. (这样的大的定义纲领,真的不好理解,特别 ...