5個最佳的Android測試框架

谷歌的Android生態系統正在不斷地迅速擴張。有證據代表,新的移動OEM正在攻陷世界的每個角落,不一樣的屏幕尺寸、ROM /固件、芯片組以及等等等等,層出不窮。因而乎,對於Android開發人員而言,處理存儲碎片變得愈來愈困窘。html

不過幸運的是,Android(還有iOS)開發人員能夠無限制地訪問一些先進的基於雲的解決方案,如Testdroid Cloud,就能夠在大規模的真實設備上執行自動化測試以確保質量,贊吧。此外,不一樣的Android測試框架的出現也大大減輕了Android開發人員的負擔。android

今天,咱們就要說說5款最經常使用的Android測試框架,而且每一個框架都給出了基本的代碼示例。web

1.Robotium

不能否認,Robotium曾是Android世界之初使用最普遍的Android測試框架,風靡一時。因爲它與Android有着類似的Selenium,因此它可以使得API的測試變得簡單起來。編程

Robotium是一個擴展於JUnit的開源庫,運用多種有用的方法來支持Android UI測試。它提供的強大的自動化黑箱測試範例,可用於Android應用(原生的和混合的)和web測試。只要源代碼容許,你就能夠經過Robotium寫功能、系統和驗收測試方案,以及測試應用。bash

Robotium的代碼示例:app

// Public void for the operation
public void testRecorded() throws Exception {
// Wait for the text 'Hello!' to be shown for newbie
if (solo.waitForText("Hello!")) {
// R class ID identifier for 'Sign in' - and click it
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.sign_in"));
// R class ID identifier for entering username
solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.login_username"),"username");
// R class ID identifier for entering password
solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.login_password"),"password");
// R class ID identifier for clicking log in
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.login_login"));
// Wait until log in is done
solo.waitForActivity("HomeTabActivity");
}
// Activate the text field to compose a tweet
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.menu_compose_tweet"));
// Type the tweet
solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.edit"), "Testdroid");
// Tweeting!
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.composer_post"));
}

爲了給你們提供便捷,還有一個用Robotium構建的用於測試腳本建立的一個很是棒的記錄工具——Testdroid Recorder。當你在真實設備上執行實際行動時,它能夠記錄你的每個步驟和每個行爲,並轉換成JavaScript,以便於你進一步的修改。composer

而且,你還能夠全權下載和使用它的擴展庫——ExtSolo,它裏面包含了多種尚未被歸入到Robotium中的實用方法,例如:框架

  • 支持任意分辨率的x、Y點擊自動縮放
  • 多路徑拖動
  • 測試故障時自動截圖
  • 模擬地點
  • 更改設備語言
  • 控制WiFi鏈接

官方網站:https://code.google.com/p/robotium/編程語言

2.uiautomator

雖然Robotium是一個很好的測試框架,可是uiautomator能讓你在測試Android應用和Android遊戲時作得更多。谷歌的測試框架容許你在一個或多個設備上測試原生Android應用的用戶界面(UI)。Uiautomator的另外一個優勢是,它運行的JUnit測試用例是有特殊權限的,這意味着測試用例能夠跨越不一樣的進程。它還提供了五種不一樣的類給開發人員使用:ide

com.android.uiautomator.core.UiCollection;
com.android.uiautomator.core.UiDevice;
com.android.uiautomator.core.UiObject;
com.android.uiautomator.core.UiScrollable;
com.android.uiautomator.core.UiSelector

遺憾的是,uiautomator只能工做於API16或更高級別的Android設備上。它的另外一個缺點是不支持web視圖,也沒有辦法直接訪問Android對象。

uiautomator的代碼示例:

// Public void for the operation
public void testSignInAndTweet() throws Exception {
// Starting application:
getUiDevice().wakeUp(); // Press Home button to ensure we're on homescreen
getUiDevice().pressHome(); // Select 'Apps' and click button
new UiObject(new UiSelector().description("Apps")).click(); // Select 'Twitter' and click
new UiObject(new UiSelector().text("Twitter")).click(); // Locate and select 'Sign in'
UiSelector signIn = new UiSelector().text("Sign In"); // If button is available, click
UiObject signInButton = new UiObject(signIn);
if (signInButton.exists()) {
signInButton.click(); // Set the username
new UiObject(new
UiSelector().className("android.widget.EditText").instance(0)).setText("username");
new UiObject(new
UiSelector().className("android.widget.EditText").instance(1)).setText("password");
new UiObject(new UiSelector().className("android.widget.Button").
text("Sign In").instance(0)).click(); // Wait Sign in progress window
getUiDevice().waitForWindowUpdate(null, 2000); // Wait for main window
getUiDevice().waitForWindowUpdate(null, 30000);
}
new UiObject(new UiSelector().description("New tweet")).click(); // Typing text for a tweet
new UiObject(new UiSelector().className("android.widget.LinearLayout").instance(8)).
setText("Awesome #Testdroid!"); // Tweeting!
new UiObject(new UiSelector().text("Tweet")).click();

官方網站:http://developer.android.com/tools/help/uiautomator/index.html

3.Espresso

Espresso是由Google開源的一款最新的Android自動化測試框架,有助於於開發人員和測試人員錘鍊出中意的用戶界面。Espresso的API體積小、可預見、簡單易學,構建在Android儀表框架的基礎上。使用它,能讓你快速編寫出簡潔可靠的Android UI測試。它支持API level 8級(Froyo)、10(Gingerbread),和15(Ice Cream Sandwich)及後續。

一方面它至關可靠,由於和UI線程是同步的,另外一方面又很是之快,由於沒有任何睡眠的必要(當某個毫秒,應用程序空轉時,運行測試)。不過它一樣不支持web視圖。

Espresso的代碼示例:

public void testEspresso() {
// Check if view with the text 'Hello.' is shown
onView(withText("Hello.")).check(matches(isDisplayed()));
// R class ID identifier for 'Sign in' - and click it
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/sign_in", null, null))).perform(click());
// R class ID identifier for entering username
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/login_username", null, null))).perform((typeText("username")));
// R class ID identifier for entering password
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/login_password", null, null))).perform((typeText("password")));
// R class ID identifier for clicking log in
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/login_login", null, null))).perform(click());
// Activate the text field to compose a tweet
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/menu_compose_tweet", null, null))).perform(click());
// Type the tweet
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/edit", null, null))).perform((typeText(」#Testdroid")));
// Tweeting!
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/composer_post", null, null))).perform(click());
}

官方網站:https://code.google.com/p/android-test-kit/wiki/Espresso

4.Calabash

Calabash是一款跨平臺的自動化測試框架,支持Android和iOS原生和混合的應用程序。Calabash易於理解的語法,使得即便是非技術人員也能夠在這兩個移動平臺上爲app建立和執行自動化驗收測試。Calabash的測試描述於Cucumber,而後在運行時轉化爲Robotium或Frank。它支持約80種不一樣的天然語言指令(控制器),而且可使用Ruby和Java實現新的控制器。

Calabash的代碼示例:

Feature: Login feature
Scenario: As a valid user I can log into my app
I wait for text "Hello"
Then I press view with id "Sign in"
Then I enter text "username" into "login_username"
Then I enter text "password" into "login_password"
Then I wait for activity "HomeTabActivity"
Then I press view with id "menu_compose_tweet"
Then I enter text "Testdroid" into field with id "edit"
Then I press view with id "composer_post"

官方網站:http://calaba.sh/

5.Appium

Appium是一款移動的自動化測試框架(和工具),支持iOS和Android原生和混合的移動Web應用程序。它內部使用的JSONWireProtocol經過Selenium的WebDriver,來與iOS和Android應用進行交互。它經過uiautomator(API level 16或更高)和Seledroid(API level 低於16)支持Android,經過UI Automation支持iOS,還有Android和iOS都支持的移動web如Selenium driver。

Appium的最大優勢在於你幾乎能夠用任意一種編程語言(例如,Java、Objective-C、JavaScript、PHP、Ruby、Python和C#等)來編寫Appium腳本而沒必要選擇工具,兼容最重要的平臺(Android和iOS)而沒必要安裝和配置設備適應測試等等。而且,若是你熟悉Selenium的話,那麼使用Appium用於移動app測試對你而言將是垂手可得的一件事。由於它們使用相同的WebDriver,而且以一樣的方式使用DesiredCapabilities。因此Appium與Selenium在配置應用程序運行時有諸多類似之處。

Appium的代碼示例:

# wait for hello
sleep(3)
textFields = driver.find_elements_by_tag_name('textField')
assertEqual(textFields[0].get_attribute("value"), "Hello")
# click sign-in button
driver.find_elements_by_name('Sign in')[0].click()
# find the text fields again, and enter username and password
textFields = driver.find_elements_by_tag_name('textField')
textFields[0].send_keys("twitter_username")
textFields[1].send_keys("passw0rd")
# click the Login button (the first button in the view)
driver.find_elements_by_tag_name('button')[0].click()
# sleep
sleep(3)
# click the first button with name "Compose"
driver.find_elements_by_name('Compose')[0].click()
# type in the tweet message
driver.find_elements_by_tag_name('textField')[0].send_keys(」#Testdroid is awesome!")
# press the Send button
driver.find_elements_by_name('Send')[0].click()
# exit
driver.quit()

官方網站:http://appium.io/

總結

以上就是咱們列出的5款最棒的測試框架,可用於平常的Android構建,創立和修改。固然,每一種框架都有其優點和缺陷。Appium能夠同時測試你的Android和iOS版本。但若是你是一個忠實的Android開發人員只開發安卓版本的app,那麼,使用Robotium就很不錯的。Testdroid Recorder還可爲咱們在生成測試腳本節省大量的時間和金錢(這是免費的哦!)。所以,好好思考下你的測試需求——功能測試、兼容性測試、UI測試等等——而後爲本身選取最適合和最佳的Android測試框架。

相關文章
相關標籤/搜索