I am looking for a way to check what radio is selected and save the value of that option into a variable.
once i have the variable i want to build a switch statement which will give me the logic to decide what to do.
here is the HTML:
<input type="radio" value="option_1" name="registration[registrationType]" id="option_1_id" class="radio">
(Option 1)
<input type="radio" value="option_2" name="registration[registrationType]" id="option_2_id" class="radio">
(Option 2)
<input type="radio" value="option_3" name="registration[registrationType]" id="option_3_id" class="radio" checked="checked">
(Option 3)
option 3 is the checked option (checked="checked")
I want to extract the value of option 3 (option_3) and save it in a variable
default_option = #the code that extracts the default option
case default_option
when "option_1"
puts 'lets do something with this'
when "option_2"
puts 'lets do something with this'
when "option_3"
puts 'lets do something with this'
else
puts 'cant do much with this'
end
Thanks in advance!
Since you want the value of the checked radio in a group of them you want to use the shared name and the checked filter
find(:radio_button, 'registration[registrationType]', checked: true).value
In the case the 'option_3_id` is checked, you can find its value by
find(:radio_button, 'option_3_id', checked: true).value
But as you don't know which radio button is checked, you should first find all the options and verify which one is currently checked (checked:true/false), and then retrieve its value.
Related
I need to click this input only if it has "aria-checked ="true"
<input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" id="mat-checkbox-131-input" tabindex="0" aria-checked="true" style="" xpath="1">
Ruby:
aria_checked = true
if aria_checked = true
impressora_etiqueta = "//mat-checkbox[#id='mat-checkbox-23']/label/div"
page.find(:xpath, impressora_etiqueta).click
end
There are many ways to do what you want - simplest is probably
page.first('#mat-checkbox-23[aria-checked="true"]', minimum: 0)&.click
which will look for the first element with the given id and aria-checked = "true" and click it if one exists.
Note: the id in your test and sample HTML didn't match so I went the id from your test, adjust as needed. Also you have a class of cdk-visually-hidden shown -- If that's actually making the element not visible on the page, then this won't work and you'll need to add more surrounding HTML to your question with a better description of exactly what you're trying to do (you can't click on non-visible elements)
I have written code for select list this way b.select_list(:id,'something').select 'something' but I don't how know how to write a code for given below select list, Can anyone help me to write the code to select from this select_list
<md-select tabindex="0" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" role="listbox" aria-disabled="false" aria-expanded="false" aria-invalid="true" aria-multiselectable="false" aria-required="true" aria-owns="select_container_5" aria-label="Gender" ng-model="user.gender" name="gender" required=""><md-select-value class="_md-select-value _md-select-placeholder" id="select_value_label_0"><span>Gender</span><span class="_md-select-icon" aria-hidden="true"></span></md-select-value><div class="_md-select-menu-container" id="select_container_5" aria-hidden="true"><md-select-menu class="ng-scope"><md-content>
<md-option tabindex="0" class="md-ink-ripple" id="select_option_3" role="option" aria-selected="false" value="Male"><div class="_md-text">Male</div></md-option>
<md-option tabindex="0" class="md-ink-ripple" id="select_option_4" role="option" aria-selected="false" value="Female"><div class="_md-text">Female</div></md-option>
</md-content></md-select-menu></div></md-select>
Based on the element tag, md-select, it looks like this select list is created by Angular Material. It is not a select element, which means you cannot use the usual select_list method. Instead, you need to manually interact with it. This involves:
Clicking the select list to open the list of options
Clicking one of the options
The following code will select an option by value. You could use a different locator as needed:
# Open the dropdown
browser.element(tag_name: 'md-select', name: 'gender').click
# Select an option (by value)
browser.element(tag_name: 'md-option', value: 'Female').when_present.click
A couple of notes:
As md-select is not a standard element, you need to locate it using the generic element method.
Make sure to use when_present (or another wait method) to ensure the options are displayed before selecting.
How do I interact with a file_field thats hidden by its parent?
<span class="btn button-large fileinput-button">
Select files...
<input accept="image/png, image/gif, image/jpeg" id="gallery_files" multiple="multiple" name="gallery_files" type="file">
</span>
The button overlays the input, therefore it's not visible.
Edit
For the record, here's my code:
data[:photos].each do |photo|
$browser.file_field.set photo
end
and the error: Watir::Wait::TimeoutError: timed out after 20 seconds, waiting for {:tag_name=>"input", :type=>"file"} to become present
Workable example in a Gist
I was a bit suprised, but I was able to set the file field in the sample HTML without any issue using:
browser.file_field.set('path/to/file.txt')
From the code, it looks like setting the file field only requires the input to exist. It does not require it to be visible (or present).
Given that you are getting a Watir::Wait::TimeoutError exception, I would guess that your code is actually failing before the file_field.set. As it looks like the page has the input in a dialog, I am guessing your code actually looks more like:
$browser.file_field.wait_until_present
data[:photos].each do |photo|
$browser.file_field.set photo
end
It would be the wait_until_present method that is actually throwing the exception.
Solution 1
Assuming that an explicit wait method is being called for the file field, you should be able to just remove the wait.
If you have the wait because the dialog is being loaded by Ajax, you could try waiting for a different element instead - eg the parent span.
Solution 2
If for some reason you need the file field to be present, you will need to change its CSS. In this case, the opacity:
p $browser.file_field.present?
#=> false
$browser.execute_script('arguments[0].style.opacity = "1.0";', browser.file_field)
p $browser.file_field.present?
#=> true
For my situation, this worked:
$browser.execute_script("jQuery(function($) {
$('.fileinput-button').css('visibility', 'hidden')
$('#gallery_files').css('visibility', 'visible').css('opacity', '1').css('width', '100').css('height', '50')
})")
I had to hide the parent span, then show, resize, and change the opacity of the input
I would like to select a checkbox from an excel spreadsheet for a data driven test.
code for the checkbox:
Not Checked:
<input type="checkbox" class="greenopt" name="Accounts[0][greenopt]" id="" value="0">
Checked:
<input type="checkbox" class="greenopt" name="Accounts[0][greenopt]" id="" value="1">
I tried the following but get the error: Unable to locate element, using {:value=>"1"}
greenopt_check=worksheet.cells(rows,"O").value
browser.checkbox(:value => greenopt_check).set
this is kind of similar to my previous question regarding radio buttons
Select Radio buttons from excel with ruby
Please review the watir checkbox documentation. in that case you can checkbox.set or checkbox.clear depending on your goals.
You want to use something that is unique, and consistent/predictable to select elements
checkbox_class = worksheet.cells(rows,"A").value
puts 'retrieved checkbox_class from spreadsheet is:' + checkbox_class #for debugging, make sure you got the correct thing from the spreadsheet
browser.checkbox(:class => checkbox_class).set
or
checkbox_name = worksheet.cells(rows,"A").value
puts 'retrieved checkbox_name from spreadsheet is:' + checkbox_name #for debugging, make sure you got the correct thing from the spreadsheet
browser.checkbox(:name => checkbox_name).set
I want to restrict entry of input onto a field of type number such that input cannot be outside range of min-max specified in the html.
input type = "number" min = "1" max = "5"
Is there a way of outputting the number field without the text box and i would rather not use
"input type = range"
as slider does not show value currently selected
Please help.
Thanks.
Based on what you said, I suggest using a simple input text field and check it's value validity on submission via JavaScript (as #Kush mentions above). You could also check it as the user types, or moves focus away from that field.
<form>
Only 1 to 100 <input type="text" name="number" pattern="\d{1,2}(?!\d)|100" title="one to hundreed only">
<input type="submit">
</form>