How to select a combobox in geb with groovy - testing

This is what I have currently with Geb. I want to be able to retrieve and set the value of a combobox (or drop down) in a page. I'm not quite sure how to do it, and the Book of Geb does not seem to cover this. This is the HTML element that I'm trying to work with.
<select style="height: 22px; width: 370px;" id="ddlContactTypes" name="ddlContactTypes">
<option value="-1">--Please Select--</option>
<option value="18">Customer Hauler</option>
<option value="19">Customer General</option>
<option value="20">Rolloff</option>
<option value="21">Supplier</option>
<option value="22">Leads</option>
<option value="23">Competition</option>
<option value="24">Employees</option>
<option value="25">Customer Car</option>
<option value="26">Secondary Metals Recycler</option>
<option value="27">Mills</option>
<option value="28">Brokerage Customers</option>
<option value="29">Type Jon</option>
</select>
This is what I've go so far. This currently will click the element, but I don't know how to actually select one. Any ideas?
static content = {
title {$("title")}
contactType { $("#ddlContactTypes") }
}
def addUser(){
contactType.click()
}

You can select an option by setting the value of the geb object :
contactType.value('20')
Above example will set the value to '20', making the list show 'Rolloff' as selected.

U can see this
HTML
Alt folk
Chiptunes
Electroclash
G-Funk
Hair metal
GOOVY
$("form").artist = "1" // first option selected by its value attribute
$("form").artist = 2 // second option selected by its value attribute
$("form").artist = "Ima Robot" // first option selected by its text
http://www.gebish.org/manual/0.9.2/navigator.html

Manual descibes it in section Setting Values

Related

Selenium webdriver select option

I'm trying to select an option from a select drop-down using Selenium/java/MS Edge driver, but I'm finding I can't select the option even though I can see I have located it.
My code so far;
final static String URL = "http://usedcars.bmw.co.uk";
final static String edgeDriverBinary = "MicrosoftWebDriver.exe";
System.setProperty("webdriver.edge.driver", edgeDriverBinary);
WebDriver driver = new EdgeDriver();
driver.get(URL);
// Click the field to activate the drop down
By byQsRangeBtn = By.xpath("//div[#id='quick-search']/form/fieldset[#class='form']/div/div[#class='js-ctm-dropdownlist']/div[#class='js-ctm-display']/span");
WebElement qsRangeElement = driver.findElement(byQsRangeBtn);
qsRangeElement.click();
Sending this click does activate the drop-down and I can see it expands
Select qsRange = new Select(driver.findElement(By.id("qsRange")));
log("OPTIONS: " + qsRange.getOptions());
In the above I can dump .getOptions() and I see all the data from the select options.
When I call this I just get an exception with no stack trace or useful logging information. I've tried .selectByIndex() and .selectByVisibleText() with the same result - an exception and no information.
qsRange.selectByValue("3648835");
The part of the page is below. The id="qsRange" is set as not visible in the page (grey'd out in firebug). class="js-ctm-display" is the only div that is visible.
If I manually click the drop down to display it's values then <div class="js-ctm-dropdownlist"> becomes <div class="js-ctm-dropdownlist js-open">, but firebug still sees the select as not displayed.
<form class="quicksearch mp-search-count" method="post" action="/search-cars.aspx">
<fieldset class="form">
<div class="search-control series js-ctm js-ctm-mode-full" data-theme-type="dropdownlist">
<div class="js-ctm-dropdownlist">
<select id="qsRange" class="mp-control-data" name="Range">
<option value="-1">Series</option>
<option value="890">1 Series</option>
<option value="3648873">2 Series</option>
<option value="66">3 Series</option>
<option value="3648835">4 Series</option>
<option value="67">5 Series</option>
<option value="68">6 Series</option>
<option value="69">7 Series</option>
<option value="3648830">X</option>
<option value="3648831">Z</option>
<option value="3648832">M</option>
<option value="3648938">PHEV</option>
<option value="3648833">Hybrid</option>
<option value="3648834">BMW i</option>
<option value="3648879">Alpina</option>
</select>
<div class="js-ctm-display">
<span>Series</span>
<span class="js-ctm-ico"></span>
</div>
If I manually select a value from the drop-down then I see class="js-ctm-display" becomes as below and the page then makes an ajax call to obtain values for subsequent drop-downs.
<div class="js-ctm-display">
<span>4 Series</span>
<span class="js-ctm-ico"></span>
</div>
Because I can tell that the page is making multiple ajax calls as I move through the options I believe I need to simulate user interaction (clicks) rather than being able to just set values. However my attempts at clicking on an option in the list aren't working out at all.
I've also attempted to get a WebElement for one of the option values with the intention of sending it a click, but that throws an exception saying the WebElement is expected to be a Select rather than an Option.
Does anyone have a view on how to approach this kind of page? I'm rather new to Selenium so expect my approach isn't the best.

How to select the Dropdown option based on class selection using webdriver

When the user expands the menu and chooses and option, how can I get the selected option with Selenium?
<select class="brand-select select-glossy user-success" name="">
<option value="All Makes">All Makes</option>
<option value="Ford">Ford</option>
<option value="Lincoln">Lincoln</option>
<option value="Mercury">Mercury</option>
</select>
Find the select element and make a Select object our of it (need to import org.openqa.selenium.support.ui.Select)
Use the API therein to get the first selected option:
WebElement selectedOption = new Select(driver.findElement(By.className("brand-select"))).getFirstSelectedOption();

PrestaShop: Get value of select menu

Is there any possible way to get a selected value in Prestashop without javascript? I tryed with Tools::getValue() but it doesn't work...
<select name="selectPosition" id="selectPosition">
<option value="0">---</option>
<option value="1">'.$this->l('Left').'</option>
<option value="2">'.$this->l('Right').'</option>
</select>
Best regards,
George!
Tools::getValue checks is value is in POST or in GET, just pass the value you want to check ad param
if (Tools::getValue('selectPosition') {
//make something
}

Using .change() to addClass with select list

I'm trying to make a hidden field show by adding a class when the "other" option is selected from a pull-down list. But I'm not quite sure the correct way to do it.
I have the input hidden and when option is chosen I want to add the class "view" which adds display block making the hidden field visible.
Here is a fiddle showing what I have so far, any help would be much appreciated: http://jsfiddle.net/maikunari/NX795/
$(document).ready(function(){
$("#select-box").change(function(){
if($(this).val() == "other"){
$("#text-field").show();
} else {
$("#text-field").hide();
}
});
});
<select id="select-box">
<option value="Email Newsletter">Email Newsletter</option>
<option value="Yellow Pages ">Yellow Pages </option>
<option id="other-select" value="other">Other</option>
</select>

Set the value of a drop down box on load

If you look at the source code for the html you can see the combobox has an id. However when i run a loop through all hte elements on the page in vb.net it comes up as a blank instead of the id thats clearly defined. I'm thinking it has something to do with the javascript giving it a dynamic id. Anyway. I need to set this box after the page has loaded in a webbrowser object. Please help. Its driving me crazy!
<input type="hidden" name="wlw-select_key:{actionForm.dobMonth}OldValue" value="true">"
<select name="wlw-select_key:{actionForm.dobMonth}" id="dobMonth" class="dobMonth"><option" value="">Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option></select>
<script language="JavaScript" type="text/JavaScript">
<!--
netui_tagIdNameMap.dobMonth="wlw-select_key:{actionForm.dobMonth}"
-->
</script>
*My Source Code Attemps ***
WebBrowser1.Document.GetElementById("dobMonth").SetAttribute("selectedtext", "March") - dosent work
Dim el As HtmlElement = WebBrowser1.Document.All("wlw-select_key:{actionForm.dobMonth}")
Dim elCol As HtmlElementCollection = el.GetElementsByTagName("option")
Dim X As Integer = 0
For Each op As HtmlElement In elCol
If op.InnerText = "March?" Then
el.SetAttribute("selectedIndex", X.ToString())
Exit For
End If
X += 1
Next
that didnt work either
So i'm really out of options. Any ideas. I did some research on the javascript reference but didnt turn out much useful. Thanks in advance!
as I remember, the name and id attributes are limited in the characters they can contain. curly-braces are probably not allowed. not sure if that's your problem or not, but it's the first thing that popped into mind... I'll look into it some more.
[much later...]
OK, this works, but I hacked out a bunch of stuff, and it may not be quite what you're looking for:
<select name="dobMonth" id="dobMonth" class="dobMonth"><option value="">Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<script type="text/javascript">
document.getElementById("dobMonth").selectedIndex = 3;
</script>
You had a stray double-quote in your first option tag, which may have been part of the problem. But fixing that didn't allow the vbscript to work in my browser. I haven't done VBscript since the mid-90s so I switched to javascript. In any case it ought to give you a basis for further development, I hope!
[later]
This code will set the option by text string. I tested under Firefox, it ought to work in IE also:
<script type="text/javascript">
function setSelected(month) {
var select = document.getElementById("dobMonth");
var options = select.getElementsByTagName("option");
for (var i = 0; i < options.length; i++) {
if (options[i].childNodes[0].nodeValue == month) {
select.selectedIndex = i;
break;
}
}
}
setSelected("March");
</script>