谷歌的Android生態(tài)系統(tǒng)正在不斷地迅速擴(kuò)張。有證據(jù)表明,新的移動(dòng)OEM正在攻陷世界的每一個(gè)角落,不同的屏幕尺寸、ROM /固件、芯片組以及等等等等,層出不窮。于是乎,對(duì)于Android開發(fā)人員而言,處理存儲(chǔ)碎片變得越來越困窘。
不過幸運(yùn)的是,Android(還有iOS)開發(fā)人員可以無限制地訪問一些先進(jìn)的基于云的解決方案,如Testdroid Cloud,就可以在大規(guī)模的真實(shí)設(shè)備上執(zhí)行自動(dòng)化測試以確保質(zhì)量,贊吧。此外,不同的Android測試框架的出現(xiàn)也大大減輕了Android開發(fā)人員的負(fù)擔(dān)。
今天,我們就要說說5款常用的Android測試框架,并且每個(gè)框架都給出了基本的代碼示例。
1.Robotium
不可否認(rèn),Robotium曾是Android世界之初使用廣泛的Android測試框架,風(fēng)靡一時(shí)。由于它與Android有著相似的Selenium,所以它能夠使得API的測試變得簡單起來。
Robotium是一個(gè)擴(kuò)展于JUnit的開源庫,運(yùn)用多種有用的方法來支持Android UI測試。它提供的強(qiáng)大的自動(dòng)化黑箱測試范例,可用于Android應(yīng)用(原生的和混合的)和web測試。只要源代碼允許,你就可以通過Robotium寫功能、系統(tǒng)和驗(yàn)收測試方案,以及測試應(yīng)用。
Robotium的代碼示例:
// 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..android.R.id.sign_in"));
// R class ID identifier for entering username
solo.enterText((EditText) solo.findViewById("com..android.R.id.login_username"),"username");
// R class ID identifier for entering password
solo.enterText((EditText) solo.findViewById("com..android.R.id.login_password"),"password");
// R class ID identifier for clicking log in
solo.clickOnView(solo.findViewById("com..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..android.R.id.menu_compose_tweet"));
// Type the tweet
solo.enterText((EditText) solo.findViewById("com..android.R.id.edit"), "Testdroid");
// Tweeting!
solo.clickOnView(solo.findViewById("com..android.R.id.composer_post"));
}
為了給大家提供便捷,還有一個(gè)用Robotium構(gòu)建的用于測試腳本創(chuàng)建的一個(gè)非常棒的記錄工具——Testdroid Recorder。當(dāng)你在真實(shí)設(shè)備上執(zhí)行實(shí)際行動(dòng)時(shí),它可以記錄你的每一個(gè)步驟和每一個(gè)行為,并轉(zhuǎn)換成JavaScript,以便于你進(jìn)一步的修改。
并且,你還可以全權(quán)下載和使用它的擴(kuò)展庫——ExtSolo,它里面包含了多種還沒有被納入到Robotium中的實(shí)用方法,例如:
支持任意分辨率的x、Y點(diǎn)擊自動(dòng)縮放
多路徑拖動(dòng)
測試故障時(shí)自動(dòng)截圖
模擬地點(diǎn)
更改設(shè)備語言
控制WiFi連接
官方網(wǎng)站:https://code..com/p/robotium/
2.uiautomator
雖然Robotium是一個(gè)很好的測試框架,但是uiautomator能讓你在測試Android應(yīng)用和Android游戲時(shí)做得更多。谷歌的測試框架允許你在一個(gè)或多個(gè)設(shè)備上測試原生Android應(yīng)用的用戶界面(UI)。Uiautomator的另一個(gè)優(yōu)點(diǎn)是,它運(yùn)行的JUnit測試用例是有特殊權(quán)限的,這意味著測試用例可以跨越不同的進(jìn)程。它還提供了五種不同的類給開發(fā)人員使用:
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或更高級(jí)別的Android設(shè)備上。它的另一個(gè)缺點(diǎn)是不支持web視圖,也沒有辦法直接訪問Android對(duì)象。
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();
官方網(wǎng)站:http://developer.android.com/tools/help/uiautomator/index.html
3.Espresso
Espresso是由Google開源的一款新的Android自動(dòng)化測試框架,有助于于開發(fā)人員和測試人員錘煉出中意的用戶界面。Espresso的API體積小、可預(yù)見、簡單易學(xué),構(gòu)建在Android儀表框架的基礎(chǔ)上。使用它,能讓你快速編寫出簡潔可靠的Android UI測試。它支持API level 8級(jí)(Froyo)、10(Gingerbread),和15(Ice Cream Sandwich)及后續(xù)。
一方面它相當(dāng)可靠,因?yàn)楹蚒I線程是同步的,另一方面又非常之快,因?yàn)闆]有任何睡眠的必要(當(dāng)某個(gè)毫秒,應(yīng)用程序空轉(zhuǎn)時(shí),運(yùn)行測試)。不過它同樣不支持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..android:id/sign_in", null, null))).perform(click());
// R class ID identifier for entering username
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com..android:id/login_username", null, null))).perform((typeText("username")));
// R class ID identifier for entering password
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com..android:id/login_password", null, null))).perform((typeText("password")));
// R class ID identifier for clicking log in
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com..android:id/login_login", null, null))).perform(click());
// Activate the text field to compose a tweet
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com..android:id/menu_compose_tweet", null, null))).perform(click());
// Type the tweet
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com..android:id/edit", null, null))).perform((typeText(”#Testdroid")));
// Tweeting!
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com..android:id/composer_post", null, null))).perform(click());
}
官方網(wǎng)站:https://code..com/p/android-test-kit/wiki/Espresso
4.Calabash
Calabash是一款跨平臺(tái)的自動(dòng)化測試框架,支持Android和iOS原生和混合的應(yīng)用程序。Calabash易于理解的語法,使得即使是非技術(shù)人員也可以在這兩個(gè)移動(dòng)平臺(tái)上為app創(chuàng)建和執(zhí)行自動(dòng)化驗(yàn)收測試。Calabash的測試描述于Cucumber,然后在運(yùn)行時(shí)轉(zhuǎn)化為Robotium或Frank。它支持約80種不同的自然語言指令(控制器),并且可以使用Ruby和Java實(shí)現(xiàn)新的控制器。
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"
官方網(wǎng)站:http://calaba.sh/
5.Appium
Appium是一款移動(dòng)的自動(dòng)化測試框架(和工具),支持iOS和Android原生和混合的移動(dòng)Web應(yīng)用程序。它內(nèi)部使用的JSONWireProtocol通過Selenium的WebDriver,來與iOS和Android應(yīng)用進(jìn)行交互。它通過uiautomator(API level 16或更高)和Seledroid(API level 低于16)支持Android,通過UI Automation支持iOS,還有Android和iOS都支持的移動(dòng)web如Selenium driver。
Appium的大優(yōu)點(diǎn)在于你幾乎可以用任意一種編程語言(例如,Java、Objective-C、JavaScript、PHP、Ruby、Python和C#等)來編寫Appium腳本而不必選擇工具,兼容重要的平臺(tái)(Android和iOS)而不必安裝和配置設(shè)備適應(yīng)測試等等。并且,如果你熟悉Selenium的話,那么使用Appium用于移動(dòng)app測試對(duì)你而言將是輕而易舉的一件事。因?yàn)樗鼈兪褂孟嗤腤ebDriver,并且以同樣的方式使用DesiredCapabilities。所以Appium與Selenium在配置應(yīng)用程序運(yùn)行時(shí)有諸多相似之處。
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("_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()
官方網(wǎng)站:http://appium.io/
總結(jié)
以上就是我們列出的5款棒的測試框架,可用于日常的Android構(gòu)建,創(chuàng)立和修改。當(dāng)然,每一種框架都有其優(yōu)勢和缺陷。Appium可以同時(shí)測試你的Android和iOS版本。但如果你是一個(gè)忠實(shí)的Android開發(fā)人員只開發(fā)安卓版本的app,那么,使用Robotium就很不錯(cuò)的。Testdroid Recorder還可為我們?cè)谏蓽y試腳本節(jié)省大量的時(shí)間和金錢(這是免費(fèi)的哦!)。因此,好好思考下你的測試需求——功能測試、兼容性測試、UI測試等等——然后為自己選取適合和佳的Android測試框架。
譯文鏈接:http://www.codeceo.com/article/5-android-test-framework.html
英文原文:Top 5 Android Testing Frameworks (with Examples)
翻譯作者:碼農(nóng)網(wǎng) – 小峰
本站文章版權(quán)歸原作者及原出處所有 。內(nèi)容為作者個(gè)人觀點(diǎn), 并不代表本站贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé),本站只提供參考并不構(gòu)成任何投資及應(yīng)用建議。本站是一個(gè)個(gè)人學(xué)習(xí)交流的平臺(tái),網(wǎng)站上部分文章為轉(zhuǎn)載,并不用于任何商業(yè)目的,我們已經(jīng)盡可能的對(duì)作者和來源進(jìn)行了通告,但是能力有限或疏忽,造成漏登,請(qǐng)及時(shí)聯(lián)系我們,我們將根據(jù)著作權(quán)人的要求,立即更正或者刪除有關(guān)內(nèi)容。本站擁有對(duì)此聲明的最終解釋權(quán)。