編輯:關於Android編程
關於Android的自動化測試之UI測試,之前介紹過Android 自動化測試(4)
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
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
public Solo(android.app.Instrumentation instrumentation, android.app.Activity activity)
instrumentation
- the Instrumentation
instanceactivity
- the start Activity
or null
if no Activity is specified
public Solo(android.app.Instrumentation instrumentation, Solo.Config config)
instrumentation
- the Instrumentation
instanceconfig
- the Solo.Config
instance
public 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 specified
public Solo(android.app.Instrumentation instrumentation)
instrumentation
- the Instrumentation
instance
public android.app.Instrumentation.ActivityMonitor getActivityMonitor()
public Solo.Config getConfig()
public ArrayList getViews()
ArrayList
of the View
objects located in the focused window
public ArrayList getViews(android.view.View parent)
parent
- the parent view from which to return the viewsReturns:an 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 requestedReturns:the top parent View
public boolean waitForText(String text)
text
- the text to wait for, specified as a regular expressionReturns: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 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 waitReturns: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 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 performedReturns: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 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 forReturns: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 forReturns: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 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 waitReturns: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 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 performedReturns:true
if the View
is displayed and false
if it is not displayed before the timeout
publicboolean waitForView(Class viewClass)
viewClass
- the View
class to wait forReturns:true
if the View
is displayed and false
if it is not displayed before the timeout
publicboolean waitForView(android.view.View view)
view
- the View
object to wait forReturns:true
if the View
is displayed and false
if it is not displayed before the timeout
publicboolean 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 performedReturns:true
if the View
is displayed and false
if it is not displayed before the timeout
publicboolean waitForView(Class 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 waitReturns:true
if the View
is displayed and false
if it is not displayed before the timeout
publicboolean waitForView(Class 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 performedReturns: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")
Returns: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 waitscroll
- true
if scrolling should be performedReturns: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 matchestimeout
- the the amount of time in milliseconds to waitscroll
- true
if scrolling should be performedReturns: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 fortimeout
- the amount of time in milliseconds to waitReturns: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 forReturns: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.
Parameters:text
- the text to search for. The parameter will be interpreted as a regular expressionReturns: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.
Parameters: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 searchedReturns: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.
Parameters:text
- the text to search for. The parameter will be interpreted as a regular expressionReturns: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.
Parameters: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 foundReturns: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.
Parameters: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 searchedReturns: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.
Parameters: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 foundReturns: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.
Parameters:text
- the text to search for. The parameter will be interpreted as a regular expressionReturns: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.
Parameters: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 searchedReturns: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.
Parameters: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 foundReturns: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.
Parameters: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 performedReturns: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.
Parameters: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 searchedReturns: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 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.class
public 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 Activity
public 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.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 waitReturns: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 waitReturns: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 coordinatey
- the y coordinate
public void clickOnScreen(float x, float y, int numberOfClicks)
x
- the x coordinatey
- the y coordinatenumberOfClicks
- the number of clicks to perform
public void clickLongOnScreen(float x, float y)
x
- the x coordinatey
- the y coordinate
public void clickLongOnScreen(float x, float y, int time)
x
- the x coordinatey
- the y coordinatetime
- 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 expressionsubMenu
- 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 clickscroll
- 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.
Parameters:index
- the index of the MenuItem
to press
public void pressMenuItem(int index, int itemsPerRow)
index
- the index of the MenuItem
to pressitemsPerRow
- 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 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 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 clickimmediately
- 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 clicktime
- 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 expressionmatch
- 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 expressionmatch
- if multiple objects match the text, this determines which one to clickscroll
- 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 expressionmatch
- 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 expressionmatch
- if multiple objects match the text, this determines which one to clickscroll
- 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 expressionmatch
- if multiple objects match the text, this determines which one to clicktime
- 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 expressionindex
- 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 clickInList(int line)
line
- the line to clickReturns:an ArrayList
of the TextView
objects located in the list line
public ArrayList clickInList(int line, int index)
line
- the line to clickindex
- the index of the list. 0
if only one is availableReturns:an ArrayList
of the TextView
objects located in the list line
public ArrayList clickLongInList(int line)
line
- the line to clickReturns:an ArrayList
of the TextView
objects located in the list line
public ArrayList clickLongInList(int line, int index)
line
- the line to clickindex
- the index of the list. 0
if only one is availableReturns:an ArrayList
of the TextView
objects located in the list line
public ArrayList 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 clickReturns:an 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 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 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 scrollReturns:true
if more scrolling can be performed
public boolean scrollListToBottom(android.widget.AbsListView list)
list
- the AbsListView
to scrollReturns:true
if more scrolling can be performed
public boolean scrollUpList(android.widget.AbsListView list)
list
- the AbsListView
to scrollReturns:true
if more scrolling can be performed
public boolean scrollListToTop(android.widget.AbsListView list)
list
- the AbsListView
to scrollReturns: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 availableReturns: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 availableReturns: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 availableReturns: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 availableReturns:true
if more scrolling can be performed
public void scrollListToLine(android.widget.AbsListView absListView, int line)
absListView
- the AbsListView
to scrollline
- the line to scroll to
public void scrollListToLine(int index, int line)
index
- the index of the AbsListView
to scrollline
- 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.60stepCount
- 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 scrollside
- 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.60stepCount
- 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 scrollside
- 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 scrollside
- 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 screenstartPoint2
- Second "finger" down on the screenendPoint1
- Corresponding ending point of startPoint1endPoint2
- 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 screenstartPoint2
- Second "finger" down on the screenendPoint1
- Corresponding ending point of startPoint1endPoint2
- 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 availableyear
- the year e.g. 2011monthOfYear
- the month which starts from zero e.g. 0 for JanuarydayOfMonth
- the day e.g. 10
public 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. 10
public 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. 30
public void setTimePicker(android.widget.TimePicker timePicker, int hour, int minute)
timePicker
- the TimePicker
objecthour
- the hour e.g. 15minute
- the minute e.g. 30
public void setProgressBar(int index, int progress)
SeekBar
and RatingBar
.
Parameters: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
.
Parameters:progressBar
- the ProgressBar
progress
- the progress to set the ProgressBar
public void setNavigationDrawer(int status)
Solo.CLOSED
and Solo.OPENED
.
Parameters:status
- the status that the NavigationDrawer
should be set to
public void setSlidingDrawer(int index, int status)
Solo.CLOSED
and Solo.OPENED
.
Parameters: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
.
Parameters: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 availabletext
- the text to enter in the EditText
field
public void enterText(android.widget.EditText editText, String text)
editText
- the EditText
to enter text intext
- 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 availabletext
- the text to type in the EditText
field
public void typeText(android.widget.EditText editText, String text)
editText
- the EditText
to type text intext
- 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
fieldmatch
- 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 intext
- 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 availableReturns:an EditText
matching the specified index
public android.widget.Button getButton(int index)
index
- the index of the Button
. 0
if only one is availableReturns:a Button
matching the specified index
public android.widget.TextView getText(int index)
index
- the index of the TextView
. 0
if only one is availableReturns:a TextView
matching the specified index
public android.widget.ImageView getImage(int index)
index
- the index of the ImageView
. 0
if only one is availableReturns:an ImageView
matching the specified index
public android.widget.ImageButton getImageButton(int index)
index
- the index of the ImageButton
. 0
if only one is availableReturns:the ImageButton
matching the specified index
public android.widget.TextView getText(String text)
text
- the text that is displayed, specified as a regular expressionReturns:the TextView
displaying the specified text
public 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 returnedReturns:the TextView
displaying the specified text
public android.widget.Button getButton(String text)
text
- the text that is displayed, specified as a regular expressionReturns:the Button
displaying the specified text
public 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 returnedReturns:the Button
displaying the specified text
public android.widget.EditText getEditText(String text)
text
- the text that is displayed, specified as a regular expressionReturns:the EditText
displaying the specified text
public 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 returnedReturns:the EditText
displaying the specified text
public android.view.View getView(int id)
id
- the R.id of the View
to returnReturns:a View
matching the specified id
public 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 availableReturns:a View
matching the specified id and index
public android.view.View getView(String id)
id
- the id of the View
to returnReturns:a View
matching the specified id
public 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 availableReturns:a View
matching the specified id and index
publicT getView(Class viewClass, int index)
viewClass
- the class of the requested viewindex
- the index of the View
. 0
if only one is availableReturns:a 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 availableReturns:a WebElement
matching the specified index
public String getWebUrl()
public ArrayList getCurrentViews()
ArrayList
of the View
objects currently displayed in the focused window
publicArrayList getCurrentViews(Class classToFilterBy)
classToFilterBy
- return all instances of this class. Examples are: Button.class
or ListView.class
Returns:an ArrayList
of View
s matching the specified Class
located in the current Activity
publicArrayList getCurrentViews(Class 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 returnedReturns:an ArrayList
of View
s matching the specified Class
located in the current Activity
publicArrayList getCurrentViews(Class 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 traversalReturns:an ArrayList
of View
s matching the specified Class
located under the specified parent
publicArrayList getCurrentViews(Class 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 returnedparent
- the parent View
for where to start the traversalReturns:an ArrayList
of View
s matching the specified Class
located under the specified parent
public ArrayListgetWebElements()
ArrayList
of all the WebElement
objects currently
displayed in the active WebView
public ArrayListgetWebElements(By by)
by
- the By object. Examples are: By.id("id")
and By.name("name")
Returns:an ArrayList
of all the WebElement
objects displayed
in the active WebView
public ArrayListgetCurrentWebElements()
ArrayList
of the WebElement
objects displayed
in the active WebView
public ArrayListgetCurrentWebElements(By by)
by
- the By object. Examples are: By.id("id")
and By.name("name")
Returns:an 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 availableReturns: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 expressionReturns: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 availableReturns: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 expressionReturns: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 availableReturns: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 expressionReturns: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 expressionReturns: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 expressionReturns: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 availabletext
- the text that is expected to be selected, specified as a regular expressionReturns: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"
Returns: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 waitReturns: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
Returns: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 waitReturns: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 waitReturns: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 tagReturns: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 tagtimeout
- the amount of time in milliseconds to waitReturns: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 fragmentReturns: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 fragmenttimeout
- the amount of time in milliseconds to waitReturns: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 forReturns:true
if log message appears and false
if it does not appear before the timeoutSee Also:clearLog()
public boolean waitForLogMessage(String logMessage, int timeout)
logMessage
- the log message to wait fortimeout
- the amount of time in milliseconds to waitReturns:true
if log message appears and false
if it does not appear before the timeoutSee Also:clearLog()
public void clearLog()
public String getString(int id)
id
- the R.id of the StringReturns:the localized String
public String getString(String id)
id
- the id of the StringReturns:the localized String
public void sleep(int time)
time
- the time in milliseconds that Robotium should sleep
public void finalize() throws Throwable
finalize
in class Object
Throws:Throwable
See Also: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.
Parameters: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.
Parameters: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);
Parameters: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.
Parameters: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{ 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
魅族發布了今年最後一款歷史性新品---魅藍metal,魅藍metal依舊采用了與或卡托(單卡槽雙卡位)設計,且支持雙卡雙待,目前預約的有移動定制版和公開版。
本文實例講述了Android實現模仿UCweb菜單效果的方法。分享給大家供大家參考。具體如下:UCWeb的菜單看起來不錯,自己模仿做一個,思路實現如下:1、保留menu按
socket編程是網絡通信的一個基礎應用,無論是手機端還是PC端都需要socket技術來建立網絡通信。在本章小編主要從以下幾個方面來介紹socket的相關知識:分別是&l
Android系統提供了5中語言的語音合成German,English,Spanish,French,Italian支持,其中不包括對中文的支持,但是Android系統允