I try to do automatic test with Espresso but I have some problem with the Login test.
My login page is a webview and all times I want to execute this test before execute any other test in order to start to a specific point.
The work flow is
- WebView1 -> Select an Item
- Webview2 -> Put user/password
-
Assert if the SplashScreen Opens
@RunWith(AndroidJUnit4.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class LogInTest { private final String TAG = this.getClass().getSimpleName();
@Rule public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class); @Before public void setUp() { onWebView().forceJavascriptEnabled(); } @Test public void testLogIn()throws Exception { IdlingPolicies.setMasterPolicyTimeout( 1000 * 30, TimeUnit.MILLISECONDS); IdlingPolicies.setIdlingResourceTimeout( 1000 * 30, TimeUnit.MILLISECONDS); try { onWebView(). withElement(findElement(Locator.NAME, "Select")) .perform(webClick()) .reset(); }catch (Exception e){ Log.d(TAG, e.getMessage()); } onWebView(). withElement(findElement(Locator.ID, "username")) .perform(clearElement()) .perform(DriverAtoms.webKeys("XXXXX")) .withElement(findElement(Locator.ID, "password")) .perform(clearElement()) .perform(DriverAtoms.webKeys("yyyyyy")) .withElement(findElement(Locator.NAME, "Sign On")) .perform(webClick()) .reset(); ViewVisibilityIdlingResource idlingResource = new ViewVisibilityIdlingResource(AvisDetailActivity.class.getSimpleName()); Espresso.registerIdlingResources(idlingResource); }
The test works well because the Login works but I have 2 problem :
- How could I wait the new activity? ( seems the IdlingResource doesn't work )
- How could I check for example if the splash screen is opened?
Here the code of idlingResource :
public class ViewVisibilityIdlingResource implements IdlingResource {
private ResourceCallback mResourceCallback;
private String mActivitySimpleName;
public ViewVisibilityIdlingResource(String activitySimpleName) {
this.mActivitySimpleName = activitySimpleName;
}
@Override
public final String getName() {
return ViewVisibilityIdlingResource.class.getSimpleName();
}
@Override
public boolean isIdleNow() {
Activity activity = getCurrentActivity();
if (activity == null || activity.getClass().getSimpleName() != mActivitySimpleName){
return false;
}else{
mResourceCallback.onTransitionToIdle();
return true;
}
}
private Activity getCurrentActivity() {
final Activity[] activity = new Activity[1];
java.util.Collection<Activity> activities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = Iterables.getOnlyElement(activities);
return activity[0];
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
mResourceCallback = resourceCallback;
}
Thank for anyone can help me..!
Aucun commentaire:
Enregistrer un commentaire