适用于Chrome和IE的Watir-Webdriver - 在自动输入文本字段时出现nil错误

时间:2014-02-28 21:23:58

标签: watir watir-webdriver selenium-chromedriver

我正在使用Watir-Webdriver。这个脚本适用于我的Firefox,但是在Google和IE上,一旦我将数据输入到模态中,它就会破坏。我还注意到IEDriver与ChromeDriver和Firefox相比,将文本输入文本字段的速度非常慢,这也打破了脚本。

这是我运行ChromeDriver或IEDriver时出现的错误:

    ===============================================================================
    Failure: <nil> is not true.
    test_orderjellybean(TestShoppingCart)
    C:/Users/cameron.sampson/Desktop/shopping cart auto/shoppingcart-jellybean.rb:69
    :in `test_orderjellybean'
         66:
         67:                # Select Monthly Plans for data, minutes and text
         68:                assert @b.div(:class, 'modal-body')
      => 69:                assert @b.text_field(:id, 'ZipCode').set '92130'
         70:                assert @b.button(:value, 'Confirm').click
         71:                sleep 5
         72:
    ===============================================================================

我更新了ChromeDriver,Watir-Webdriver和Selenium-Webdriver。我应该安装一些其他的gem来使跨浏览器自动化更加实用吗?

以下是我运行它的HTML源代码:

    <div class="modal fade" id="modal-checkplans">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <h3>Confirm Coverage</h3>
                    <div class="alert alert-danger fade hidden">
                        <p><strong>We are sorry :(</strong>
                            <br>
                            This product should work almost anywhere in the U.S. where it is available, your address is one of the few locations where coverage is not available. If you feel that this is an error, please call customer service at 000-000-0000. </p>
                    </div>
                    <form class="form-ajax form-inline" action="/Cart/cart/checkservice" data-callback="OnCheckNetworkCoverageComplete">
                        <label for="PostalCode">Enter the zip code where the phone will be used most often:</label>
                        <div class="input-group col-4 col-8">
                            <input autofocus="autofocus" class="form-control" data-val="true" data-val-length="Postal code must be between 5 and 10 digits." data-val-length-max="10" data-val-length-min="5" data-val-required="*required" id="ZipCode" name="ZipCode" type="text" value="" />
                            <span class="input-group-btn">
                                <input type="submit" class="btf btf-info" data-loading-text="Checking..." value="Confirm" />
                            </span>
                        </div>
                        <span class="field-validation-valid" data-for="ZipCode" data-replace="true"></span>
                    </form>
                </div>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:2)

看起来Chromedriver和Firefoxdriver与设置文本字段时返回的值不一致:

# Chrome
@b.text_field(:id, 'ZipCode').set '92130'
#=> ""

# Firefox
@b.text_field(:id, 'ZipCode').set '92130'
#=> nil

这就是为什么你的测试对于Firefox而不是Chrome的测试失败的原因 - 即零值将无法通过断言。

那就是说,我认为根本问题是assert被使用的方式。在任何这些行上执行断言都没有意义。

例如,行:

assert @b.div(:class, 'modal-body')

永远不会过去。 div方法始终返回Watir对象,无论页面上是否存在该元素。这意味着assert永远不会失败。

您应删除所显示行的assert。然后添加assert以测试特定值。