关于Android的自动化测试之UI测试,之前介绍过Android 自动化测试(4)<uiautomator>, 在android原生的单元测试框架上,利用uiautomator.jar这个类库来完成Android的界面上的测试,这已经使得测试比较简单了。但还有更加简单的写测试的方式,那就是利用一些第三方的测试框架,比如robotium。
Android的第三方的测试框架,有Robolectric 和 robotium,我试着用了下,觉得robotium已经非常好用了。
1、概要
Robotium是一款国外的Android自动化测试框架,它提供了模拟各种手势操作(点击、长按、滑动等)、查找和断言机制的API,能够对各种控件进行操作。Robotium结合Android官方提供的测试框架,做了一些封装,达到对简化应用程序进行自动化的测试的目的。
Robotium provides the following benefits:
Test Android apps, both native and hybrid. 测试安卓应用,native的和hybrid的都支持。
Requires minimal knowledge of the application under test. 需要很少的应用测试的知识
The framework handles multiple Android activities automatically. 框架能自动的支持多个安卓Activities
Minimal time needed to write solid test cases. 花较少的时间就可以写比较可靠的测试Case
Readability of test cases is greatly improved, compared to standard instrumentation tests. 对比标准的测试,测试Case的可读性大大提高。
Test cases are more robust due to the run-time binding to UI components. 由于run-time 绑定到了UI不见, 测试Case更加鲁棒了。
Fast test case execution. 快速的测试Case的执行
Integrates smoothly with Maven, Gradle or Ant to run tests as part of continuous integration. 和Mave,Gradle,Ant等平滑的集成,跑单元测试,成为持续集成的一个部分。
入门教程和简单工程搭建见Robotium环境搭建与新手入门教程。
2、核心类介绍:
最核心的类,如之前的所述,当属控件的搜索和控制。com.robotium.solo.Solo 当然不让地成为 Robotium框架的核心。
This class contains all the methods that the sub-classes have. It supports test cases that span over multiple activities. Robotium has full support for Activities, Dialogs, Toasts, Menus and Context Menus. When writing tests there is no need to plan for
or expect new activities in the test case. All is handled automatically by Robotium-Solo. Robotium-Solo can be used in conjunction with ActivityInstrumentationTestCase2. The test cases are written from a user perspective were technical details are not needed.
Example of usage (test case spanning over multiple activities):
以下是摘自robotium-solo-5.2.1-javadoc
| Field Detail |
|---|
protected final com.robotium.solo.Asserter asserter
protected final com.robotium.solo.ViewFetcher viewFetcher
protected final com.robotium.solo.Checker checker
protected final com.robotium.solo.Clicker clicker
protected final com.robotium.solo.Presser presser
protected final com.robotium.solo.Searcher searcher
protected final com.robotium.solo.ActivityUtils activityUtils
protected final com.robotium.solo.DialogUtils dialogUtils
protected final com.robotium.solo.TextEnterer textEnterer
protected final com.robotium.solo.Rotator rotator
protected final com.robotium.solo.Scroller scroller
protected final com.robotium.solo.Sleeper sleeper
protected final com.robotium.solo.Swiper swiper
protected final com.robotium.solo.Tapper tapper
protected final com.robotium.solo.Waiter waiter
protected final com.robotium.solo.Setter setter
protected final com.robotium.solo.Getter getter
protected final com.robotium.solo.WebUtils webUtils
protected final com.robotium.solo.Sender sender
protected final com.robotium.solo.ScreenshotTaker screenshotTaker
protected final android.app.Instrumentation instrumentation
protected final com.robotium.solo.Zoomer zoomer
protected String webUrl
public static final int LANDSCAPE
public static final int PORTRAIT
public static final int RIGHT
public static final int LEFT
public static final int UP
public static final int DOWN
public static final int ENTER
public static final int MENU
public static final int DELETE
public static final int CLOSED
public static final int OPENED
| Constructor Detail |
|---|
public Solo(android.app.Instrumentation instrumentation,
android.app.Activity activity)
instrumentation - the Instrumentation instanceactivity - the start Activity or null if no Activity is specifiedpublic Solo(android.app.Instrumentation instrumentation,
Solo.Config config)
instrumentation - the Instrumentation instanceconfig - the Solo.Config instancepublic Solo(android.app.Instrumentation instrumentation,
Solo.Config config,
android.app.Activity activity)
instrumentation - the Instrumentation instanceconfig - the Solo.Config instanceactivity - the start Activity or null if no Activity is specifiedpublic Solo(android.app.Instrumentation instrumentation)
instrumentation - the Instrumentation instance| Method Detail |
|---|
public android.app.Instrumentation.ActivityMonitor getActivityMonitor()
public Solo.Config getConfig()
public ArrayList<android.view.View> getViews()
ArrayList of the View objects located in the focused windowpublic ArrayList<android.view.View> getViews(android.view.View parent)
parent - the parent view from which to return the viewsArrayList of the View objects contained in the specified Viewpublic android.view.View getTopParent(android.view.View view)
view - the View whose top parent is requestedViewpublic boolean waitForText(String text)
text - the text to wait for, specified as a regular expressiontrue if text is displayed and false if it is not displayed before the timeoutpublic boolean waitForText(String text, int minimumNumberOfMatches, long timeout)
text - the text to wait for, specified as a regular expressionminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the the amount of time in milliseconds to waittrue if text is displayed and false if it is not displayed before the timeoutpublic boolean waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll)
text - the text to wait for, specified as a regular expressionminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the the amount of time in milliseconds to waitscroll - true if scrolling should be performedtrue if text is displayed and false if it is not displayed before the timeoutpublic boolean waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible)
text - the text to wait for, specified as a regular expressionminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the the amount of time in milliseconds to waitscroll - true if scrolling should be performedonlyVisible - true if only visible text views should be waited fortrue if text is displayed and false if it is not displayed before the timeoutpublic boolean waitForView(int id)
id - the R.id of the View to wait fortrue if the View is displayed and false if it is not displayed before the timeoutpublic boolean waitForView(int id,
int minimumNumberOfMatches,
int timeout)
id - the R.id of the View to wait forminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the amount of time in milliseconds to waittrue if the View is displayed and false if it is not displayed before the timeoutpublic boolean waitForView(int id,
int minimumNumberOfMatches,
int timeout,
boolean scroll)
id - the R.id of the View to wait forminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the amount of time in milliseconds to waitscroll - true if scrolling should be performedtrue if the View is displayed and false if it is not displayed before the timeoutpublic <T extends android.view.View> boolean waitForView(Class<T> viewClass)
viewClass - the View class to wait fortrue if the View is displayed and false if it is not displayed before the timeoutpublic <T extends android.view.View> boolean waitForView(android.view.View view)
view - the View object to wait fortrue if the View is displayed and false if it is not displayed before the timeoutpublic <T extends android.view.View> boolean waitForView(android.view.View view,
int timeout,
boolean scroll)
view - the View object to wait fortimeout - the amount of time in milliseconds to waitscroll - true if scrolling should be performedtrue if the View is displayed and false if it is not displayed before the timeoutpublic <T extends android.view.View> boolean waitForView(Class<T> viewClass, int minimumNumberOfMatches, int timeout)
viewClass - the View class to wait forminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the amount of time in milliseconds to waittrue if the View is displayed and false if it is not displayed before the timeoutpublic <T extends android.view.View> boolean waitForView(Class<T> viewClass, int minimumNumberOfMatches, int timeout, boolean scroll)
viewClass - the View class to wait forminimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the amount of time in milliseconds to waitscroll - true if scrolling should be performedtrue if the View is displayed and false if it is not displayed before the timeoutpublic boolean waitForWebElement(By by)
by - the By object. Examples are: By.id("id") and By.name("name")true if the WebElement is displayed and false if
it is not displayed before the timeoutpublic boolean waitForWebElement(By by, int timeout, boolean scroll)
by - the By object. Examples are: By.id("id") and By.name("name")timeout - the the amount of time in milliseconds to waitscroll - true if scrolling should be performedtrue if the WebElement is displayed and false if
it is not displayed before the timeoutpublic boolean waitForWebElement(By by, int minimumNumberOfMatches, int timeout, boolean scroll)
by - the By object. Examples are: By.id("id") and By.name("name")minimumNumberOfMatches - the minimum number of matches that are expected to be found. 0 means any number of matchestimeout - the the amount of time in milliseconds to waitscroll - true if scrolling should be performedtrue if the WebElement is displayed and false if
it is not displayed before the timeoutpublic boolean waitForCondition(Condition condition, int timeout)
condition - the condition to wait fortimeout - the amount of time in milliseconds to waittrue if condition is satisfied and false if it is not satisfied before the timeoutpublic boolean searchEditText(String text)
text - the text to search fortrue if an EditText displaying the specified text is found or false if it is not foundpublic boolean searchButton(String text)
true if at least one Button is found. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressiontrue if a Button displaying the specified text is found and false if it is not foundpublic boolean searchButton(String text, boolean onlyVisible)
true if at least one Button is found. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressiononlyVisible - true if only Button visible on the screen should be searchedtrue if a Button displaying the specified text is found and false if it is not foundpublic boolean searchToggleButton(String text)
true if at least one ToggleButton is found. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressiontrue if a ToggleButton displaying the specified text is found and false if it is not foundpublic boolean searchButton(String text, int minimumNumberOfMatches)
true if the searched Button is found a specified number of times. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressionminimumNumberOfMatches - the minimum number of matches expected to be found. 0 matches means that one or more matches are expected to be foundtrue if a Button displaying the specified text is found a specified number of times and false if it is not foundpublic boolean searchButton(String text, int minimumNumberOfMatches, boolean onlyVisible)
true if the searched Button is found a specified number of times. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressionminimumNumberOfMatches - the minimum number of matches expected to be found. 0 matches means that one or more matches are expected to be foundonlyVisible - true if only Button visible on the screen should be searchedtrue if a Button displaying the specified text is found a specified number of times and false if it is not foundpublic boolean searchToggleButton(String text, int minimumNumberOfMatches)
true if the searched ToggleButton is found a specified number of times. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressionminimumNumberOfMatches - the minimum number of matches expected to be found. 0 matches means that one or more matches are expected to be foundtrue if a ToggleButton displaying the specified text is found a specified number of times and false if it is not foundpublic boolean searchText(String text)
true if at least one item is found displaying the expected text. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressiontrue if the search string is found and false if it is not foundpublic boolean searchText(String text, boolean onlyVisible)
true if at least one item is found displaying the expected text. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressiononlyVisible - true if only texts visible on the screen should be searchedtrue if the search string is found and false if it is not foundpublic boolean searchText(String text, int minimumNumberOfMatches)
true if the searched text is found a specified number of times. Will automatically scroll when needed.
text - the text to search for. The parameter will be interpreted as a regular expressionminimumNumberOfMatches - the minimum number of matches expected to be found. 0 matches means that one or more matches are expected to be foundtrue if text is found a specified number of times and false if the text is not foundpublic boolean searchText(String text, int minimumNumberOfMatches, boolean scroll)
true if the searched text is found a specified number of times.
text - the text to search for. The parameter will be interpreted as a regular expression.minimumNumberOfMatches - the minimum number of matches expected to be found. 0 matches means that one or more matches are expected to be foundscroll - true if scrolling should be performedtrue if text is found a specified number of times and false if the text is not foundpublic boolean searchText(String text, int minimumNumberOfMatches, boolean scroll, boolean onlyVisible)
true if the searched text is found a specified number of times.
text - the text to search for. The parameter will be interpreted as a regular expression.minimumNumberOfMatches - the minimum number of matches expected to be found. 0 matches means that one or more matches are expected to be foundscroll - true if scrolling should be performedonlyVisible - true if only texts visible on the screen should be searchedtrue if text is found a specified number of times and false if the text is not foundpublic void setActivityOrientation(int orientation)
public android.app.Activity getCurrentActivity()
public void assertCurrentActivity(String message, String name)
message - the message to display if the assert failsname - the name of the Activity that is expected to be active. Example is: "MyActivity"public void assertCurrentActivity(String message, Class activityClass)
message - the message to display if the assert failsactivityClass - the class of the Activity that is expected to be active. Example is: MyActivity.classpublic void assertCurrentActivity(String message, String name, boolean isNewInstance)
message - the message to display if the assert failsname - the name of the Activity that is expected to be active. Example is: "MyActivity"isNewInstance - true if the expected Activity is a new instance of the Activitypublic void assertCurrentActivity(String message, Class activityClass, boolean isNewInstance)
message - the message to display if the assert failsactivityClass - the class of the Activity that is expected to be active. Example is: MyActivity.classisNewInstance - true if the expected Activity is a new instance of the Activitypublic void assertMemoryNotLow()
public boolean waitForDialogToOpen()
true if the Dialog is opened before the timeout and false if it is not openedpublic boolean waitForDialogToClose()
true if the Dialog is closed before the timeout and false if it is not closedpublic boolean waitForDialogToOpen(long timeout)
timeout - the amount of time in milliseconds to waittrue if the Dialog is opened before the timeout and false if it is not openedpublic boolean waitForDialogToClose(long timeout)
timeout - the amount of time in milliseconds to waittrue if the Dialog is closed before the timeout and false if it is not closedpublic void goBack()
public void clickOnScreen(float x,
float y)
x - the x coordinatey - the y coordinatepublic void clickOnScreen(float x,
float y,
int numberOfClicks)
x - the x coordinatey - the y coordinatenumberOfClicks - the number of clicks to performpublic void clickLongOnScreen(float x,
float y)
x - the x coordinatey - the y coordinatepublic void clickLongOnScreen(float x,
float y,
int time)
x - the x coordinatey - the y coordinatetime - the amount of time to long clickpublic void clickOnButton(String text)
text - the text displayed by the Button. The parameter will be interpreted as a regular expressionpublic void clickOnImageButton(int index)
index - the index of the ImageButton to click. 0 if only one is availablepublic void clickOnToggleButton(String text)
text - the text displayed by the ToggleButton. The parameter will be interpreted as a regular expressionpublic void clickOnMenuItem(String text)
text - the text displayed by the MenuItem. The parameter will be interpreted as a regular expressionpublic void clickOnMenuItem(String text, boolean subMenu)
text - the text displayed by the MenuItem. The parameter will be interpreted as a regular expressionsubMenu - true if the menu item could be located in a sub menupublic void clickOnWebElement(WebElement webElement)
webElement - the WebElement to clickpublic void clickOnWebElement(By by)
by - the By object. Examples are: By.id("id") and By.name("name")public void clickOnWebElement(By by, int match)
by - the By object. Examples are: By.id("id") and By.name("name")match - if multiple objects match, this determines which one to clickpublic void clickOnWebElement(By by, int match, boolean scroll)
by - the By object. Examples are: By.id("id") and By.name("name")match - if multiple objects match, this determines which one to clickscroll - true if scrolling should be performedpublic void pressMenuItem(int index)
0 is the first item in the first row, Index 3 is the first item in the second row and index 6 is the first item
in the third row.
index - the index of the MenuItem to presspublic void pressMenuItem(int index,
int itemsPerRow)
index - the index of the MenuItem to pressitemsPerRow - the amount of menu items there are per rowpublic void pressSoftKeyboardNextButton()
public void pressSoftKeyboardSearchButton()
public void pressSpinnerItem(int spinnerIndex,
int itemIndex)
spinnerIndex - the index of the Spinner menu to useitemIndex - the index of the Spinner item to press relative to the currently selected item. A Negative number moves up on the Spinner, positive moves downpublic void clickOnView(android.view.View view)
view - the View to clickpublic void clickOnView(android.view.View view,
boolean immediately)
view - the View to clickimmediately - true if View should be clicked without any waitpublic void clickLongOnView(android.view.View view)
view - the View to long clickpublic void clickLongOnView(android.view.View view,
int time)
view - the View to long clicktime - the amount of time to long clickpublic void clickOnText(String text)
text - the text to click. The parameter will be interpreted as a regular expressionpublic void clickOnText(String text, int match)
text - the text to click. The parameter will be interpreted as a regular expressionmatch - if multiple objects match the text, this determines which one to clickpublic void clickOnText(String text, int match, boolean scroll)
text - the text to click. The parameter will be interpreted as a regular expressionmatch - if multiple objects match the text, this determines which one to clickscroll - true if scrolling should be performedpublic void clickLongOnText(String text)
text - the text to click. The parameter will be interpreted as a regular expressionpublic void clickLongOnText(String text, int match)
text - the text to click. The parameter will be interpreted as a regular expressionmatch - if multiple objects match the text, this determines which one to clickpublic void clickLongOnText(String text, int match, boolean scroll)
text - the text to click. The parameter will be interpreted as a regular expressionmatch - if multiple objects match the text, this determines which one to clickscroll - true if scrolling should be performedpublic void clickLongOnText(String text, int match, int time)
text - the text to click. The parameter will be interpreted as a regular expressionmatch - if multiple objects match the text, this determines which one to clicktime - the amount of time to long clickpublic void clickLongOnTextAndPress(String text, int index)
text - the text to click. The parameter will be interpreted as a regular expressionindex - the index of the menu item to press. 0 if only one is availablepublic void clickOnButton(int index)
index - the index of the Button to click. 0 if only one is availablepublic void clickOnRadioButton(int index)
index - the index of the RadioButton to click. 0 if only one is availablepublic void clickOnCheckBox(int index)
index - the index of the CheckBox to click. 0 if only one is availablepublic void clickOnEditText(int index)
index - the index of the EditText to click. 0 if only one is availablepublic ArrayList<android.widget.TextView> clickInList(int line)
line - the line to clickArrayList of the TextView objects located in the list linepublic ArrayList<android.widget.TextView> clickInList(int line, int index)
line - the line to clickindex - the index of the list. 0 if only one is availableArrayList of the TextView objects located in the list linepublic ArrayList<android.widget.TextView> clickLongInList(int line)
line - the line to clickArrayList of the TextView objects located in the list linepublic ArrayList<android.widget.TextView> clickLongInList(int line, int index)
line - the line to clickindex - the index of the list. 0 if only one is availableArrayList of the TextView objects located in the list linepublic ArrayList<android.widget.TextView> clickLongInList(int line, int index, int time)
line - the line to clickindex - the index of the list. 0 if only one is availabletime - the amount of time to long clickArrayList of the TextView objects located in the list linepublic void clickOnActionBarItem(int id)
id - the R.id of the ActionBar item to clickpublic void clickOnActionBarHomeButton()
public void drag(float fromX,
float toX,
float fromY,
float toY,
int stepCount)
fromX - X coordinate of the initial touch, in screen coordinatestoX - X coordinate of the drag destination, in screen coordinatesfromY - Y coordinate of the initial touch, in screen coordinatestoY - Y coordinate of the drag destination, in screen coordinatesstepCount - how many move steps to include in the drag. Less steps results in a faster dragpublic boolean scrollDown()
true if more scrolling can be performed and false if it is at the end of the screenpublic void scrollToBottom()
public boolean scrollUp()
true if more scrolling can be performed and false if it is at the top of the screenpublic void scrollToTop()
public boolean scrollDownList(android.widget.AbsListView list)
list - the AbsListView to scrolltrue if more scrolling can be performedpublic boolean scrollListToBottom(android.widget.AbsListView list)
list - the AbsListView to scrolltrue if more scrolling can be performedpublic boolean scrollUpList(android.widget.AbsListView list)
list - the AbsListView to scrolltrue if more scrolling can be performedpublic boolean scrollListToTop(android.widget.AbsListView list)
list - the AbsListView to scrolltrue if more scrolling can be performedpublic boolean scrollDownList(int index)
index - the index of the ListView to scroll. 0 if only one list is availabletrue if more scrolling can be performedpublic boolean scrollListToBottom(int index)
index - the index of the ListView to scroll. 0 if only one list is availabletrue if more scrolling can be performedpublic boolean scrollUpList(int index)
index - the index of the ListView to scroll. 0 if only one list is availabletrue if more scrolling can be performedpublic boolean scrollListToTop(int index)
index - the index of the ListView to scroll. 0 if only one list is availabletrue if more scrolling can be performedpublic void scrollListToLine(android.widget.AbsListView absListView,
int line)
absListView - the AbsListView to scrollline - the line to scroll topublic void scrollListToLine(int index,
int line)
index - the index of the AbsListView to scrollline - the line to scroll topublic void scrollToSide(int side,
float scrollPosition,
int stepCount)
public void scrollToSide(int side,
float scrollPosition)
public void scrollToSide(int side)
public void scrollViewToSide(android.view.View view,
int side,
float scrollPosition,
int stepCount)
public void scrollViewToSide(android.view.View view,
int side,
float scrollPosition)
public void scrollViewToSide(android.view.View view,
int side)
public void pinchToZoom(android.graphics.PointF startPoint1,
android.graphics.PointF startPoint2,
android.graphics.PointF endPoint1,
android.graphics.PointF endPoint2)
startPoint1 - First "finger" down on the screenstartPoint2 - Second "finger" down on the screenendPoint1 - Corresponding ending point of startPoint1endPoint2 - Corresponding ending point of startPoint2public void swipe(android.graphics.PointF startPoint1,
android.graphics.PointF startPoint2,
android.graphics.PointF endPoint1,
android.graphics.PointF endPoint2)
startPoint1 - First "finger" down on the screenstartPoint2 - Second "finger" down on the screenendPoint1 - Corresponding ending point of startPoint1endPoint2 - Corresponding ending point of startPoint2public void rotateLarge(android.graphics.PointF center1,
android.graphics.PointF center2)
center1 - Center of semi-circle drawn from [0, Pi]center2 - Center of semi-circle drawn from [Pi, 3*Pi]public void rotateSmall(android.graphics.PointF center1,
android.graphics.PointF center2)
center1 - Center of semi-circle drawn from [0, Pi]center2 - Center of semi-circle drawn from [Pi, 3*Pi]public void setDatePicker(int index,
int year,
int monthOfYear,
int dayOfMonth)
index - the index of the DatePicker. 0 if only one is availableyear - the year e.g. 2011monthOfYear - the month which starts from zero e.g. 0 for JanuarydayOfMonth - the day e.g. 10public void setDatePicker(android.widget.DatePicker datePicker,
int year,
int monthOfYear,
int dayOfMonth)
datePicker - the DatePicker objectyear - the year e.g. 2011monthOfYear - the month which starts from zero e.g. 03 for AprildayOfMonth - the day e.g. 10public void setTimePicker(int index,
int hour,
int minute)
index - the index of the TimePicker. 0 if only one is availablehour - the hour e.g. 15minute - the minute e.g. 30public void setTimePicker(android.widget.TimePicker timePicker,
int hour,
int minute)
timePicker - the TimePicker objecthour - the hour e.g. 15minute - the minute e.g. 30public void setProgressBar(int index,
int progress)
SeekBar and RatingBar.
index - the index of the ProgressBarprogress - the progress to set the ProgressBarpublic void setProgressBar(android.widget.ProgressBar progressBar,
int progress)
SeekBar and RatingBar.
progressBar - the ProgressBarprogress - the progress to set the ProgressBarpublic void setNavigationDrawer(int status)
Solo.CLOSED and Solo.OPENED.
status - the status that the NavigationDrawer should be set topublic void setSlidingDrawer(int index,
int status)
Solo.CLOSED and Solo.OPENED.
index - the index of the SlidingDrawerstatus - the status to set the SlidingDrawerpublic void setSlidingDrawer(android.widget.SlidingDrawer slidingDrawer,
int status)
Solo.CLOSED and Solo.OPENED.
slidingDrawer - the SlidingDrawerstatus - the status to set the SlidingDrawerpublic void enterText(int index,
String text)
index - the index of the EditText. 0 if only one is availabletext - the text to enter in the EditText fieldpublic void enterText(android.widget.EditText editText,
String text)
editText - the EditText to enter text intext - the text to enter in the EditText fieldpublic void enterTextInWebElement(By by, String text)
by - the By object. Examples are: By.id("id") and By.name("name")text - the text to enter in the WebElement fieldpublic void typeText(int index,
String text)
index - the index of the EditText. 0 if only one is availabletext - the text to type in the EditText fieldpublic void typeText(android.widget.EditText editText,
String text)
editText - the EditText to type text intext - the text to type in the EditText fieldpublic void typeTextInWebElement(By by, String text)
by - the By object. Examples are: By.id("id") and By.name("name")text - the text to enter in the WebElement fieldpublic void typeTextInWebElement(By by, String text, int match)
by - the By object. Examples are: By.id("id") and By.name("name")text - the text to enter in the WebElement fieldmatch - if multiple objects match, this determines which one will be typed inpublic void typeTextInWebElement(WebElement webElement, String text)
webElement - the WebElement to type text intext - the text to enter in the WebElement fieldpublic void clearEditText(int index)
index - the index of the EditText to clear. 0 if only one is availablepublic void clearEditText(android.widget.EditText editText)
editText - the EditText to clearpublic void clearTextInWebElement(By by)
by - the By object. Examples are: By.id("id") and By.name("name")public void clickOnImage(int index)
index - the index of the ImageView to click. 0 if only one is availablepublic android.widget.EditText getEditText(int index)
index - the index of the EditText. 0 if only one is availableEditText matching the specified indexpublic android.widget.Button getButton(int index)
index - the index of the Button. 0 if only one is availableButton matching the specified indexpublic android.widget.TextView getText(int index)
index - the index of the TextView. 0 if only one is availableTextView matching the specified indexpublic android.widget.ImageView getImage(int index)
index - the index of the ImageView. 0 if only one is availableImageView matching the specified indexpublic android.widget.ImageButton getImageButton(int index)
index - the index of the ImageButton. 0 if only one is availableImageButton matching the specified indexpublic android.widget.TextView getText(String text)
text - the text that is displayed, specified as a regular expressionTextView displaying the specified textpublic android.widget.TextView getText(String text, boolean onlyVisible)
text - the text that is displayed, specified as a regular expressiononlyVisible - true if only visible texts on the screen should be returnedTextView displaying the specified textpublic android.widget.Button getButton(String text)
text - the text that is displayed, specified as a regular expressionButton displaying the specified textpublic android.widget.Button getButton(String text, boolean onlyVisible)
text - the text that is displayed, specified as a regular expressiononlyVisible - true if only visible buttons on the screen should be returnedButton displaying the specified textpublic android.widget.EditText getEditText(String text)
text - the text that is displayed, specified as a regular expressionEditText displaying the specified textpublic android.widget.EditText getEditText(String text, boolean onlyVisible)
text - the text that is displayed, specified as a regular expressiononlyVisible - true if only visible EditTexts on the screen should be returnedEditText displaying the specified textpublic android.view.View getView(int id)
id - the R.id of the View to returnView matching the specified idpublic android.view.View getView(int id,
int index)
id - the R.id of the View to returnindex - the index of the View. 0 if only one is availableView matching the specified id and indexpublic android.view.View getView(String id)
id - the id of the View to returnView matching the specified idpublic android.view.View getView(String id, int index)
id - the id of the View to returnindex - the index of the View. 0 if only one is availableView matching the specified id and indexpublic <T extends android.view.View> T getView(Class<T> viewClass, int index)
viewClass - the class of the requested viewindex - the index of the View. 0 if only one is availableView matching the specified class and indexpublic WebElement getWebElement(By by, int index)
by - the By object. Examples are: By.id("id") and By.name("name")index - the index of the WebElement. 0 if
only one is availableWebElement matching the specified indexpublic String getWebUrl()
public ArrayList<android.view.View> getCurrentViews()
ArrayList of the View objects currently displayed in the focused windowpublic <T extends android.view.View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy)
classToFilterBy - return all instances of this class. Examples are: Button.class or ListView.classArrayList of Views matching the specified Class located in the current Activitypublic <T extends android.view.View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy, boolean includeSubclasses)
classToFilterBy - return all instances of this class. Examples are: Button.class or ListView.classincludeSubclasses - include instances of the subclasses in the ArrayList that will be returnedArrayList of Views matching the specified Class located in the current Activitypublic <T extends android.view.View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy, android.view.View parent)
classToFilterBy - return all instances of this class. Examples are: Button.class or ListView.classparent - the parent View for where to start the traversalArrayList of Views matching the specified Class located under the specified parentpublic <T extends android.view.View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy, boolean includeSubclasses, android.view.View parent)
classToFilterBy - return all instances of this class. Examples are: Button.class or ListView.classincludeSubclasses - include instances of subclasses in the ArrayList that will be returnedparent - the parent View for where to start the traversalArrayList of Views matching the specified Class located under the specified parentpublic ArrayList<WebElement> getWebElements()
ArrayList of all the WebElement objects currently
displayed in the active WebViewpublic ArrayList<WebElement> getWebElements(By by)
by - the By object. Examples are: By.id("id") and By.name("name")ArrayList of all the WebElement objects displayed
in the active WebViewpublic ArrayList<WebElement> getCurrentWebElements()
ArrayList of the WebElement objects displayed
in the active WebViewpublic ArrayList<WebElement> getCurrentWebElements(By by)
by - the By object. Examples are: By.id("id") and By.name("name")ArrayList of the WebElement objects currently
displayed in the active WebViewpublic boolean isRadioButtonChecked(int index)
index - of the RadioButton to check. 0 if only one is availabletrue if RadioButton is checked and false if it is not checkedpublic boolean isRadioButtonChecked(String text)
text - the text that the RadioButton displays, specified as a regular expressiontrue if a RadioButton matching the specified text is checked and false if it is not checkedpublic boolean isCheckBoxChecked(int index)
index - of the CheckBox to check. 0 if only one is availabletrue if CheckBox is checked and false if it is not checkedpublic boolean isToggleButtonChecked(String text)
text - the text that the ToggleButton displays, specified as a regular expressiontrue if a ToggleButton matching the specified text is checked and false if it is not checkedpublic boolean isToggleButtonChecked(int index)
index - of the ToggleButton to check. 0 if only one is availabletrue if ToggleButton is checked and false if it is not checkedpublic boolean isCheckBoxChecked(String text)
text - the text that the CheckBox displays, specified as a regular expressiontrue if a CheckBox displaying the specified text is checked and false if it is not checkedpublic boolean isTextChecked(String text)
text - the text that the CheckedTextView or CompoundButton objects display, specified as a regular expressiontrue if the specified text is checked and false if it is not checkedpublic boolean isSpinnerTextSelected(String text)
text - the text that is expected to be selected, specified as a regular expressiontrue if the specified text is selected in any Spinner and false if it is notpublic boolean isSpinnerTextSelected(int index,
String text)
index - the index of the spinner to check. 0 if only one spinner is availabletext - the text that is expected to be selected, specified as a regular expressiontrue if the specified text is selected in the specified Spinner and false if it is notpublic void hideSoftKeyboard()
public void unlockScreen()
public void sendKey(int key)
public void goBackToActivity(String name)
name - the name of the Activity to return to. Example is: "MyActivity"public boolean waitForActivity(String name)
name - the name of the Activity to wait for. Example is: "MyActivity"true if Activity appears before the timeout and false if it does notpublic boolean waitForActivity(String name, int timeout)
name - the name of the Activity to wait for. Example is: "MyActivity"timeout - the amount of time in milliseconds to waittrue if Activity appears before the timeout and false if it does notpublic boolean waitForActivity(Class<? extends android.app.Activity> activityClass)
activityClass - the class of the Activity to wait for. Example is: MyActivity.classtrue if Activity appears before the timeout and false if it does notpublic boolean waitForActivity(Class<? extends android.app.Activity> activityClass, int timeout)
activityClass - the class of the Activity to wait for. Example is: MyActivity.classtimeout - the amount of time in milliseconds to waittrue if Activity appears before the timeout and false if it does notpublic boolean waitForEmptyActivityStack(int timeout)
timeout - the amount of time in milliseconds to waittrue if activity stack is empty before the timeout and false if it is notpublic boolean waitForFragmentByTag(String tag)
tag - the name of the tagtrue if fragment appears and false if it does not appear before the timeoutpublic boolean waitForFragmentByTag(String tag, int timeout)
tag - the name of the tagtimeout - the amount of time in milliseconds to waittrue if fragment appears and false if it does not appear before the timeoutpublic boolean waitForFragmentById(int id)
id - the R.id of the fragmenttrue if fragment appears and false if it does not appear before the timeoutpublic boolean waitForFragmentById(int id,
int timeout)
id - the R.id of the fragmenttimeout - the amount of time in milliseconds to waittrue if fragment appears and false if it does not appear before the timeoutpublic boolean waitForLogMessage(String logMessage)
logMessage - the log message to wait fortrue if log message appears and false if it does not appear before the timeoutclearLog()public boolean waitForLogMessage(String logMessage, int timeout)
logMessage - the log message to wait fortimeout - the amount of time in milliseconds to waittrue if log message appears and false if it does not appear before the timeoutclearLog()public void clearLog()
public String getString(int id)
id - the R.id of the Stringpublic String getString(String id)
id - the id of the Stringpublic void sleep(int time)
time - the time in milliseconds that Robotium should sleeppublic void finalize()
throws Throwable
public void finishOpenedActivities()
public void takeScreenshot()
Solo.Config objects
save path (default set to: /sdcard/Robotium-Screenshots/). Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
public void takeScreenshot(String name)
Solo.Config objects
save path (default set to: /sdcard/Robotium-Screenshots/). Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
name - the name to give the screenshotpublic void takeScreenshot(String name, int quality)
Solo.Config objects
save path (default set to: /sdcard/Robotium-Screenshots/). Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
name - the name to give the screenshotquality - the compression rate. From 0 (compress for lowest size) to 100 (compress for maximum quality)public void startScreenshotSequence(String name)
Solo.Config objects
save path (default set to: /sdcard/Robotium-Screenshots/). The name prefix is appended with "_" + sequence_number for each image in the sequence, where numbering starts at 0. Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml
of the application under test. At present multiple simultaneous screenshot sequences are not supported. This method will throw an exception if stopScreenshotSequence() has not been called to finish any prior sequences. Calling this method is equivalend to
calling startScreenshotSequence(name, 80, 400, 100);
name - the name prefix to give the screenshotpublic void startScreenshotSequence(String name, int quality, int frameDelay, int maxFrames)
Solo.Config objects
save path (default set to: /sdcard/Robotium-Screenshots/). The name prefix is appended with "_" + sequence_number for each image in the sequence, where numbering starts at 0. Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in the AndroidManifest.xml
of the application under test. Taking a screenshot will take on the order of 40-100 milliseconds of time on the main UI thread. Therefore it is possible to mess up the timing of tests if the frameDelay value is set too small. At present multiple simultaneous
screenshot sequences are not supported. This method will throw an exception if stopScreenshotSequence() has not been called to finish any prior sequences.
name - the name prefix to give the screenshotquality - the compression rate. From 0 (compress for lowest size) to 100 (compress for maximum quality)frameDelay - the time in milliseconds to wait between each framemaxFrames - the maximum number of frames that will comprise this sequence
3、样例
package com.robotium.test;
import com.robotium.solo.Solo;
import com.example.android.notepad.NotesList;
import android.test.ActivityInstrumentationTestCase2;
public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{
private Solo solo;
public NotePadTest() {
super(NotesList.class);
}
@Override
public void setUp() throws Exception {
//setUp() is run before a test case is started.
//This is where the solo object is created.
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
public void tearDown() throws Exception {
//tearDown() is run after a test case has finished.
//finishOpenedActivities() will finish all the activities that have been opened during the test execution.
solo.finishOpenedActivities();
}
public void testAddNote() throws Exception {
//Unlock the lock screen
solo.unlockScreen();
solo.clickOnMenuItem("Add note");
//Assert that NoteEditor activity is opened
solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");
//In text field 0, enter Note 1
solo.enterText(0, "Note 1");
solo.goBack();
//Clicks on menu item
solo.clickOnMenuItem("Add note");
//In text field 0, type Note 2
solo.typeText(0, "Note 2");
//Go back to first activity
solo.goBack();
//Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/".
solo.takeScreenshot();
boolean notesFound = solo.searchText("Note 1") && solo.searchText("Note 2");
//Assert that Note 1 & Note 2 are found
assertTrue("Note 1 and/or Note 2 are not found", notesFound);
}
public void testEditNote() throws Exception {
// Click on the second list line
solo.clickInList(2);
//Hides the soft keyboard
solo.hideSoftKeyboard();
// Change orientation of activity
solo.setActivityOrientation(Solo.LANDSCAPE);
// Change title
solo.clickOnMenuItem("Edit title");
//In first text field (0), add test
solo.enterText(0, " test");
solo.goBack();
solo.setActivityOrientation(Solo.PORTRAIT);
// (Regexp) case insensitive
boolean noteFound = solo.waitForText("(?i).*?note 1 test");
//Assert that Note 1 test is found
assertTrue("Note 1 test is not found", noteFound);
}
public void testRemoveNote() throws Exception {
//(Regexp) case insensitive/text that contains "test"
solo.clickOnText("(?i).*?test.*");
//Delete Note 1 test
solo.clickOnMenuItem("Delete");
//Note 1 test should not be found
boolean noteFound = solo.searchText("Note 1 test");
//Assert that Note 1 test is not found
assertFalse("Note 1 Test is found", noteFound);
solo.clickLongOnText("Note 2");
//Clicks on Delete in the context menu
solo.clickOnText("Delete");
//Will wait 100 milliseconds for the text: "Note 2"
noteFound = solo.waitForText("Note 2", 1, 100);
//Assert that Note 2 is not found
assertFalse("Note 2 is found", noteFound);
}
}
4、总结:
后续将介绍 Android CodeCoverage
原文地址:http://blog.csdn.net/vshuang/article/details/40214119