0

I'm using Watir WebDriver with Firefox.

Here are two asserts for the same element. The first works, but not the second:

assert_match(/Please add user to GMT/, @browser.small(:class, "error").text)
assert_match(/Please add user to GMT/, @browser.div(:class, "eight mobile-three columns").small(:class, "error").text)

I need the second assert to work, because there are 8 error messages on the page, which are presented if the user does not populate 8 mandatory fields. All 8 errors have the identical HTML. So, I need to be able to step down from the parent.

Here is the HTML:

<div class="eight mobile-three columns">
  <a id="btnAddUserToGMT" class="success radius button expand error" onclick="AddUserToGMT();" data-reveal-id="addToGMT" href="#"> … </a>
  <small class="error" style="margin-top:0px;">

    Please add user to GMT

  </small>
</div>

Here is the error message:

Watir::Exception::UnknownObjectException: unable to locate element, using {:class=>"error", :tag_name=>"small"}
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:490:in `assert_exists'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:85:in `text'
C:/Documents and Settings/Asserts_01.rb:22:in `testBasic'

The complaint for the second assert is:

unable to locate element, using {:class=>"error", :tag_name=>"small"}

And yet that same using was OK for the first assert.

4

1 回答 1

0

问题解决了。

在与开发人员讨论后,似乎通过对自动生成的 HTML 进行一些不寻常的操作,错误消息的文本出现在页面上的正确位置。但是断言必须基于不同的标记,该标记在页面上某个完全不同的位置指定。

例如,我试图在正确的位置对此代码进行断言:

<small class="error" style="margin-top:0px;">

  Gender is required

</small

甚至由 FirePath 生成的唯一 xPath 也无法找到它。

我应该断言的是页面完全不同部分上的隐藏标签。

<input id="errorMsgGenderID" name="errorMsgGenderID" type="hidden" value="Gender is required" />

每个未填充的必填字段都有几个这样的标签,所有这些标签都放在同一行上。它们都是“输入”标签,这让我感到困惑。

于 2013-10-22T08:18:23.850 回答