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