51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1761|回复: 2
打印 上一主题 下一主题

Appium 处理滑动

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-2-26 16:18:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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运行的时候并不是全屏,还有系统自带工具栏和边框,一定要避免!!!
  1.   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

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

  14.     /**
  15.      * This Method for swipe down
  16.      *
  17.      * @author Young
  18.      * @param driver
  19.      * @param during
  20.      */
  21.     public void swipeToDown(AndroidDriver driver, int during) {
  22.         int width = driver.manage().window().getSize().width;
  23.         int height = driver.manage().window().getSize().height;
  24.         driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
  25.         // wait for page loading
  26.     }

  27.     /**
  28.      * This Method for swipe Left
  29.      *
  30.      * @author Young
  31.      * @param driver
  32.      * @param during
  33.      */
  34.     public void swipeToLeft(AndroidDriver driver, int during) {
  35.         int width = driver.manage().window().getSize().width;
  36.         int height = driver.manage().window().getSize().height;
  37.         driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
  38.         // wait for page loading
  39.     }

  40.     /**
  41.      * This Method for swipe Right
  42.      *
  43.      * @author Young
  44.      * @param driver
  45.      * @param during
  46.      */
  47.     public void swipeToRight(AndroidDriver driver, int during) {
  48.         int width = driver.manage().window().getSize().width;
  49.         int height = driver.manage().window().getSize().height;
  50.         driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
  51.         // wait for page loading
  52.     }
  53. 复制代码
复制代码
最后来测试一下这几个滑动方法:

复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2018-2-26 16:18:46 | 只看该作者
  1. package com.dbyl.core;

  2. import org.apache.commons.io.FileUtils;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.NoSuchElementException;
  5. import org.openqa.selenium.OutputType;
  6. import org.openqa.selenium.TakesScreenshot;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.remote.CapabilityType;
  9. import org.openqa.selenium.remote.DesiredCapabilities;
  10. import org.testng.Assert;
  11. import org.testng.annotations.AfterClass;
  12. import org.testng.annotations.BeforeClass;
  13. import org.testng.annotations.Test;

  14. import io.appium.java_client.android.AndroidDriver;

  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.net.URL;
  18. import java.util.List;
  19. import java.util.concurrent.TimeUnit;

  20. public class zhiHu {
  21. private AndroidDriver driver;
  22. private boolean isInstall = false;

  23. /**
  24. * @author Young
  25. * @throws IOException
  26. */
  27. public void startRecord() throws IOException {
  28. Runtime rt = Runtime.getRuntime();
  29. // this code for record the screen of your device
  30. rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");

  31. }

  32. @BeforeClass(alwaysRun = true)
  33. public void setUp() throws Exception {
  34. // set up appium

  35. DesiredCapabilities capabilities = new DesiredCapabilities();
  36. capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
  37. capabilities.setCapability("platformName", "Android");
  38. capabilities.setCapability("deviceName", "Android Emulator");
  39. capabilities.setCapability("platformVersion", "4.4");
  40. // if no need install don't add this
  41. if (isInstall) {
  42. File classpathRoot = new File(System.getProperty("user.dir"));
  43. File appDir = new File(classpathRoot, "apps");
  44. File app = new File(appDir, "zhihu.apk");
  45. capabilities.setCapability("app", app.getAbsolutePath());
  46. }
  47. capabilities.setCapability("appPackage", "com.zhihu.android");
  48. // support Chinese
  49. capabilities.setCapability("unicodeKeyboard", "True");
  50. capabilities.setCapability("resetKeyboard", "True");
  51. // no need sign
  52. capabilities.setCapability("noSign", "True");
  53. capabilities.setCapability("appActivity", ".ui.activity.GuideActivity");
  54. driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
  55. capabilities);
  56. startRecord();
  57. }

  58. public void login() {

  59. WebElement loginButton;
  60. if (isLoginPresent(driver, 60)) {
  61. loginButton = driver.findElement(By
  62. .id("com.zhihu.android:id/login"));
  63. loginButton.click();
  64. }

  65. else {
  66. Assert.assertTrue(false);
  67. }

  68. // wait for 20s
  69. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

  70. // find login userName and password editText
  71. List<WebElement> textFieldsList = driver
  72. .findElementsByClassName("android.widget.EditText");
  73. textFieldsList.get(0).sendKeys("seleniumcookies@126.com");
  74. textFieldsList.get(1).sendKeys("cookies123");
  75. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

  76. // find ok button byName
  77. driver.findElementById("android:id/button1").click();
  78. driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

  79. // find keyword 首页 and verify it is display
  80. Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());

  81. }

  82. public boolean isLoginPresent(AndroidDriver driver, int timeout) {
  83. boolean isPresent = new AndroidDriverWait(driver, timeout).until(
  84. new ExpectedCondition<WebElement>() {
  85. public WebElement apply(AndroidDriver d) {
  86. return d.findElement(By
  87. .id("com.zhihu.android:id/login"));
  88. }

  89. }).isDisplayed();
  90. return isPresent;
  91. }

  92. @Test(groups = "swipeTest", priority = 1)
  93. public void swipe() {

  94. // find login button
  95. if (isInstall) {
  96. login();
  97. }
  98. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  99. // find keyword 首页 and verify it is display
  100. Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
  101. snapshot((TakesScreenshot) driver, "zhihu_before_swipe.png");
  102. swipeToUp(driver, 500);
  103. snapshot((TakesScreenshot) driver, "zhihu_after_swipe.png");

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

  105. swipeToDown(driver, 1000);
  106. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  107. List<WebElement> titles = driver
  108. .findElementsById("com.zhihu.android:id/title");
  109. titles.get(0).click();
  110. driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

  111. //swipe to right
  112. swipeToRight(driver, 100);

  113. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  114. // find keyword 首页 and verify it is display
  115. Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
  116. }

  117. /**
  118. * This Method for swipe up
  119. *
  120. * @author Young
  121. * @param driver
  122. * @param during
  123. */
  124. public void swipeToUp(AndroidDriver driver, int during) {
  125. int width = driver.manage().window().getSize().width;
  126. int height = driver.manage().window().getSize().height;
  127. driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);
  128. // wait for page loading
  129. }

  130. /**
  131. * This Method for swipe down
  132. *
  133. * @author Young
  134. * @param driver
  135. * @param during
  136. */
  137. public void swipeToDown(AndroidDriver driver, int during) {
  138. int width = driver.manage().window().getSize().width;
  139. int height = driver.manage().window().getSize().height;
  140. driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
  141. // wait for page loading
  142. }

  143. /**
  144. * This Method for swipe Left
  145. *
  146. * @author Young
  147. * @param driver
  148. * @param during
  149. */
  150. public void swipeToLeft(AndroidDriver driver, int during) {
  151. int width = driver.manage().window().getSize().width;
  152. int height = driver.manage().window().getSize().height;
  153. driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
  154. // wait for page loading
  155. }

  156. /**
  157. * This Method for swipe Right
  158. *
  159. * @author Young
  160. * @param driver
  161. * @param during
  162. */
  163. public void swipeToRight(AndroidDriver driver, int during) {
  164. int width = driver.manage().window().getSize().width;
  165. int height = driver.manage().window().getSize().height;
  166. driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
  167. // wait for page loading
  168. }

  169. @Test(groups = { "profileSetting" }, priority = 2)
  170. public void profileSetting() {

  171. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  172. // find keyword 首页 and verify it is display
  173. Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());

  174. driver.swipe(100, 400, 100, 200, 500);
  175. WebElement myButton = driver.findElement(By
  176. .className("android.widget.ImageButton"));
  177. myButton.click();
  178. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  179. driver.swipe(700, 500, 100, 500, 10);
  180. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  181. List<WebElement> textViews = driver
  182. .findElementsByClassName("android.widget.TextView");
  183. textViews.get(0).click();
  184. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  185. driver.findElementById("com.zhihu.android:id/name").click();
  186. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  187. // wait for 60s if WebElemnt show up less than 60s , then return , until
  188. // 60s

  189. By by = new By.ById("com.zhihu.android:id/showcase_close");

  190. snapshot((TakesScreenshot) driver, "zhihu_showClose.png");
  191. if (isElementPresent(by, 30)) {
  192. driver.findElement(by).click();
  193. }

  194. Assert.assertTrue(driver
  195. .findElementsByClassName("android.widget.TextView").get(0)
  196. .getText().contains("selenium"));

  197. driver.findElementById("com.zhihu.android:id/menu_people_edit").click();
  198. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  199. WebElement intro = driver
  200. .findElementById("com.zhihu.android:id/introduction");
  201. intro.click();
  202. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  203. WebElement content = driver
  204. .findElementById("com.zhihu.android:id/content");
  205. String text = content.getAttribute("text");
  206. content.click();
  207. clearText(text);
  208. content.sendKeys("Appium Test. Create By Young");

  209. driver.findElementById("com.zhihu.android:id/menu_question_done")
  210. .click();

  211. WebElement explanation = driver
  212. .findElementById("com.zhihu.android:id/explanation");
  213. explanation.click();
  214. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  215. content = driver.findElementById("com.zhihu.android:id/content");
  216. text = content.getAttribute("text");
  217. content.click();
  218. clearText(text);
  219. content.sendKeys("Appium Test. Create By Young. This is an appium type hahahahah");

  220. driver.findElementById("com.zhihu.android:id/menu_question_done")
  221. .click();
  222. snapshot((TakesScreenshot) driver, "zhihu.png");

  223. }

  224. /**
  225. * This method for delete text in textView
  226. *
  227. * @author Young
  228. * @param text
  229. */
  230. public void clearText(String text) {
  231. driver.sendKeyEvent(123);
  232. for (int i = 0; i < text.length(); i++) {
  233. driver.sendKeyEvent(67);
  234. }
  235. }

  236. @AfterClass(alwaysRun = true)
  237. public void tearDown() throws Exception {
  238. driver.quit();
  239. }

  240. /**
  241. * This Method create for take screenshot
  242. *
  243. * @author Young
  244. * @param drivername
  245. * @param filename
  246. */
  247. public static void snapshot(TakesScreenshot drivername, String filename) {
  248. // this method will take screen shot ,require two parameters ,one is
  249. // driver name, another is file name

  250. String currentPath = System.getProperty("user.dir"); // get current work
  251. // folder
  252. File scrFile = drivername.getScreenshotAs(OutputType.FILE);
  253. // Now you can do whatever you need to do with it, for example copy
  254. // somewhere
  255. try {
  256. System.out.println("save snapshot path is:" + currentPath + "/"
  257. + filename);
  258. FileUtils
  259. .copyFile(scrFile, new File(currentPath + "\\" + filename));
  260. } catch (IOException e) {
  261. System.out.println("Can't save screenshot");
  262. e.printStackTrace();
  263. } finally {
  264. System.out.println("screen shot finished, it's in " + currentPath
  265. + " folder");
  266. }
  267. }

  268. /**
  269. *
  270. * @param by
  271. * @param timeOut
  272. * @return
  273. */
  274. public boolean isElementPresent(final By by, int timeOut) {
  275. try {
  276. new AndroidDriverWait(driver, timeOut)
  277. .until(new ExpectedCondition<WebElement>() {
  278. public WebElement apply(AndroidDriver d) {
  279. return d.findElement(by);
  280. }

  281. });
  282. return true;
  283. } catch (Exception e) {
  284. return false;
  285. }

  286. }
  287. }
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-10 22:49 , Processed in 0.067052 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表