簡體   English   中英

嘗試從Watir-Webdriver中的自定義屬性中選擇一個單選按鈕

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

這樣嘗試是不可能的嗎? 我添加了代碼以擴展允許自定義選擇器的功能:

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

這是我的代碼嘗試從隨機數組中選擇它。 即使中間兩行沒有注釋,我仍然會出錯。

$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)**

我得到的錯誤是UnknownObjectException:無法找到元素。 引發錯誤的行是顯示的最后一行。

UPDATE

我更新的代碼收到超時錯誤:

    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

堆棧跟蹤:

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

我假設您頁面上的單選按鈕具有類似html的HTML:

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

解決方案1-數據屬性定位器

Watir已經支持通過自定義數據屬性定位元素。 無需對normalize_selector方法進行任何修補。

執行以下操作時,您正在正確定義數據屬性定位符:

@b.radio(:data_sku, $monthlydata)

但是,這一行:

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

不會做你所期望的。 Watir::Wait.until將等待,直到該塊評估為true。 @b.radio(:data_sku, $monthlydata)行將始終返回真實值,因為它正在返回Watir元素對象(尚未定位的對象)。 因此,等待永遠不會等待。 該塊需要驗證該元素是否存在:

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

不過,您可以將其縮短為:

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

或者由於您要設置單選按鈕:

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

解決方案2-Xpath

雖然我不建議在這種情況下使用xpath路由,但有可能。 之所以行:

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

失敗的$monthlydata是沒有正確插入$monthlydata 如所寫,xpath表示要找到一個具有值“ $ monthlydata”(而不是隨機選擇的值)的data-sku屬性。

您需要進行字符串插值,以便實際上解釋$monthlydata而不是文字。 同樣,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