Calabash-android:触摸因“未找到元素”而失败,但找到了元素

时间:2014-06-06 16:41:43

标签: java cucumber cucumber-jvm calabash

我使用calabash-android-java来测试使用Xamarin构建的混合应用程序。正如您在下面看到的,我能够查询DOM元素,甚至将一些信息记录到控制台,但是当我执行touch命令时,无论出于何种原因,都无法再找到该元素。

我看了一下calabash-android-java source code中的失败点,但它似乎只是将我的查询传递给脚本引擎,这是我可以接受的。

我只是误解了touch命令的使用?我认为它应该能够触摸"一个锚标签,但也许不是?

无论如何,任何帮助都会受到高度赞赏......我的谷歌今天让我失望了。

HTML

<div style="width: 100px">
    <a data-role="button" ng-click="submit()" id="loginButton">Log In</a>
</div>

爪哇

@When("^user \"(.*?)\" logs in with valid credentials$")
public void user_logs_in_with_valid_credentials(String username) throws Throwable {
    UIElement login = app.query("webView css:'#loginButton'").first();
    System.out.println("Element_Id=" + login.getId());
    System.out.println("Element_Class=" + login.getElementClass());
    System.out.println("Element_Query=" + login.getQuery());
    System.out.println("Element_Text=" + login.getText());
    System.out.println("Element_Enabled=" + login.isEnabled());
    Rect rect = login.getRect();
    System.out.println("Center_X=" + rect.getCenter_x());
    System.out.println("Center_Y=" + rect.getCenter_y());
    login.touch();
}

控制台输出

Element_Id=loginButton
Element_Class=ui-btn ui-btn-up-c ui-shadow ui-btn-corner-all
Element_Query=webView css:'#loginButton' index:0
Element_Text=
Element_Enabled=false
Center_X=91.98979
Center_Y=308.5714 
com.thoughtworks.calabash.android.CalabashException: Failed to touch on: webView css:'#loginButton' index:0. (RuntimeError) No elements found. Query: webView css:'#loginButton' index:0
at com.thoughtworks.calabash.android.CalabashWrapper.touch(CalabashWrapper.java:293)
at com.thoughtworks.calabash.android.UIElement.touch(UIElement.java:196)
at com.mycompany.calabash.LoginStepDefs.user_logs_in_with_valid_credentials(LoginStepDefs.java:41)
at ?.When user "xxx@xx" logs in with valid credentials(C:/_Workspace/calabash-android-java/calabash-android-java/src/test/resources/features/login.feature:13)

更新:值得注意的是,在控制台模式下,我可以使用相同的查询触摸元素,并且设备会做出相应的响应:

irb(main):023:0> touch("webView css:'#loginButton'")
{
    "bonusInformation" => [],
             "message" => "",
             "success" => true
}

2 个答案:

答案 0 :(得分:0)

通过VishnuGitHub通话后,我能够更改源代码以解决错误:

注意,必须截断附加到查询变量的字符串“index:N”,然后将其传递给Ruby脚本:

<强> CalabashWrapper.java

public void touch(String query) throws CalabashException {
    try {
        info("Touching - %s", query);
        query = query.substring(0, query.indexOf("index:"));
        container.put(QUERY_STRING, query);
        container.runScriptlet(String.format("touch(%s)", QUERY_STRING));
        pause();
    } catch (Exception e) {
        error("Failed to touch on: %s", e, query);
        throw new CalabashException(String.format("Failed to touch on: %s. %s", query, e.getMessage()));
    }

}

答案 1 :(得分:0)

我更新到calabash android 0.5.1,这有助于处理更好的触摸和设置web元素上的文本。 Webview现在处理得更好(与calabash android一致)。

https://github.com/vishnukarthikl/calabash-android-java/issues/3