网络视图上的浓缩咖啡



我在网页视图中有我的应用程序的注册页面。现在我正在为此编写测试用例,但无法运行测试。

在我的主活动中,我有 initUi 函数,它将触发对我的 webviewActivity 的意图,在我的 webviewActivity 中,有一个 FragWebView,其中 url 正在加载。

以下是来自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 intent = new Intent(getContext(), ActivityWebView.class);
intent.putExtra(Extras.URL, "https://www.racq.com.au/register?device=mobile");
startActivity(intent);
bottomToTopAnimation();

现在我为此Web视图编写的测试用例如下

@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"));
}
}

这是我在应用程序内部使用的版本列表

// 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'
}

我得到的错误

java.lang.NoSuchMethodError: no interface method trackUsage(Ljava/lang/String;(V类 Landroid/support/test/internal/runner/tracker/UsageTracker;或其 超类(声明 "android.support.test.internal.runner.tracker.UsageTracker"出现在/data/app/com.racq.racq.test-2/base.apk( at android.support.test.espresso.web.sugar.web.(网站.java:64( 在 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( 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12( 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47( 在 android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:76( 在 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( 在 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( 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384( 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1984(

请让我知道如何解决这个问题。

问候

您可以使用"UiDevice"来实现此目的。

只需添加此依赖项:

安卓测试实现 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'

并使用下面的代码来处理 Web 视图元素

@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();
} } }

您甚至可以在测试用例中同时使用浓缩咖啡和UIautomator来处理点击和填充数据。

希望对您有所帮助!!!

最新更新