Issue
When I try to instantiate testAYLogout()
and run the application, testAXEndTrip()
also runs. Why is that?
public class ApplicationTest extends UiAutomatorTestCase {
public void testAYLogout() throws RemoteException, UiObjectNotFoundException {
//Get the device state
UiDevice myDevice = getUiDevice();
//menu button
UiObject menuButton = new UiObject(new UiSelector().className(android.widget.ImageButton.class.getName()).index(0));
//logout button
UiObject logoutButton = new UiObject(new UiSelector().className(android.widget.CheckedTextView.class.getName()).text("Logout"));
//yes button
UiObject yesButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Yes"));
//no button
UiObject noButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("No"));
UiObject loginText = new UiObject(new UiSelector().className(android.widget.TextView.class.getName()).text("Login"));
if (!myDevice.isScreenOn()) {
myDevice.wakeUp();
}
//click menu button and wait for new window
menuButton.clickAndWaitForNewWindow();
assertTrue(logoutButton.exists());
//click logout
logoutButton.clickAndWaitForNewWindow();
//click no
noButton.clickAndWaitForNewWindow();
menuButton.clickAndWaitForNewWindow();
logoutButton.clickAndWaitForNewWindow();
//click yes button
yesButton.clickAndWaitForNewWindow();
assertTrue(loginText.exists());
}
public void testAXEndTrip() throws RemoteException, UiObjectNotFoundException {
//get device
UiDevice myDevice = getUiDevice();
//pick up location feild
UiObject okButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Ok").index(0));
//end trip button
UiObject endTripButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("END TRIP").index(1));
//no button
UiObject noButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("No"));
//yes button
UiObject yesButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Yes"));
//submit button
UiObject submitButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Submit"));
//rate button
UiObject rateButton = new UiObject(new UiSelector().className(android.widget.ImageButton.class.getName()).index(4));
//feedback field
UiObject feedback = new UiObject(new UiSelector().className(android.widget.EditText.class.getName()).index(1));
//map
UiObject map = new UiObject(new UiSelector().className(android.view.View.class.getName()).index(0));
//wake up device if screen is off
if (!myDevice.isScreenOn())
myDevice.wakeUp();
rateButton.waitForExists(5000);
rateButton.clickAndWaitForNewWindow();
feedback.setText("ride or die ma niguh");
submitButton.clickAndWaitForNewWindow();
assertTrue(map.exists());
}
}
Then this is my main class
public class MainTest extends ApplicationTest{
public static void TestMain() throws RemoteException, UiObjectNotFoundException {
MainTest login = new MainTest();
login.testAYLogout();
}
}
Solution
You should use Testing Support Library.
First add uiautomator
dependency to your gradle file
dependencies {
...
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
Then, create a test case class like this one
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class MySampleTest extends MyAbstractTest {
...
@Test
public void myTest() {
...
}
}
Finally, run your tests. Make sure to specify AndroidJUnitRunner
as the default instrumentation runner in your project.
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
Answered By - Diego Torres Milano
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.