Geb:如何从下拉列表中选择一个值?

时间:2014-09-19 20:52:45

标签: jquery forms drop-down-menu geb

我对一块Geb测试有很大的困难;如何从下拉列表中选择一个值。我尝试了四种不同的方式,但这些方法都没有。它会使测试崩溃或直接跳过它。任何帮助将不胜感激

HTML表单元素(102727是db表中加利福尼亚的ID ...)

<div class="col-sm-2">
    <label id="submitterState-label" class="toplabel" for=submitterState>State</label>
    <g:select name="submitterState" id="submitterState" from="stuff here..." class="form-control" optionKey="id" value="morestuff" noSelection="['null':'']" aria-labelledby="submitterState-label"/>
</div>

结果HTML:

<div class="col-sm-2">
        <label id="submitterState-label" class="toplabel" for=submitterState>State</label>
        <select name="submitterState" id="submitterState" class="form-control" aria-labelledby="submitterState-label" >
<option value="null"></option>
<option value="102722" >Alabama</option>
<option value="102723" >Alaska</option>
<option value="102724" >American Samoa</option>
<option value="102721" >APO Address - AA</option>
<option value="102725" >Arizona</option>
<option value="102726" >Arkansas</option>
<option value="102727" >California</option>
<option value="102728" >Colorado</option>
<option value="102729" >Connecticut</option>
<option value="102730" >Delaware</option>
<option value="102731" >District of Columbia</option>
<option value="102732" >Florida</option>
<option value="102733" >Georgia</option>
<option value="102734" >Guam</option>
<option value="102735" >Hawaii</option>
<option value="102736" >Idaho</option>
<option value="102737" >Illinois</option>
<option value="102738" >Indiana</option>
<option value="102739" >Iowa</option>
<option value="102740" >Kansas</option>
<option value="102741" >Kentucky</option>
<option value="102742" >Louisiana</option>
<option value="102743" >Maine</option>
<option value="102744" >Maryland</option>
<option value="102745" >Massachusetts</option>
<option value="102746" >Michigan</option>
<option value="102747" >Minnesota</option>
<option value="102748" >Mississippi</option>
<option value="102749" >Missouri</option>
<option value="102750" >Montana</option>
<option value="102751" >Nebraska</option>
<option value="102752" >Nevada</option>
<option value="102753" >New Hampshire</option>
<option value="102754" >New Jersey</option>
<option value="102755" >New Mexico</option>
<option value="102756" >New York</option>
<option value="102757" >North Carolina</option>
<option value="102758" >North Dakota</option>
<option value="102759" >Northern Mariana Islands</option>
<option value="102760" >Ohio</option>
<option value="102761" >Oklahoma</option>
<option value="102762" >Oregon</option>
<option value="102763" >Pennsylvania</option>
<option value="102764" >Puerto Rico</option>
<option value="102765" >Rhode Island</option>
<option value="102766" >South Carolina</option>
<option value="102767" >South Dakota</option>
<option value="102768" >Tennessee</option>
<option value="102769" >Texas</option>
<option value="102770" >U.S. Virgin Islands</option>
<option value="102771" >Utah</option>
<option value="102772" >Vermont</option>
<option value="102773" >Virginia</option>
<option value="102774" >Washington</option>
<option value="102775" >West Virginia</option>
<option value="102776" >Wisconsin</option>
<option value="102777" >Wyoming</option>
</select>

Geb测试(这些都不起作用......):

$('#submitterState').value(102727)
    $("form").submitterState = 102727 
    $('select', name: "submitterState").value(102727) 
    $('div.col-sm-2').find('select', name: "submitterState").value(102727)

3 个答案:

答案 0 :(得分:9)

我发现完全模仿用户是最好的方法。首先点击<select>,然后点击您想要的<option>

$('#submitterState').click()
$('#submitterState').find("option").find{ it.value() == "102727" }.click()

显然,这在方法调用中看起来更好,但这应该让你去。

答案 1 :(得分:0)

import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By; 

Select dropdown = new Select(driver.findElement(By.id("var_302")));
             dropdown.selectByVisibleText("Hispanic or Latino"); 

试试这个。

答案 2 :(得分:0)

您需要尝试:

$('select', name: "submitterState") = "102727"