1

我试图找出在我的(WebView)中检查我的主要活动 URL 的最简单方法。这将在使用 Robotium 4.1 框架的单元测试中进行。我只是想弄清楚如何与 webview 交互,例如向活动发送要加载的 URL,因此我可以对此进行测试并确保加载了正确的 URL,并检查我的操作栏项目。

任何代码、建议或建议将不胜感激。

public class ErrorHandling extends
    ActivityInstrumentationTestCase2<Webview_Main> {
        private Solo solo;
        private WebView web;
 ..
protected void setUp() throws Exception {
    super.setUp();
    solo = new Solo(getInstrumentation(),getActivity());
}

根据建议更新(当前 - NullPointerException Erorrs)

public void testUrl() {
    String URL = solo.getString(com.example.webview.R.string.url);

         web = (WebView) getActivity().findViewById(R.id.webview_main); //Added

    try {
    solo.assertCurrentActivity("Launching Main Activity", Webview_Main.class);
    assertTrue(solo.getWebUrl().equals("URL")); } 
    catch (Error error) {
}
}
public void testWeb() {
    try {
                    web = (WebView) getActivity().findViewById(R.id.webview_main); //Added
        solo.getCurrentActivity().runOnUiThread(new Runnable() {
            public void run() {
                web.loadUrl("http://www.google.com");
            }
        });
        solo.sleep(1000);
    } catch (Error error) {

    }
}
4

2 回答 2

3

首先,调用

web.getSettings();

什么都没有给你。它为您提供 WebSettings,但您不对该对象执行任何操作。

要在 webview 中声明当前 url,您可以使用:

assertTrue(solo.getWebUrl().equals("your url"));

要在您的 webview 中加载指定的 url,您必须在另一个线程中运行它(这里我不确定它应该是 runOnMainSync 还是 runOnUiThread,但是我尝试了 runOnUiThread 并且它工作正常),例如:

solo.getCurrentActivity().runOnUiThread(new Runnable() {
    public void run() {
        web.loadUrl("http://www.OTHER_url_TO_test_AGAINST");
    }
});
于 2013-04-15T21:20:11.557 回答
1

检查 URL 的步骤如下:

1)为特定应用创建webview对象

obj = (WebView)getActivity().findViewById((源或apk的包名).R.id.(源webview的Id))

注意:如果您没有编写测试用例的源应用程序的源代码,则使用层次结构查看器查找 webview 的 id。

2)添加一些睡眠,以便它会等待加载 urlsolo.sleep(时间)所花费的时间

3) 使用它来获取应用程序的当前值或状态。 obj.getSettings()

4) 取一个布尔变量并存储布尔变量的值 = solo.getWebUrl().equals("待测试的 URL");

5) assertTrue(Boolean variable) 验证值真假。

于 2013-06-11T06:22:28.617 回答