问题
I'm actually trying to implement a simple test suite within an AndroidJunit Test project which uses the following class
- UiObject
- UiSelector
- UiAutomatorTestcase
to click and open the Messaging application on the Android Device and run it as AndroidJunit Test in Eclipse.
While running the code I'm get the following exception
java.lang.RuntimeException: Stub!
I don't understand where I'm going wrong. Please let me know whether we can run the UiAutomatorTestcase test suite using the AndroidJuint Test project or not.
Here is the sample code and the failure trace
public class UiautomatorTest extends
ActivityInstrumentationTestCase2<MyMainActivity> {
UiAutomatorTestCase mUiAutomatorTestCase;
public UiautomatorTest() {
super(MyMainActivity.class);`enter code here`
// TODO Auto-generated constructor stub
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
mUiAutomatorTestCase = new UiAutomatorTestCase();
}
public void testToOpenMessageApp() throws UiObjectNotFoundException {
UiObject message = new UiObject(
new UiSelector().description("Messaging"));
message.clickAndWaitForNewWindow();
UiObject composeMessage = new UiObject(
new UiSelector().description("compose"));
composeMessage.clickAndWaitForNewWindow();
UiObject typeMessage = new UiObject(
new UiSelector().description("Type Message"));
typeMessage.clickAndWaitForNewWindow();
mUiAutomatorTestCase.getUiDevice().pressHome();
}
}
The stacktrace
java.lang.RuntimeException: Stub!
at mypackagename.UiAutomatorTestCase.<init>(UiAutomatorTestCase.java:5)
at mypackagename..setUp(UiautomatorTest.java:25)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
回答1:
Android 4.3 includes a way to interact with UIAutomation from Instrumentation http://developer.android.com/reference/android/app/Instrumentation.html#getUiAutomation() and the topic is introduced at http://developer.android.com/about/versions/android-4.3.html#Testing
This seems to be another flavour of UI Test Automation. I I'd like to know more about the interaction between 4.3's UIAutomation and UIAutomator http://developer.android.com/tools/help/uiautomator/index.html
回答2:
You need to run the test on an android device. For this to work the code needs to be dexed before it can be run, etc. Read more here on how to build your test for running on android.
Run the test like so
adb shell uiautomator runtest <JARS> -c <CLASSES> [options]
From the official documentation.
To run your testcases on the target device, you can use the adb shell command to invoke the uiautomator tool.
回答3:
As standard, UI Automator tests are compiled and built into a jar file, on a desktop computer, they are then deployed to the device by pushing the jar file to the device using adb. They are then run on the device, in the Android runtime by using adb shell uiautomator ...
Thanks to https://stackoverflow.com/users/363437/vidstige for correcting my earlier post which was inaccurate.
Probably the fastest way to help you get your tests running is for you to post your code as part of your question.
Update 22 July 2013 I have not seen anyone else trying to run the test cases directly on the device rather than building them following the approach Google (Android) document. It might be possible, however you may have to work hard to discover whether your approach is possible or viable. UIAutomator is distinct and separate from the Android InstrumentationTestCase framework. Instead it interacts with the GUI and has the advantage of being able to interact with a wide range of apps, including things like the inbuilt Settings UI on Android devices.
I'd suggest you start with the documented tutorial from the Android development site http://developer.android.com/tools/testing/testing_ui.html and see if you can get that working. You'll want to use a device running Android 4.2 (or later for those of you reading this when newer versions of Android are released). Once you've got the tutorial working, I hope it'll be a good starting point to write UIAutomator tests for whatever app you're aiming to write automated tests for.
You're welcome to post another update.
回答4:
You can use like script with .sh
ant build
$youtSdkPath/platform-tools/adb shell rm /data/local/tmp/ProjectName.jar
$youtSdkPath/platform-tools/adb push $pathProject/bin/ProjectName.jar /data/local/tmp/
$youtSdkPath/platform-tools/adb shell uiautomator runtest ProjectName.jar -c ClassToTest
bash theScript.sh
来源:https://stackoverflow.com/questions/17752860/running-uiautomatortestcase-in-androidjunit-test-project