关于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环境搭建与新手入门教程。 代码下载见http://download.csdn.net/detail/vshuang/8053865
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
instance
activity
- the start
Activity
or
null
if no Activity is specified
public Solo(android.app.Instrumentation instrumentation, Solo.Config config)
instrumentation
- the
Instrumentation
instance
config
- the
Solo.Config
instance
public Solo(android.app.Instrumentation instrumentation, Solo.Config config, android.app.Activity activity)
instrumentation
- the
Instrumentation
instance
config
- the
Solo.Config
instance
activity
- the start
Activity
or
null
if no Activity is specified
public 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 window
public ArrayList<android.view.View> getViews(android.view.View parent)
parent
- the parent view from which to return the views
ArrayList
of the
View
objects contained in the specified
View
public android.view.View getTopParent(android.view.View view)
view
- the
View
whose top parent is requested
View
public boolean waitForText(String text)
text
- the text to wait for, specified as a regular expression
true
if text is displayed and
false
if it is not displayed before the timeout
public boolean waitForText(String text, int minimumNumberOfMatches, long timeout)
text
- the text to wait for, specified as a regular expression
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the the amount of time in milliseconds to wait
true
if text is displayed and
false
if it is not displayed before the timeout
public boolean waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll)
text
- the text to wait for, specified as a regular expression
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the the amount of time in milliseconds to wait
scroll
-
true
if scrolling should be performed
true
if text is displayed and
false
if it is not displayed before the timeout
public boolean waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible)
text
- the text to wait for, specified as a regular expression
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the the amount of time in milliseconds to wait
scroll
-
true
if scrolling should be performed
onlyVisible
-
true
if only visible text views should be waited for
true
if text is displayed and
false
if it is not displayed before the timeout
public boolean waitForView(int id)
id
- the R.id of the
View
to wait for
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public boolean waitForView(int id, int minimumNumberOfMatches, int timeout)
id
- the R.id of the
View
to wait for
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the amount of time in milliseconds to wait
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public boolean waitForView(int id, int minimumNumberOfMatches, int timeout, boolean scroll)
id
- the R.id of the
View
to wait for
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the amount of time in milliseconds to wait
scroll
-
true
if scrolling should be performed
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public <T extends android.view.View> boolean waitForView(Class<T> viewClass)
viewClass
- the
View
class to wait for
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public <T extends android.view.View> boolean waitForView(android.view.View view)
view
- the
View
object to wait for
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public <T extends android.view.View> boolean waitForView(android.view.View view, int timeout, boolean scroll)
view
- the
View
object to wait for
timeout
- the amount of time in milliseconds to wait
scroll
-
true
if scrolling should be performed
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public <T extends android.view.View> boolean waitForView(Class<T> viewClass, int minimumNumberOfMatches, int timeout)
viewClass
- the
View
class to wait for
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the amount of time in milliseconds to wait
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public <T extends android.view.View> boolean waitForView(Class<T> viewClass, int minimumNumberOfMatches, int timeout, boolean scroll)
viewClass
- the
View
class to wait for
minimumNumberOfMatches
- the minimum number of matches that are expected to be found.
0
means any number of matches
timeout
- the amount of time in milliseconds to wait
scroll
-
true
if scrolling should be performed
true
if the
View
is displayed and
false
if it is not displayed before the timeout
public 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 timeout
public 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 wait
scroll
-
true
if scrolling should be performed
true
if the
WebElement
is displayed and
false
if it is not displayed before the timeout
public 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 matches
timeout
- the the amount of time in milliseconds to wait
scroll
-
true
if scrolling should be performed
true
if the
WebElement
is displayed and
false
if it is not displayed before the timeout
public boolean waitForCondition(Condition condition, int timeout)
condition
- the condition to wait for
timeout
- the amount of time in milliseconds to wait
true
if condition is satisfied and
false
if it is not satisfied before the timeout
public boolean searchEditText(String text)
text
- the text to search for
true
if an
EditText
displaying the specified text is found or
false
if it is not found
public 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 expression
true
if a
Button
displaying the specified text is found and
false
if it is not found
public 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 expression
onlyVisible
-
true
if only
Button
visible on the screen should be searched
true
if a
Button
displaying the specified text is found and
false
if it is not found
public 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 expression
true
if a
ToggleButton
displaying the specified text is found and
false
if it is not found
public 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 expression
minimumNumberOfMatches
- the minimum number of matches expected to be found.
0
matches means that one or more matches are expected to be found
true
if a
Button
displaying the specified text is found a specified number of times and
false
if it is not found
public 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 expression
minimumNumberOfMatches
- the minimum number of matches expected to be found.
0
matches means that one or more matches are expected to be found
onlyVisible
-
true
if only
Button
visible on the screen should be searched
true
if a
Button
displaying the specified text is found a specified number of times and
false
if it is not found
public 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 expression
minimumNumberOfMatches
- the minimum number of matches expected to be found.
0
matches means that one or more matches are expected to be found
true
if a
ToggleButton
displaying the specified text is found a specified number of times and
false
if it is not found
public 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 expression
true
if the search string is found and
false
if it is not found
public 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 expression
onlyVisible
-
true
if only texts visible on the screen should be searched
true
if the search string is found and
false
if it is not found
public 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 expression
minimumNumberOfMatches
- the minimum number of matches expected to be found.
0
matches means that one or more matches are expected to be found
true
if text is found a specified number of times and
false
if the text is not found
public 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 found
scroll
-
true
if scrolling should be performed
true
if text is found a specified number of times and
false
if the text is not found
public 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 found
scroll
-
true
if scrolling should be performed
onlyVisible
-
true
if only texts visible on the screen should be searched
true
if text is found a specified number of times and
false
if the text is not found
public void setActivityOrientation(int orientation)
orientation
- the orientation to set.
Solo.
LANDSCAPE
for landscape or
Solo.
PORTRAIT
for portrait.
public android.app.Activity getCurrentActivity()
public void assertCurrentActivity(String message, String name)
message
- the message to display if the assert fails
name
- 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 fails
activityClass
- the class of the Activity that is expected to be active. Example is:
MyActivity.class
public void assertCurrentActivity(String message, String name, boolean isNewInstance)
message
- the message to display if the assert fails
name
- 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
Activity
public void assertCurrentActivity(String message, Class activityClass, boolean isNewInstance)
message
- the message to display if the assert fails
activityClass
- the class of the Activity that is expected to be active. Example is:
MyActivity.class
isNewInstance
-
true
if the expected
Activity
is a new instance of the
Activity
public void assertMemoryNotLow()
public boolean waitForDialogToOpen()
true
if the
Dialog
is opened before the timeout and
false
if it is not opened
public boolean waitForDialogToClose()
true
if the
Dialog
is closed before the timeout and
false
if it is not closed
public boolean waitForDialogToOpen(long timeout)
timeout
- the amount of time in milliseconds to wait
true
if the
Dialog
is opened before the timeout and
false
if it is not opened
public boolean waitForDialogToClose(long timeout)
timeout
- the amount of time in milliseconds to wait
true
if the
Dialog
is closed before the timeout and
false
if it is not closed
public void goBack()
public void clickOnScreen(float x, float y)
x
- the x coordinate
y
- the y coordinate
public void clickOnScreen(float x, float y, int numberOfClicks)
x
- the x coordinate
y
- the y coordinate
numberOfClicks
- the number of clicks to perform
public void clickLongOnScreen(float x, float y)
x
- the x coordinate
y
- the y coordinate
public void clickLongOnScreen(float x, float y, int time)
x
- the x coordinate
y
- the y coordinate
time
- the amount of time to long click
public void clickOnButton(String text)
text
- the text displayed by the
Button
. The parameter will be interpreted as a regular expression
public void clickOnImageButton(int index)
index
- the index of the
ImageButton
to click. 0 if only one is available
public void clickOnToggleButton(String text)
text
- the text displayed by the
ToggleButton
. The parameter will be interpreted as a regular expression
public void clickOnMenuItem(String text)
text
- the text displayed by the MenuItem. The parameter will be interpreted as a regular expression
public void clickOnMenuItem(String text, boolean subMenu)
text
- the text displayed by the MenuItem. The parameter will be interpreted as a regular expression
subMenu
-
true
if the menu item could be located in a sub menu
public void clickOnWebElement(WebElement webElement)
webElement
- the WebElement to click
public 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 click
public 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 click
scroll
-
true
if scrolling should be performed
public 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 press
public void pressMenuItem(int index, int itemsPerRow)
index
- the index of the
MenuItem
to press
itemsPerRow
- the amount of menu items there are per row
public void pressSoftKeyboardNextButton()
public void pressSoftKeyboardSearchButton()
public void pressSpinnerItem(int spinnerIndex, int itemIndex)
spinnerIndex
- the index of the
Spinner
menu to use
itemIndex
- the index of the
Spinner
item to press relative to the currently selected item. A Negative number moves up on the
Spinner
, positive moves down
public void clickOnView(android.view.View view)
view
- the
View
to click
public void clickOnView(android.view.View view, boolean immediately)
view
- the
View
to click
immediately
-
true
if View should be clicked without any wait
public void clickLongOnView(android.view.View view)
view
- the
View
to long click
public void clickLongOnView(android.view.View view, int time)
view
- the
View
to long click
time
- the amount of time to long click
public void clickOnText(String text)
text
- the text to click. The parameter will be interpreted as a regular expression
public void clickOnText(String text, int match)
text
- the text to click. The parameter will be interpreted as a regular expression
match
- if multiple objects match the text, this determines which one to click
public void clickOnText(String text, int match, boolean scroll)
text
- the text to click. The parameter will be interpreted as a regular expression
match
- if multiple objects match the text, this determines which one to click
scroll
-
true
if scrolling should be performed
public void clickLongOnText(String text)
text
- the text to click. The parameter will be interpreted as a regular expression
public void clickLongOnText(String text, int match)
text
- the text to click. The parameter will be interpreted as a regular expression
match
- if multiple objects match the text, this determines which one to click
public void clickLongOnText(String text, int match, boolean scroll)
text
- the text to click. The parameter will be interpreted as a regular expression
match
- if multiple objects match the text, this determines which one to click
scroll
-
true
if scrolling should be performed
public void clickLongOnText(String text, int match, int time)
text
- the text to click. The parameter will be interpreted as a regular expression
match
- if multiple objects match the text, this determines which one to click
time
- the amount of time to long click
public void clickLongOnTextAndPress(String text, int index)
text
- the text to click. The parameter will be interpreted as a regular expression
index
- the index of the menu item to press.
0
if only one is available
public void clickOnButton(int index)
index
- the index of the
Button
to click.
0
if only one is available
public void clickOnRadioButton(int index)
index
- the index of the
RadioButton
to click.
0
if only one is available
public void clickOnCheckBox(int index)
index
- the index of the
CheckBox
to click.
0
if only one is available
public void clickOnEditText(int index)
index
- the index of the
EditText
to click.
0
if only one is available
public ArrayList<android.widget.TextView> clickInList(int line)
line
- the line to click
ArrayList
of the
TextView
objects located in the list line
public ArrayList<android.widget.TextView> clickInList(int line, int index)
line
- the line to click
index
- the index of the list.
0
if only one is available
ArrayList
of the
TextView
objects located in the list line
public ArrayList<android.widget.TextView> clickLongInList(int line)
line
- the line to click
ArrayList
of the
TextView
objects located in the list line
public ArrayList<android.widget.TextView> clickLongInList(int line, int index)
line
- the line to click
index
- the index of the list.
0
if only one is available
ArrayList
of the
TextView
objects located in the list line
public ArrayList<android.widget.TextView> clickLongInList(int line, int index, int time)
line
- the line to click
index
- the index of the list.
0
if only one is available
time
- the amount of time to long click
ArrayList
of the
TextView
objects located in the list line
public void clickOnActionBarItem(int id)
id
- the R.id of the ActionBar item to click
public void clickOnActionBarHomeButton()
public void drag(float fromX, float toX, float fromY, float toY, int stepCount)
fromX
- X coordinate of the initial touch, in screen coordinates
toX
- X coordinate of the drag destination, in screen coordinates
fromY
- Y coordinate of the initial touch, in screen coordinates
toY
- Y coordinate of the drag destination, in screen coordinates
stepCount
- how many move steps to include in the drag. Less steps results in a faster drag
public boolean scrollDown()
true
if more scrolling can be performed and
false
if it is at the end of the screen
public void scrollToBottom()
public boolean scrollUp()
true
if more scrolling can be performed and
false
if it is at the top of the screen
public void scrollToTop()
public boolean scrollDownList(android.widget.AbsListView list)
list
- the
AbsListView
to scroll
true
if more scrolling can be performed
public boolean scrollListToBottom(android.widget.AbsListView list)
list
- the
AbsListView
to scroll
true
if more scrolling can be performed
public boolean scrollUpList(android.widget.AbsListView list)
list
- the
AbsListView
to scroll
true
if more scrolling can be performed
public boolean scrollListToTop(android.widget.AbsListView list)
list
- the
AbsListView
to scroll
true
if more scrolling can be performed
public boolean scrollDownList(int index)
index
- the index of the
ListView
to scroll.
0
if only one list is available
true
if more scrolling can be performed
public boolean scrollListToBottom(int index)
index
- the index of the
ListView
to scroll.
0
if only one list is available
true
if more scrolling can be performed
public boolean scrollUpList(int index)
index
- the index of the
ListView
to scroll.
0
if only one list is available
true
if more scrolling can be performed
public boolean scrollListToTop(int index)
index
- the index of the
ListView
to scroll.
0
if only one list is available
true
if more scrolling can be performed
public void scrollListToLine(android.widget.AbsListView absListView, int line)
absListView
- the
AbsListView
to scroll
line
- the line to scroll to
public void scrollListToLine(int index, int line)
index
- the index of the
AbsListView
to scroll
line
- the line to scroll to
public void scrollToSide(int side, float scrollPosition, int stepCount)
side
- the side to scroll;
RIGHT
or
LEFT
scrollPosition
- the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
stepCount
- how many move steps to include in the scroll. Less steps results in a faster scroll
public void scrollToSide(int side, float scrollPosition)
side
- the side to scroll;
RIGHT
or
LEFT
scrollPosition
- the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
public void scrollToSide(int side)
side
- the side to scroll;
RIGHT
or
LEFT
public void scrollViewToSide(android.view.View view, int side, float scrollPosition, int stepCount)
view
- the View to scroll
side
- the side to scroll;
RIGHT
or
LEFT
scrollPosition
- the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
stepCount
- how many move steps to include in the scroll. Less steps results in a faster scroll
public void scrollViewToSide(android.view.View view, int side, float scrollPosition)
view
- the View to scroll
side
- the side to scroll;
RIGHT
or
LEFT
scrollPosition
- the position to scroll to, from 0 to 1 where 1 is all the way. Example is: 0.60
public void scrollViewToSide(android.view.View view, int side)
view
- the View to scroll
side
- the side to scroll;
RIGHT
or
LEFT
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 screen
startPoint2
- Second "finger" down on the screen
endPoint1
- Corresponding ending point of startPoint1
endPoint2
- Corresponding ending point of startPoint2
public void swipe(android.graphics.PointF startPoint1, android.graphics.PointF startPoint2, android.graphics.PointF endPoint1, android.graphics.PointF endPoint2)
startPoint1
- First "finger" down on the screen
startPoint2
- Second "finger" down on the screen
endPoint1
- Corresponding ending point of startPoint1
endPoint2
- Corresponding ending point of startPoint2
public 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 available
year
- the year e.g. 2011
monthOfYear
- the month which starts from zero e.g. 0 for January
dayOfMonth
- the day e.g. 10
public void setDatePicker(android.widget.DatePicker datePicker, int year, int monthOfYear, int dayOfMonth)
datePicker
- the
DatePicker
object
year
- the year e.g. 2011
monthOfYear
- the month which starts from zero e.g. 03 for April
dayOfMonth
- the day e.g. 10
public void setTimePicker(int index, int hour, int minute)
index
- the index of the
TimePicker
.
0
if only one is available
hour
- the hour e.g. 15
minute
- the minute e.g. 30
public void setTimePicker(android.widget.TimePicker timePicker, int hour, int minute)
timePicker
- the
TimePicker
object
hour
- the hour e.g. 15
minute
- the minute e.g. 30
public void setProgressBar(int index, int progress)
SeekBar
and
RatingBar
.
index
- the index of the
ProgressBar
progress
- the progress to set the
ProgressBar
public void setProgressBar(android.widget.ProgressBar progressBar, int progress)
SeekBar
and
RatingBar
.
progressBar
- the
ProgressBar
progress
- the progress to set the
ProgressBar
public void setNavigationDrawer(int status)
Solo.CLOSED
and
Solo.OPENED
.
status
- the status that the
NavigationDrawer
should be set to
public void setSlidingDrawer(int index, int status)
Solo.CLOSED
and
Solo.OPENED
.
index
- the index of the
SlidingDrawer
status
- the status to set the
SlidingDrawer
public void setSlidingDrawer(android.widget.SlidingDrawer slidingDrawer, int status)
Solo.CLOSED
and
Solo.OPENED
.
slidingDrawer
- the
SlidingDrawer
status
- the status to set the
SlidingDrawer
public void enterText(int index, String text)
index
- the index of the
EditText
.
0
if only one is available
text
- the text to enter in the
EditText
field
public void enterText(android.widget.EditText editText, String text)
editText
- the
EditText
to enter text in
text
- the text to enter in the
EditText
field
public 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
field
public void typeText(int index, String text)
index
- the index of the
EditText
.
0
if only one is available
text
- the text to type in the
EditText
field
public void typeText(android.widget.EditText editText, String text)
editText
- the
EditText
to type text in
text
- the text to type in the
EditText
field
public 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
field
public 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
field
match
- if multiple objects match, this determines which one will be typed in
public void typeTextInWebElement(WebElement webElement, String text)
webElement
- the WebElement to type text in
text
- the text to enter in the
WebElement
field
public void clearEditText(int index)
index
- the index of the
EditText
to clear. 0 if only one is available
public void clearEditText(android.widget.EditText editText)
editText
- the
EditText
to clear
public 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 available
public android.widget.EditText getEditText(int index)
index
- the index of the
EditText
.
0
if only one is available
EditText
matching the specified index
public android.widget.Button getButton(int index)
index
- the index of the
Button
.
0
if only one is available
Button
matching the specified index
public android.widget.TextView getText(int index)
index
- the index of the
TextView
.
0
if only one is available
TextView
matching the specified index
public android.widget.ImageView getImage(int index)
index
- the index of the
ImageView
.
0
if only one is available
ImageView
matching the specified index
public android.widget.ImageButton getImageButton(int index)
index
- the index of the
ImageButton
.
0
if only one is available
ImageButton
matching the specified index
public android.widget.TextView getText(String text)
text
- the text that is displayed, specified as a regular expression
TextView
displaying the specified text
public android.widget.TextView getText(String text, boolean onlyVisible)
text
- the text that is displayed, specified as a regular expression
onlyVisible
-
true
if only visible texts on the screen should be returned
TextView
displaying the specified text
public android.widget.Button getButton(String text)
text
- the text that is displayed, specified as a regular expression
Button
displaying the specified text
public android.widget.Button getButton(String text, boolean onlyVisible)
text
- the text that is displayed, specified as a regular expression
onlyVisible
-
true
if only visible buttons on the screen should be returned
Button
displaying the specified text
public android.widget.EditText getEditText(String text)
text
- the text that is displayed, specified as a regular expression
EditText
displaying the specified text
public android.widget.EditText getEditText(String text, boolean onlyVisible)
text
- the text that is displayed, specified as a regular expression
onlyVisible
-
true
if only visible EditTexts on the screen should be returned
EditText
displaying the specified text
public android.view.View getView(int id)
id
- the R.id of the
View
to return
View
matching the specified id
public android.view.View getView(int id, int index)
id
- the R.id of the
View
to return
index
- the index of the
View
.
0
if only one is available
View
matching the specified id and index
public android.view.View getView(String id)
id
- the id of the
View
to return
View
matching the specified id
public android.view.View getView(String id, int index)
id
- the id of the
View
to return
index
- the index of the
View
.
0
if only one is available
View
matching the specified id and index
public <T extends android.view.View> T getView(Class<T> viewClass, int index)
viewClass
- the class of the requested view
index
- the index of the
View
.
0
if only one is available
View
matching the specified class and index
public 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 available
WebElement
matching the specified index
public String getWebUrl()
public ArrayList<android.view.View> getCurrentViews()
ArrayList
of the
View
objects currently displayed in the focused window
public <T extends android.view.View> ArrayList<T> getCurrentViews(Class<T> classToFilterBy)
classToFilterBy
- return all instances of this class. Examples are:
Button.class
or
ListView.class
ArrayList
of
View
s matching the specified
Class
located in the current
Activity
public <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.class
includeSubclasses
- include instances of the subclasses in the
ArrayList
that will be returned
ArrayList
of
View
s matching the specified
Class
located in the current
Activity
public <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.class
parent
- the parent
View
for where to start the traversal
ArrayList
of
View
s matching the specified
Class
located under the specified
parent
public <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.class
includeSubclasses
- include instances of subclasses in the
ArrayList
that will be returned
parent
- the parent
View
for where to start the traversal
ArrayList
of
View
s matching the specified
Class
located under the specified
parent
public ArrayList<WebElement> getWebElements()
ArrayList
of all the
WebElement
objects currently displayed in the active WebView
public 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 WebView
public ArrayList<WebElement> getCurrentWebElements()
ArrayList
of the
WebElement
objects displayed in the active WebView
public 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 WebView
public boolean isRadioButtonChecked(int index)
index
- of the
RadioButton
to check.
0
if only one is available
true
if
RadioButton
is checked and
false
if it is not checked
public boolean isRadioButtonChecked(String text)
text
- the text that the
RadioButton
displays, specified as a regular expression
true
if a
RadioButton
matching the specified text is checked and
false
if it is not checked
public boolean isCheckBoxChecked(int index)
index
- of the
CheckBox
to check.
0
if only one is available
true
if
CheckBox
is checked and
false
if it is not checked
public boolean isToggleButtonChecked(String text)
text
- the text that the
ToggleButton
displays, specified as a regular expression
true
if a
ToggleButton
matching the specified text is checked and
false
if it is not checked
public boolean isToggleButtonChecked(int index)
index
- of the
ToggleButton
to check.
0
if only one is available
true
if
ToggleButton
is checked and
false
if it is not checked
public boolean isCheckBoxChecked(String text)
text
- the text that the
CheckBox
displays, specified as a regular expression
true
if a
CheckBox
displaying the specified text is checked and
false
if it is not checked
public boolean isTextChecked(String text)
text
- the text that the
CheckedTextView
or
CompoundButton
objects display, specified as a regular expression
true
if the specified text is checked and
false
if it is not checked
public boolean isSpinnerTextSelected(String text)
text
- the text that is expected to be selected, specified as a regular expression
true
if the specified text is selected in any
Spinner
and false if it is not
public boolean isSpinnerTextSelected(int index, String text)
index
- the index of the spinner to check.
0
if only one spinner is available
text
- the text that is expected to be selected, specified as a regular expression
true
if the specified text is selected in the specified
Spinner
and false if it is not
public void hideSoftKeyboard()
public void unlockScreen()
public void sendKey(int key)
key
- the key to be sent. Use
Solo.
RIGHT
,
LEFT
,
UP
,
DOWN
,
ENTER
,
MENU
,
DELETE
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 not
public 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 wait
true
if
Activity
appears before the timeout and
false
if it does not
public boolean waitForActivity(Class<? extends android.app.Activity> activityClass)
activityClass
- the class of the
Activity
to wait for. Example is:
MyActivity.class
true
if
Activity
appears before the timeout and
false
if it does not
public boolean waitForActivity(Class<? extends android.app.Activity> activityClass, int timeout)
activityClass
- the class of the
Activity
to wait for. Example is:
MyActivity.class
timeout
- the amount of time in milliseconds to wait
true
if
Activity
appears before the timeout and
false
if it does not
public boolean waitForEmptyActivityStack(int timeout)
timeout
- the amount of time in milliseconds to wait
true
if activity stack is empty before the timeout and
false
if it is not
public boolean waitForFragmentByTag(String tag)
tag
- the name of the tag
true
if fragment appears and
false
if it does not appear before the timeout
public boolean waitForFragmentByTag(String tag, int timeout)
tag
- the name of the tag
timeout
- the amount of time in milliseconds to wait
true
if fragment appears and
false
if it does not appear before the timeout
public boolean waitForFragmentById(int id)
id
- the R.id of the fragment
true
if fragment appears and
false
if it does not appear before the timeout
public boolean waitForFragmentById(int id, int timeout)
id
- the R.id of the fragment
timeout
- the amount of time in milliseconds to wait
true
if fragment appears and
false
if it does not appear before the timeout
public boolean waitForLogMessage(String logMessage)
logMessage
- the log message to wait for
true
if log message appears and
false
if it does not appear before the timeout
clearLog()
public boolean waitForLogMessage(String logMessage, int timeout)
logMessage
- the log message to wait for
timeout
- the amount of time in milliseconds to wait
true
if log message appears and
false
if it does not appear before the timeout
clearLog()
public void clearLog()
public String getString(int id)
id
- the R.id of the String
public String getString(String id)
id
- the id of the String
public void sleep(int time)
time
- the time in milliseconds that Robotium should sleep
public void finalize() throws Throwable
finalize
in class
Object
Throwable
finishOpenedActivities() to close the activities that have been active
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 screenshot
public 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 screenshot
quality
- 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 screenshot
public 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 screenshot
quality
- the compression rate. From 0 (compress for lowest size) to 100 (compress for maximum quality)
frameDelay
- the time in milliseconds to wait between each frame
maxFrames
- 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