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. The line that's throwing up the error is the last one shown.
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:
<input type="radio" data-sku="GC311Z-02" />
Solution 1 - Data Attribute Locator
Watir already supports locating elements via custom data attributes. There is no need to do any patching of the normalize_selector
method.
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. 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). 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
While I do not recommend going the xpath route in this scenario, it is possible. The reason that the line:
@b.radio(:xpath, "//input[@data_sku='$monthlydata']").set
fails is that $monthlydata
is not properly inserted. As written, the xpath says to find a data-sku attribute that has the value "$monthlydata" (not the value that was randomly selected).
You need to do a string interpolation so that $monthlydata
is actually interpreted rather than literal. As well, xpath requires @data-sku
instead of @data_sku
:
@b.radio(:xpath, "//input[@data-sku='#{$monthlydata}']").when_present.set