Appium測試安卓Launcher以滑動表單獲得目標應用,appium安卓
所謂Launcher,指的是安卓的案頭管理程式,所有的應用表徵圖都放在launcher上面。其實這是一個很簡單的例子,只是為了驗證幾點想法而已。
1.實驗目的
做這個實驗的目的有二
- 嘗試下表單滑動函數swipe的使用
- 好奇究竟能不能正常的對安卓的Launcher進行指定package和activity進行測試
2.實驗背景過程是打算使用appium來啟動launcher,然後滑動視窗去擷取在第三個案頭的sdk內建應用”Notes“。如所示
3. 實驗步驟3.1 獲得launcher的package和activity兩個capabilities可以通過HierarchyViewer直接查看獲得
3.2 編碼實現
package majcit.com.AppiumDemo;import io.appium.java_client.android.AndroidDriver;import java.net.URL;import org.junit.Test;import org.junit.After;import org.junit.Before;import org.openqa.selenium.Dimension;import org.openqa.selenium.NoSuchElementException;import org.openqa.selenium.Point;import org.openqa.selenium.WebElement;import org.openqa.selenium.remote.DesiredCapabilities;import static org.hamcrest.Matchers.*;import static org.hamcrest.MatcherAssert.assertThat;/** * Unit test for simple App. */public class LauncherTest { /** * Create the test case * * @param testName name of the test case */private AndroidDriver driver; @Before public void setUp() throws Exception { // set up appium //File classpathRoot = new File(System.getProperty("user.dir")); //File appDir = new File(classpathRoot, "apps"); //File app = new File(appDir, "NotePad.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName","Android"); //capabilities.setCapability("platformVersion", "4.2"); //capabilities.setCapability("platformName", "Android"); //capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "com.miui.home"); capabilities.setCapability("appActivity", "com.miui.home.launcher.Launcher"); //capabilities.setCapability("appActivity", ".NotesList"); //capabilities.setCapability("autoLaunch", "false"); //capabilities.setCapability("noReset", true); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void launchNotePad() throws InterruptedException{ WebElement el = null; WebElement screen = null; Point point = null; Dimension size = null; boolean found = false; int pageCount = 3; //Assume that there are totally 3 screens to be swiped. int xStart = -1; int yStart = -1; int xEnd = -1; int yEnd = -1; //Get the start and end coordinates for swipe Thread.sleep(3000); screen = driver.findElementById("com.miui.home:id/cell_layout"); point = screen.getLocation(); size = screen.getSize(); xEnd = point.getX(); yEnd = point.getY() + size.getHeight()/2; xStart = point.getX() + size.getWidth() - 5; yStart = yEnd; System.out.println("starX:" + xStart +"\nstartY:" + yStart + "\nendX:" + xEnd + "\nendY:" + yEnd); //本來想通過判斷螢幕上的幾個小圓點來判斷究竟有多少個螢幕的,但發覺xPath根本不起效,父目錄感覺根本起不了定位作用,只有最後的//android.widget.ImageView起效,所以一下找出75個元素。 /* List<WebElement> pageImages = driver.findElementsByXPath("//android.view.View/android.widget.LinearLayout/android.widget.ImageView"); assertThat(pageImages.size(),is(3)); for (WebElement e: pageImages) { e.click(); } */ //Swipe all screens till get the expected control int currentPage = 0; while (found == false && currentPage < pageCount) { found = true; currentPage += 1; try { el = driver.findElementByName("Notes"); }catch (NoSuchElementException e) { found = false; System.out.println(e); } if (found == true) break; driver.swipe(xStart, yStart, xEnd, yEnd, 100); Thread.sleep(1000); } assertThat(found,is(true)); assertThat(el,notNullValue()); el.click(); } }步驟說明大概如下:
- 準備好必須的capabilities傳送給appium伺服器端,注意指定app,因為我們不需要重新安裝Launcher
- 找到代表整個螢幕的控制項,然後通過擷取它的location和size屬性來計算出滑動開始和結束的座標。注意開始的座標如果是螢幕的邊界,需要調整下像素(例子中是減去5個像素)以防出錯。
- 通過滑動遍曆每個頁面直到找到目標控制項為止。
手機app測試有什工具盒方法
1、安卓黑箱測試工具
1)包括本身內建的monkey,Monkey是Android中的一個命令列工具,可以運行在模擬器裡或實際裝置中
2) robotium與appium,這些工具用於黑盒的自動化測試。可以在有源碼或者只有APK 的情況下對目標應用進行測試。提供了模仿使用者操作行為的API,比如在某個控制項上點擊,輸入Text
等等。(推舉你可以研究一下這個工具,開源的,我有資料)
2、白盒測試,Android在SDK中整合了JUnit架構。所以,你可以基於JUNIT架構進行安卓的白盒測試代碼開發
3、安卓的測試方法其實與web測試方法大多類似,只是多了一些特殊的測試情境,例如-遍曆測試、故事驅動、適配測試、電量測試、弱網測試、流量測試等
4、相容性測試的話,一是可以利用虛擬機器保證系統間的相容性,但更多的還需要依靠真機測試,因為每個真機的上層API使用和渲染是不太一樣的
如果你想要什麼資料的話或者想一起學習研究的話,可以給我發郵件:test_sunny@hotmail.com