1
votes

I have my application's registration page inside the web-view. Now i am writing my test case for that but not able to run the test.

In my Main Activity, I have initUi function which will trigger intent to my webviewActivity and inside my webviewActivity, there is a FragWebView in which url is getting load.

Here is the sample code from FragWebView :

 WebView webView = (WebView) layout.findViewById(R.id.web_view);
        String url = getArguments().getString(Extras.URL, null);
        if(url != null){
            webView.clearCache(true);
            webView.clearHistory();

            webView.getSettings().setJavaScriptEnabled(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                WebView.setWebContentsDebuggingEnabled(true);
            }

            webView.loadUrl(url);
            webView.setVisibility(View.VISIBLE);
        } else {
            webView.setVisibility(View.GONE);
        }

Intent I am passing to open webview from my MainActivity is :

Intent intent = new Intent(getContext(), ActivityWebView.class);

        intent.putExtra(Extras.URL, "https://www.racq.com.au/register?device=mobile");

        startActivity(intent);
        bottomToTopAnimation();

Now the Test case which I wrote for this webview is as follows :

@RunWith(AndroidJUnit4.class)
public class ActivityRegistrationTest {

    @Rule
    public ActivityTestRule<ActivityWebView> mActivityRule =
            new ActivityTestRule<ActivityWebView>(ActivityWebView.class,
                    false, false) {
                @Override
                protected void afterActivityLaunched() {
                    onWebView().forceJavascriptEnabled();
                }
            };

    @Test
    public void testSingUpWebPage()throws Exception

    {
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        // check WELCOME text is present
        onWebView().withElement(findElement(Locator.ID, "register"))
                .check(webMatches(getText(), containsString("Register")));
        //Check UI elements text boxes and buttons are present on the page
        onWebView().check(webContent(hasElementWithId("l")));
        onWebView().check(webContent(hasElementWithId("phracq_body_0_phracq_contentcontainer_0_ucFirstName_txt")));
        onWebView().check(webContent(hasElementWithId("phracq_body_0_phracq_contentcontainer_0_ucLastName_txt")));
        onWebView().check(webContent(hasElementWithId("date_of_birth")));
        onWebView().check(webContent(hasElementWithId("phracq_body_0_phracq_contentcontainer_0_ucPostCode_txt")));
        //enter values
        onWebView().withElement(findElement(Locator.ID,"phracq_body_0_phracq_contentcontainer_0_ucFirstName_txt")).perform(webKeys("siddharth"));
    }
}

Here is the list of versions I am using inside application :

 // Espreso Dependencies
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        androidTestImplementation 'com.android.support.test:rules:1.0.2'
        androidTestImplementation("com.android.support.test.espresso:espresso-contrib:2.2.2") {
            exclude group: 'com.android.support', module: 'appcompat'
            exclude group: 'com.android.support', module: 'support-v4'
            exclude group: 'com.android.support', module: 'support-v7'
            exclude group: 'com.android.support', module: 'design'
            exclude module: 'support-annotations'
            exclude module: 'recyclerview-v7'
        }
        androidTestImplementation('com.android.support.test.espresso:espresso-web:2.2') {
            exclude group: 'com.android.support', module: 'support-annotations'
        }

Error i am getting :

java.lang.NoSuchMethodError: No interface method trackUsage(Ljava/lang/String;)V in class Landroid/support/test/internal/runner/tracker/UsageTracker; or its super classes (declaration of 'android.support.test.internal.runner.tracker.UsageTracker' appears in /data/app/com.racq.racq.test-2/base.apk) at android.support.test.espresso.web.sugar.Web.(Web.java:64) at com.mnetmobile.racq.ActivityRegistrationTest.setUp(ActivityRegistrationTest.java:36) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:76) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1984)

please let me know how to resolve this.

Regards

1

1 Answers

2
votes

You can achieve this using "UiDevice".

Just add this dependencie :

androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'

And use bellow code to deal with webview elements:

@RunWith(AndroidJUnit4.class)
public class ActivityRegistrationTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule =
            new ActivityTestRule<MainActivity>(MainActivity.class,
                    false, false);
    @Before
    public void setUp() {
        mActivityRule.launchActivity(new Intent());
    }


    @Test
    public void testSingUpWebPage()throws Exception

    {
        final UiDevice mDevice =
                UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        final int timeOut = 1000 * 60;
        mDevice.wait(Until.findObject(By.clazz(WebView.class)), timeOut);
        try {
            UiObject membershipCardInput = mDevice.findObject(new UiSelector()
                    .instance(0)
                    .className(EditText.class));

            membershipCardInput.waitForExists(timeOut);
            membershipCardInput.setText("123456789");
        } catch (Exception e) {
            e.printStackTrace();
        } } }

you can even make use of both espresso and uiautomator in your test cases inorder to handle clicks and to fill data.

Hope it helps!!!!