appium實現截圖和清空EditText

來源:互聯網
上載者:User

標籤:

前些日子,配置好了appium測試環境,至於環境怎麼搭建,參考:http://www.cnblogs.com/tobecrazy/p/4562199.html

                    知乎Android用戶端登陸:http://www.cnblogs.com/tobecrazy/p/4579631.html

在使用appium的過程中,發現一些appium的坑(後邊會詳說)。

 

adb基本命令總結(Android Debug Bridge)

adb 是PC和裝置串連的橋樑,可以通過adb對devices進行相關操作

  • adb devices           列出你的devices
  • adb kill-server         殺掉adb服務(如果裝置串連出問題,可嘗試)
  • adb start-server      重啟adb服務
  • adb shell                進入預設device的Linux shell,可以直接執行Linux命令
  • adb shell screenrecord /sdcard/runCase.mp4  錄製視頻儲存,預設3min,也可以加--time-limit 60限制時間 
  • adb install jd.apk      向裝置安裝app
  • adb pull /sdcard/runCase.mp4 c:// 把手機中檔案copy到電腦
  • adb push C://runCase.mp4 /sdcard/          把電腦中檔案放到手機     

 以上就是一些基本的adb 命令

 

 appium實現

由於我有webdriver 的基礎,理解起來比較easy,appium實際是繼承webdriver的

selenium 中使用的是TakesScreenShot介面getScreenShotAs方法,具體實現如下

 1 /** 2      * This Method create for take screenshot 3      *  4      * @author Young 5      * @param drivername 6      * @param filename 7      */ 8     public static void snapshot(TakesScreenshot drivername, String filename) { 9         // this method will take screen shot ,require two parameters ,one is10         // driver name, another is file name11 12         String currentPath = System.getProperty("user.dir"); // get current work13                                                                 // folder14         File scrFile = drivername.getScreenshotAs(OutputType.FILE);15         // Now you can do whatever you need to do with it, for example copy16         // somewhere17         try {18             System.out.println("save snapshot path is:" + currentPath + "/"19                     + filename);20             FileUtils21                     .copyFile(scrFile, new File(currentPath + "\\" + filename));22         } catch (IOException e) {23             System.out.println("Can‘t save screenshot");24             e.printStackTrace();25         } finally {26             System.out.println("screen shot finished, it‘s in " + currentPath27                     + " folder");28         }29     }

調用: snapshot((TakesScreenshot) driver, "zhihu_showClose.png");

你可以在捕獲異常的時候調用,可以實現錯誤,做測試結果分析很有效

appium清空EditText(一個坑)

在使用appium過程中,發現sendkeys和clear方法並不太好使,只好自己封裝類比手工一個一個刪除

這裡用到keyEvent,具體內容請參考api http://appium.github.io/java-client/

要刪除一段文字,該怎麼做:

1. 擷取文本長度

2. 移動到文本最後

3. 按下刪除按鈕,直到和文本一樣長度

移動到文本最後: 123刪除67

public static final int BACKSPACE 67
public static final int DEL 67

 

public static final int KEYCODE_MOVE_END 123

 實現代碼如下:

/** * 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);}}

  

整個case的代碼貼一下

初始化driver,執行cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4 開始錄製測試視頻

執行login

執行修改知乎的個人介紹

package com.dbyl.core;import org.apache.commons.io.FileUtils;import org.openqa.selenium.By;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;/** * @author Young * @throws IOException */public void startRecord() throws IOException {Runtime rt = Runtime.getRuntime();// this code for record the screen of your devicert.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");}@BeforeClass(alwaysRun = true)public void setUp() throws Exception {// set up appiumFile classpathRoot = new File(System.getProperty("user.dir"));File appDir = new File(classpathRoot, "apps");File app = new File(appDir, "zhihu.apk");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 thiscapabilities.setCapability("app", app.getAbsolutePath());capabilities.setCapability("appPackage", "com.zhihu.android");// support Chinesecapabilities.setCapability("unicodeKeyboard", "True");capabilities.setCapability("resetKeyboard", "True");// no need signcapabilities.setCapability("noSign", "True");capabilities.setCapability("appActivity", ".ui.activity.GuideActivity");driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);startRecord();}@Test(groups = { "login" })public void login() {// find login buttonWebElement loginButton = driver.findElement(By.id("com.zhihu.android:id/login"));loginButton.click();// wait for 20sdriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);// find login userName and password editTextList<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 byNamedriver.findElementById("android:id/button1").click();driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);// find keyword 首頁 and verify it is displayAssert.assertTrue(driver.findElement(By.name("首頁")).isDisplayed());}@Test(groups = { "profileSetting" }, dependsOnMethods = "login")public void profileSetting() {driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);// find keyword 首頁 and verify it is displayAssert.assertTrue(driver.findElement(By.name("首頁")).isDisplayed());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);List<WebElement> showClose = driver.findElementsById("com.zhihu.android:id/showcase_close");if (!showClose.isEmpty()) {snapshot((TakesScreenshot) driver, "zhihu_showClose.png");showClose.get(0).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 nameString currentPath = System.getProperty("user.dir"); // get current work// folderFile scrFile = drivername.getScreenshotAs(OutputType.FILE);// Now you can do whatever you need to do with it, for example copy// somewheretry {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");}}}

  

 這是運行中的兩處:

 

 

 

 

 

 

 

 

 

 

 

appium實現和清空EditText

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.