Robot Framework : Random Selection of Drop downs - random

I need some help, I want to create a test that will randomly get the value during test case since in my case I declared it in my code, how can I make this randomly?
Heres my code
html
<select name="gender">
<option>Please Select</option>
<option value='Male'>Male</option>
<option value='Female'>Female</option>
</select>
robot
*** Test Cases ***
Select From List By Value xpath://select[#name='gender'] Male

You can use the 'Random Element' from the Faker Library. Random Element returns a random item from the list of items provided.
*** Settings ***
Library FakerLibrary
*** Test Cases ***
Randomise gender selection
${gender} Select random gender
Select from list by value xpath://select[#name='gender'] ${gender}
*** Keywords ***
Select random gender
${randomValue} Random Element ['Male', 'Female']
[Return] ${randomValue}
This will randomise gender selection in the test case. In case you want to add more items to your list, you can add it in the list argument following the Random Element keyword.
Note: Make sure to install and import the FakerLibrary

Related

I need to click this input only if it has "aria-checked ="true"

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)

Can an input of type select be done with an option that does not go sweep a list?

an option can be just an object instead of a list of objects?
in the documentation each sweeps a list but I would like to retrieve a single object
<select th:field="*{type}">
<option th:each="type : ${allTypes}"
th:value="${type}"
th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
</select>
is possible do somethint like
<option th:each="${Types}"
The Standard Dialect in thymeleaf offers Using th:each. Its the Iteration basics.
“for each element in the result of evaluating ${allTypes}, repeat this fragment of template setting that element into a variable called type”.
Dropdown Selector usually is for a list of options to be displayed.
You simply cant do th:each="${Types}", as you need to declare a variable to access the iterated variable
https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-theach

xpath for selecting multiple elements from drop down

I am trying to extract some information from an html page. Consider the drop down select list below:
<select name="ctl00$MainContent$ddlColor" onchange="chageColor(this);setTimeout('__doPostBack(\'ctl00$MainContent$ddlColor\',\'\')', 0)" id="ctl00_MainContent_ddlColor" class="input" style="width:175px;">
<option selected="selected" value="">Color</option>
<option value="00114743-03|large|0|03">CHARCOAL</option>
<option value="00114743-04|large|2|04">BLACK</option>
</select>
It has 3 values, "Color", "CHARCOAL" and "BLACK".
Now if I view the source and copy XPATH of "CHARCOAL" using google chrome, I get
//*[#id="ctl00_MainContent_ddlColor"]/option[2]
However, I want to extract the information as "CHARCOAL" and "BLACK". I want this to be applied on multiple pages where the drop down list might contain more or less number of elements. However, I always want to skip the first element, which will be "Color". How to do this ?
Here you are ..
//select/option[not(contains(., 'Color'))]/text()
You can skip the first option by it's position by this
//select/option[position() > 1]/text()
I hope this could help

Angular dropdown/select required always says it is valid

So I have a dropdown and I am using angular to build it. Minutes is an array of numbers [0,1,2....59]. The filter is a simple filter that displays the digits from 0-9 as 00, 01... etc.
<select ng-model="addObj.StartMinute"
ng-options="m as m | pad2Digit for m in Minutes"
required
name="startMinute">
<option value="">-- select --</option>
</select>
My problem is that this ALWAYS reports being valid. I have removed the option in there that lets me customize the option used when no match is found, and that doesn't change it. I have tried setting StartMinute to null, -1 and undefined and still the select ALWAYS says it is valid.
I have found so far that the problem has to do with my using simple numbers rather than binding to objects. In cases where I am doing a dropdown with more a collection of objects, required validation is correctly detecting that nothing is chosen. I would have thought that setting the initial value on the above dropdown to null would work, but it isn't. So does anyone know how to use required validation on a dropdown that is bound to an array of numbers?
There must be something else on the project where it wasn't working making this not work because the raw jsfiddle I threw together to try to demo the problem works properly. If the initial value is null, then the validation does fail like I would expect.
HTML
<div ng-app="app">
<div ng-controller="ctrlDropdown">
<form name="testForm">
<select ng-model="number1"
ng-options="num for num in numbers"
required
name="ddl1">
<option value="">- select - </option>
</select>
<br/>failsValidation: {{testForm.ddl1.$error.required}}
</form>
</div>
</div>
JS
var app = angular.module("app",[]);
app.controller("ctrlDropdown",function($scope){
$scope.test = "wee";
$scope.number1 = null;
$scope.numbers=[1,2,3,4,5,6];
});
http://jsfiddle.net/NBhTT/

Capybara writing drop down's options texts into an array

I'd like to put a dropdown list's options into an array generically in capybara. After the process I'm expecting to have an arrray of strings, containing all dropdown options. I've tried the code below but the length of my array stays 1 regardless of what the option count is.
periods = Array.new()
periods = all('#MainContent_dd')
print periods.length
The problem is that all('#MainContent_dd') returns all elements that have the id MainContent_dd. Assuming this is your dropdown and ids are unique, it is expected that the periods.length is 1 (ie periods is the select list).
What you want to do is get the option elements instead of the select element.
Assuming your html is:
<select id="MainContent_dd">
<option>Option A</option>
<option>Option B</option>
<option>Option C</option>
</select>
Then you can do:
periods = find('#MainContent_dd').all('option').collect(&:text)
p periods.length
#=> 3
p periods
#=> ["Option A", "Option B", "Option C"]
What this does is:
find('#MainContent_dd') - Finds the select list that you want to get the options from
all('option') - Gets all option elements within the select list
collect(&:text) - Collects the text of each option and returns it as an array
#JustinCo's answer has a problem if used driver isn't fast: Capybara will make a query to driver for every invocation of text. So if select contains 200 elements, Capybara will make 201 query to browser instead of 1 which may be slow.
I suggest you to do it using one query with Javascript:
periods = page.execute_script("options = document.querySelectorAll('#MainContent_dd > option'); texts=[]; for (i=0; i<options.length; i++) texts.push(options[i].textContent); return texts")
or (shorter variant with jQuery):
periods = page.evaluate_script("$('#MainContent_dd').map(function() { return $(this).text() }).get()")

Resources