简体   繁体   English

尝试从Watir-Webdriver中的自定义属性中选择一个单选按钮

[英]Trying to select a radio button from a custom attribute in Watir-Webdriver

Is trying it this way not possible? 这样尝试是不可能的吗? I added the code to extend the ability to allow custom selectors: 我添加了代码以扩展允许自定义选择器的功能:

module Watir
  class ElementLocator    
    alias :old_normalize_selector :normalize_selector

    def normalize_selector(how, what)
      case how
        when :data_sku
          [how, what]
        else
          old_normalize_selector(how, what)
      end
    end
  end
end

And here is my code trying to select it from the random array. 这是我的代码尝试从随机数组中选择它。 Even with the middle two lines uncommented I still get errors. 即使中间两行没有注释,我仍然会出错。

$monthlydata = ["GC311Z-02","GC307Z-02","GC308Z-02","GC309Z-02","GC310Z-02"].sample
#Watir::Wait.until { @b.radio(:data_sku, $monthlydata) }
#@b.radio(:xpath, "//input[@data_sku='$monthlydata']").exists?
@b.radio(:xpath, "//input[@data_sku='$monthlydata']").set **(line causing the error)**

The error I get is the UnknownObjectException: unable to locate element. 我得到的错误是UnknownObjectException:无法找到元素。 The line that's throwing up the error is the last one shown. 引发错误的行是显示的最后一行。

UPDATE UPDATE

My updated code getting timeout errors: 我更新的代码收到超时错误:

    if @b.url == "http://www.website.com/cart/plans?ProductId=product"
        $monthlydata = ["GC311Z-02","GC307Z-02","GC308Z-02","GC309Z-02","GC310A-02"].sample
        assert @b.radio(:data_sku, $monthlydata).when_present.set
    end

    $monthlymin = ["GC504Z","GC505Z","GC506Z","GC507Z","GC508Z"].sample
    assert @b.radio(:data_sku, $monthlymin).when_present.set

    $monthlytxt = ["GC313Z-02","GC314Z-02","GC315Z-02","GC316Z-02","GC320Z-02"].sample
    assert @b.radio(:data_sku, $monthlytxt).when_present.set

Stacktrace: 堆栈跟踪:

Error: test_goproject(TestShoppingCart)

  Watir::Wait::TimeoutError: timed out after 30 seconds, waiting for {:data_sku=
>"GC309Z-02", :tag_name=>"input", :type=>"radio"} to become present

C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.7/lib/watir-webdriver/wa
it.rb:33:in `until'

C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.7/lib/watir-webdriver/wa
it.rb:106:in `method_missing'

C:/Users/User.Name/Downloads/shoppingcart2.rb:95:in `test_goproject'

     92:
     93:                if @b.url == "http://www.website.com/cart/plans?ProductId=product"
     94:                        $monthlydata = ["GC311Z-02","GC307Z-02","GC308Z-
02","GC309Z-02","GC310Z-02"].sZmple
  => 95:                        assert @b.radio(:data_sku, $monthlydZtZ).when_pr
esent.set
     96:                end
     97:
     98:                $monthlymin = ["GC504Z","GC505Z","GC506Z","GC507Z","GC50
8Z"].sample

I assume that the radio buttons on your page have html like: 我假设您页面上的单选按钮具有类似html的HTML:

<input type="radio" data-sku="GC311Z-02" />

Solution 1 - Data Attribute Locator 解决方案1-数据属性定位器

Watir already supports locating elements via custom data attributes. Watir已经支持通过自定义数据属性定位元素。 There is no need to do any patching of the normalize_selector method. 无需对normalize_selector方法进行任何修补。

You are correctly defining the data attribute locator when you did: 执行以下操作时,您正在正确定义数据属性定位符:

@b.radio(:data_sku, $monthlydata)

However, the line: 但是,这一行:

Watir::Wait.until { @b.radio(:data_sku, $monthlydata) }

Will not do what you expected. 不会做你所期望的。 Watir::Wait.until will wait until the block evaluates to true. Watir::Wait.until将等待,直到该块评估为true。 The line @b.radio(:data_sku, $monthlydata) will always return a truthy value as it is returning a Watir element object (one that has not yet been located). @b.radio(:data_sku, $monthlydata)行将始终返回真实值,因为它正在返回Watir元素对象(尚未定位的对象)。 Therefore, wait never waits. 因此,等待永远不会等待。 The block needs to verify that the element is present: 该块需要验证该元素是否存在:

Watir::Wait.until { @b.radio(:data_sku, $monthlydata).present? }

Though, you could shorten this to: 不过,您可以将其缩短为:

@b.radio(:data_sku => $monthlydata).wait_until_present

Or since you want to set the radio button: 或者由于您要设置单选按钮:

@b.radio(:data_sku => $monthlydata).when_present.set

Solution 2 - Xpath 解决方案2-Xpath

While I do not recommend going the xpath route in this scenario, it is possible. 虽然我不建议在这种情况下使用xpath路由,但有可能。 The reason that the line: 之所以行:

@b.radio(:xpath, "//input[@data_sku='$monthlydata']").set

fails is that $monthlydata is not properly inserted. 失败的$monthlydata是没有正确插入$monthlydata As written, the xpath says to find a data-sku attribute that has the value "$monthlydata" (not the value that was randomly selected). 如所写,xpath表示要找到一个具有值“ $ monthlydata”(而不是随机选择的值)的data-sku属性。

You need to do a string interpolation so that $monthlydata is actually interpreted rather than literal. 您需要进行字符串插值,以便实际上解释$monthlydata而不是文字。 As well, xpath requires @data-sku instead of @data_sku : 同样,xpath需要@data-sku而不是@data_sku

@b.radio(:xpath, "//input[@data-sku='#{$monthlydata}']").when_present.set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM