1. 程式人生 > >Appium swip滑動

Appium swip滑動

複製程式碼
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("[email protected]"); 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; } } }

相關推薦

Appium swip滑動

package com.dbyl.core; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import

Appium-Java滑動操作

結束 etc png pstore type 代碼 rem als setup Java滑動操作,通常可以直接使用API中AndroidDriver類中的swipe方法,直接進行調用 swipe(int startx, int starty, int endx, int

Appium頁面滑動連續操作-TouchAction

TouchAction包含一系列操作,例如短按、長按、點選、移動、暫停,由不同的操作可以組成一套動作,使用TouchAction需要先匯入對應的模組: from appium.webdriver.common.touch_action import TouchAction  

Appium模擬滑動軌跡(如手勢解鎖)

final TouchAction gesture = new TouchAction(driver).press(startX, stratY) .moveTo(startX, stratY + height) .moveTo(s

Appium頁面滑動問題

我們在自動化測試的過程中會遇到通過頁面滑動的操作,或者通過滑動來選擇某個值,下面通過三種方法來實現,主要以向上滑動為例子進行講解。 1.swipe()用法 swipe(int st

appium+python自動化24-滑動方法封裝(swipe)【轉載】

start fun optional image device rgs launcher ice oba swipe介紹 1.查看源碼語法,起點和終點四個坐標參數,duration是滑動屏幕持續的時間,時間越短速度越快。默認為None可不填,一般設置500-1000毫秒比較

Python Appium 滑動、點擊等操作

arm sof 使用場景 坐標 args python art icu pyc Python Appium 滑動、點擊等操作 1、手機滑動-swipe # FileName : Tmall_App.py # Author : Adil # DateTi

Appium滑動引導頁-swipe函式

Appium滑動引導頁-swipe函式 基礎函式 獲取螢幕解析度: driver.get_window_size()[‘height’]  #獲取螢幕的高度 driver.get_window_size()[‘width’]   #獲取螢幕的寬度 實

Appium九宮格滑動解鎖

info eight oca ESS 代碼實現 inf appium web element 1、適配各種機型,首先獲取整個解鎖元素的坐標 2、代碼實現 WebElement lockPattern = driver.findElement(By.id("com.

Appium滑動操作

座標:  手機介面,預設左上角為  (0,0)  座標;      橫座標  向右  逐漸增加      縱座標  向下  逐漸增加  # 獲取螢幕的size     size = driver.

Appium移動端自動化測試-常用API-滑動相關操作

1:APP的滑動的概念 如下圖 橫方向為 X軸 豎方向為Y軸 左上側的左邊為(0,0),右下側的最大左邊為螢幕的解析度(例如1080*1920 座標為(1080,1920));頁面左滑動 Y軸座標值不變 X軸座標值從大到小  頁面向上滑動 X軸座標值不變 Y軸座標值從小變大

appium中引導圖等-滑動.通過座標定位

給大家分享一下如何進行座標定位,從而實現滑動的例子: 首先我們的應從場景有很多,比如app首頁的引導圖,例如我們公司的資訊流的下拉重新整理上拉載入.等等,都會用到頁面的滑動. 首先先了解一下什麼是座標滑動: 我們app的座標是在左上角進行的原點定位. 通過針對座標的定

Appium左右、上下滑動(Java)

網上很多文章都說用swipe來左右滑動,你把程式碼一貼,結果報錯,看半天,原來是java-client中swipe早就被廢除了!!!下面介紹一種Java寫法來左右上下滑動: 首先,建立一個Swipe類 import org.openqa.selenium.

appium+python:螢幕滑動功能;簡單實用很流皮

前提環境已搭建;好了直接上程式碼# coding=utf-8 import time,unittest from appium import webdriverdesired_caps = { 'platformName': 'Andr

基於Appium+java進行Android自動化測試:列表定位、用例失敗截圖、長按、滑動、依賴、切換~

一、appium中對元素的定位方法的使用 場景:元素的定位,常利用resource-id、index、name(text)等屬性進行定位。如下圖所示,列表專案的index、resource-id、class、package等屬性均相同,無法利用常用屬性進行區分。直接根據r

appium用adb命令執行滑動操作

直接上程式碼  def exe_adb(command): """ 執行一條adb命令 :param command: 執行的命令 :return: 執行結果

Appium滑動問題研究

JavascriptExecutor js =(JavascriptExecutor) driver; HashMap<String,Double> flickObject =newHashMap<String,Double>(); flickObject.put("startX",

Appium系列之iOS平臺】關於滑動的一些用法

前提: 早在去年JLipps在release Appium1.0的時候,已經是對外宣佈在iOS平臺中將不支援使用swipe和flick的方法了。 但是在1.0版本時,雖說對外宣傳是不支援,實質上還是暗地裡開了後門的,還是能用TouchAction的

appium+ios 指定頁面向左向右向上向下滑動多少:mobile:dragFromToForDuration

使用 WebDriverAgent/XCTest Backend 進行iOS自動化手勢操作 很可惜,蘋果官方的 XCTest 框架本身並不支援 TouchAction 介面實現的 W3C 標準。儘管如此,XCTest 提供了非常豐富的手勢操作,這些操作都是 iOS 平

一套appium 滑動方法的封裝,滑動方向及滑動次數

appium做自動化測試時,經常需要用到滑動螢幕功能,我這裡封裝一個支援前後左右滑動,且支援設定滑動次數,show me your code ,還是看程式碼吧 public enum ORIENTATION { UP(8), DOWN(2), LEFT(