[英]Geb test: cannot select value of dropdown based on Ajax call
我在Geb測試中遇到了一個問題,我試圖點擊選擇下拉框,其中第一個選項為null(用戶希望它顯示空白作為第一個選項)。 它是如下的狀態字段。 請注意,原始html的from值有一個空數組。 基於基於Ajax調用將“美國”選為國家來填充該州,我不得不對其進行一些編輯。
如何設置選擇器使其達到第二或第三值? 我可以將值作為數組或類似形式傳遞給選擇器嗎? Firebug HTML和我的測試數據庫xml中,“ 100225”的值為美國,“阿拉巴馬州”的值為“ 102722”
***我想要達到的目的是:我希望能夠擊中唯一狀態值(例如102722),但是Geb僅擊中選擇框中始終為空的第一個值,盡管'102722值在Firebug HTML和我的測試xml數據庫中都存在,所以我知道在那兒。 如您所見,我以幾種不同的方式為State編寫了Geb選擇器。 這是總是會回來的錯誤:
| java.lang.IllegalArgumentException:無法選擇帶有文本或值的選項:102722
注意:下面還要注意,Geb中的值的println始終返回空值。
原始表單項:
<div class="col-sm-3">
<label id="Country-label" class="toplabel" for="Country">Country<span class="required-indicator">*</span></label>
<g:select name="Country" id="Country" from="${dbinfo...Country.list().sort{it.orderNumber}}" class="form-control" required="" optionKey="id" value="${Instance.Country}" aria-labelledby="Country-label"/>
</div>
<div class="col-sm-2">
<label id="State-label" class="toplabel" for=State>State<span id="StateAsterisk" class="required-indicator">*</span></label>
<g:select name="State" id="State" from="${[]}" class="form-control" optionKey="id" value="${...Instance.State}" noSelection="['null':'']" aria-labelledby="State-label"/>
</div>
從Firebug渲染的HTML:
<select id="Country" class="form-control" aria-labelledby="Country-label" required="" name="Country">
<option selected="selected" value="100225">United States</option>
<select aria-labelledby="State-label" class="form-control" id="State" name="State">
<option value="null"></option>
<option value="102722">Alabama</option>
<option value="102723">Alaska</option>
測試中的Geb選擇器摘錄:
println $('#Country').value()
waitFor(5){$('#Country').click()}
$('#Country').value(100225) // USA
waitFor(10){$('#Country').find("option").find{ it.value() == 100225 }.click()} // USA
println $('#State').value() // --> RETURNS NULL VALUE
$('#State').find("option").find{ it.value() == 102722}.click() // --> NONE OF THESE WILL HIT
waitFor(5){$('#State').click()} // --> NONE OF THESE WILL HIT
waitFor(5){$('#State').value(102722)} // --> NONE OF THESE WILL HIT
Javascript / Ajax調用-根據美國國家/地區加載狀態:
$(document).ready(function(){
$('#Country').change(function() {
var country = $(this).val();
if(country != null && country != 'null'){
loadStates(country, null, 'submitter', true);
}
});
<g:if test="${...Country}">
$('#Country').val(${...Country});
loadStates(${...Country}, "${...State?:null}", 'submitter', true);
</g:if>
var xhr = null;
function loadStates(country, state, selectId, hideShowAsterisk){
if(xhr != null){
xhr.abort();
xhr = null;
}
xhr = $.ajax({
url: '${request.contextPath}.../aj/loadStatesByCountry',
data: { country: country, selectId:selectId },
async:false,
success: function(data) {
$('#'+selectId+'State').html(data);
$('#'+selectId+'State-label').show();
if(state){
$('#'+selectId+'State').val(state);
}
if(hideShowAsterisk){
if(country == ${grailsApplication....country.usa}){
$('#'+selectId+'StateAsterisk').show();
}else{
$('#'+selectId+'StateAsterisk').hide();
}
}
xhr = null;
},
error:function(jqXHR, textStatus){
if(textStatus != 'abort'){
xhr = null;
alert("There was an error requesting the States for the selected Country");
}
}
});
}
您是否嘗試過以下方法:
waitFor(30){$("form").State = "102722"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.