android - 网络视图上的浓缩咖啡
问题描述
我在网络视图中有我的应用程序的注册页面。现在我正在为此编写测试用例,但无法运行测试。
在我的主活动中,我有 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);
}
我通过 MainActivity 打开 webview 的意图是:
Intent intent = new Intent(getContext(), ActivityWebView.class);
intent.putExtra(Extras.URL, "https://www.racq.com.au/register?device=mobile");
startActivity(intent);
bottomToTopAnimation();
现在我为这个 webview 写的测试用例如下:
@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:类 Landroid/support/test/internal/runner/tracker/UsageTracker 中没有接口方法 trackUsage(Ljava/lang/String;)V;或其超类(“android.support.test.internal.runner.tracker.UsageTracker”的声明出现在/data/app/com.racq.racq.test-2/base.apk)在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) 在 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。
请让我知道如何解决这个问题。
问候
解决方案
您可以使用“UiDevice”来实现这一点。
只需添加此依赖项:
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
并使用下面的代码来处理 webview 元素:
@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();
} } }
您甚至可以在测试用例中同时使用 espresso 和 uiautomator 来处理点击和填充数据。
希望能帮助到你!!!!
推荐阅读
- python-2.7 - 从 csv 文件中提取法语字符并使用它们更新要素类 (ArcGIS 10.4 & Python 2.7.10)
- xamarin.android - Xamarin Android 异步崩溃
- java - Thorntail/swarm:不能使用 XAdatasources?
- php - 如何在 Lumen 中定义外观?
- intellij-idea - 使用带有 IntelliJ 的 Project Lombok 进行模糊方法调用
- javascript - Highcarts.js 系列标签对齐行为
- spring - Spring JPA :: 找不到能够从类型转换的转换器
- python - 替换字符串列中的数据框值,从另一列获取要替换的值
- php - file_get_contents(): http:// wrapper 在服务器配置中被allow_url_fopen=0 禁用
- python - python 3 (Qt) [Errno 13] 权限被拒绝。如何获取文件夹路径?