Unable to select option from dropdown using nightwatch js - ui-automation

I have seen some other posts using below format but it did not work at all, even the select is not opening:
.click(".selectpicker option[value='somevalue']")
But when I wrote like this:
.click("select[id='chooseone']")
it did open the dropdown.
This is how select looks like:
<select class="selectpicker btn dropdown-toggle btn-default" title="Choose one of the following..." id="chooseone" style="">
<option value="chooseone" style="">Choose one</option>
<option value="value1" style="">option 1</option>
<option value="value2" style="">option 2</option>
<option value="value3">option 3</option>
</select>
There is react code in backend, so an onchange event is fired which will display appropriate input field per option and a submit button.
My test is basically:
select an option
fill fields
submit
validate result container
How should I write this code? This is my first time in such thing. Any help is appreciated.
Thanks
UPDATE >>>
This was in Safari and it did not work. But when I installed chromedriver, it did work. Here is the working code:
.click('select[id="searchBySelect"] option[value="any_option_value"]')
This will click the provided option in the select element.

First you need to open the dropdown menu. It looks like you already know this but in any case you would start with:
browser.click('.selectpicker');
Then there are a couple of ways you can get the option you want to click. You can choose a specific option by doing something like this (just modify it to select whichever option you want)
browser.element('css selector', '.selectpicker', function(element) {
browser.elementIdElement(element.value.ELEMENT, 'css selector', 'option[value="value1"]', function(option) {
browser.elementIdClick(option.ELEMENT);
});
});
Or you could get all of the options in an array and click on one of them this way:
browser.elements('css selector', 'option', function(elements) {
browser.elementIdClick(elements.value[0].ELEMENT); //can use any index here as long as you know which one you are looking for
});

tehbeardedone thanks so much for providing your solution. I had to do a small fix to get the elementIdClick() working.
Instead of option.ELEMENT
I had to use option.value.ELEMENT
Since my code snippet had to be added to a page object, this is its final version:
let durationOptionSelector = `option[contains(text(), "${duration}")]`; //dynamic option selector
this.click("#duration_dropdown");
this.api.element("xpath", "#duration_dropdown", function (dropdown) {
this.elementIdElement(dropdown.value.ELEMENT, "xpath", durationOptionSelector, function(option) {
this.elementIdClick(option.value.ELEMENT);
});
});

Related

Semantic UI bypasses browser form messages

I have a dropdown that is required for my form. Without Semantic UI everything works as expected. If the user doesn't select anything he gets a message "You must select an optino" or similar from the browser.
<select required>
<option value="" selected="">Please select</option>
<option value="True">Yes</option>
<option value="False">No</option>
</select>
As soon as I style my dropdown with Semantic UI this functionality disappears.
<select class="ui search dropdown" required>
<option value="" selected="">Please select</option>
<option value="True">Yes</option>
<option value="False">No</option>
</select>
This seems to be because Semantic UI hides the original select and adds some bastardized select based on divs. However due to this some of the functionality described above is vanished.
Is there a workaround on this? I want to keep my forms working even with javascript disabled.
Here's a jsfiddle.
As Andrew Pointed out on the issue, this is what you can do:
Hi #Pithikos, you’re correct about the cause of it not working. You could additionally use the validation component that would be falling back to browser validation for clients with disabled JavaScript.
The issue seems to be that Semantic UI converts the select tag into some other HTML including an input tag. The problem is that it doesn't respect the required attribute so the new input tag ends up not having it.
Here's a workaround (after $('.ui.dropdown').dropdown();):
$('.ui.dropdown').each(function(){
$select = $(this).find('select');
$input = $(this).find('input');
if ($select.attr('required') && $input) {
$input.attr('required', 'true');
// Remove required attribute when user or browser focuses on
// the input element and thus getting the selection menu. After
// this event we assume there is a guarantee that the input has
// a value.
$('.ui.dropdown input').on('focus', function(e){
$(this).removeAttr('required');
});
}
});
The workaround works by adding the necessary required attribute. Once that input is focused we assume that a value has been selected and remove the required attribute.
Here's the bug reported on github.

Angular 2: How to get the selected value from different options of a form?

I would like to use a <select> in a form to let the user being able to update values among different <option>. I have used the technique from the guide here: https://angular.io/docs/ts/latest/guide/forms.html. Here is the sample I am talking about:
<div class="form-group">
<label for="type">Type :</label>
<select class="form-control" [(ngModel)]="order.type" ngControl="type">
<option *ngFor="#type of types" [value]="type">{{type}}</option>
</select>
</div>
In my order-details.component I have got an updateOrder() which calls the updateOrder() from myApp.services.
My problem is when I am trying to send the data from the form to the back-end: all the parts with an <input> are OK, but not those with <select> (it returns the original values, and not the one selected).
Does anyone have encountered this or a similar problem?
Thanks for your help!
There is a way to get the value from different options.
check this plunker
component.html
<select class="form-control" #t (change)="callType(t.value)">
<option *ngFor="#type of types" [value]="type">{{type}}</option>
</select>
component.ts
this.types = [ 'type1', 'type2', 'type3' ];
this.order = {
type: 'type1'
};
callType(value){
console.log(value);
this.order.type=value;
}
Been tackling this problem for a few hours.
Checked in the (incomplete) documentation to find an item in the NgSelectOption page called "ngValue"
Not sure if this is the intended use but it seemed to work fine.
So instead of using
[value]="item"
Use:
[ngValue]="item"
Just use ngModel on the select and ngModelChange event if you want to do something when it changes.
In fact I can't reproduce your problem. I created a plunkr with a very simple form with an input and a select. When I submit the form, I have actual values in the bound object. See this plunkr: https://plnkr.co/edit/5C3agW7QZfcrdt88QzSh?p=preview.
Feel free to tell me if I didn't correctly understand your problem.
Thierry
If you have static, hard-coded values for the select tag like below:
<select #quantity>
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
</select>
You can do the following:
#ViewChild('quantity') quantity: ElementRef;
console.log(this.quantity.nativeElement.value); // will print value of the currently selected option

How to have Chosen plugin retain selection option following page reload

I'm using a series of select elements on a form within an MVC5 view, initialised with the jQuery Chosen plugin. The plugin looks as it should and saves the selected option back to the model successfully. However if any other element on the page fails validation and the page is reloaded, the select elements are back to their original state and the selected option is no longer shown. How can I get the select list to retain its selected option following a reload?
<select class="chosen" id="SE_Visit1" name="#Html.NameFor(model => model.SE_Visit1)">
<option value=""></option>
<option value="0">0 (25)</option>
<option value="1">1 (23)</option>
<option value="2">2 (37)</option>
<option value="3">3 (34)</option>
<option value="4">4 (12)</option>
</select>
$(document).ready(function () {
// initialise the Chosen plugin on the select lists
$('.chosen').chosen({ placeholder_text_single: "Select visit...", width: "75px" });
});
I've tried to add a change event handler on the element to save the selected value which could be used to assign the value again after reload but this just seems to break the page. Any help much appreciated since this particular form has 20 select elements so a lot of values for the user to have to complete more than once.

Jeditable show always select box

i'm using the Jeditable plugin to send request to the server by changing the select box. my problem is, that i want to show the select box at any time, not only if the users (double)clicks the text. i did not found any option on the plugin site, so i ask you if there is another solution (for the plugin).
$('.col-type').editable( 'set-type.php', {
data: function(value, settings) {
return " {'1':'1','2':'2','3':'3', 'selected':'2'}";
},
onblur : "submit",
type : "select"
} );
Idea on inplace editing is to show the form only when you click element. If you always want to show the form use plain old html.
<select>
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
</select>

how to use jquery live() to add attribute to element

I have a select element that is in the DOM from page load. Lets say it has an id of my_select like so:
<select name="my_select" id="my_select">
</select>
At some point though when the user does something some values are pulled in via ajax to populate this select element so that it now contains options. So it will now look like this:
<select name="my_select" id="my_select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
So how do I then add an attribute to one of these options?
I want to do this:
$('#my_select option[value="2"]').attr("selected", "selected");
But I think I need to use live() because the options were added via ajax after the DOM had been created.
Thanks
$("body").delegate('#my_select", 'YOUREVENT', function(){
$("#my_select option[value='2']").attr("selected", "selected");
});
Just setting the selected option can be done like this: $('#my_select').val(2); Are you looking for more?
$.live is the same as $.bind, for event binding, the diference is "live" works for elements than not exists yet, and will be created by DOM scripting (i.e. ajax response).
the right form to set val = 2 is $('#my_select').val(2), when it's already in the dom ;)