Java Selenium选择作为字符串传递的变量的下拉选项
背景:Java Selenium选择作为字符串传递的变量的下拉选项,java,drop-down-menu,selenium,Java,Drop Down Menu,Selenium,背景: 卡片库存-包含计划、库存位置和库存国家/地区的详细信息 客户销售卡屏幕-当您向客户销售新卡时,您必须输入他们的地址。一旦您输入他们的国家,特定字段(州/地址行2/邮政编码)要么成为强制性的,要么成为自愿的 问题是,正在使用的两个国家的数据库不一样,可能有所不同。卡片库存中显示“德国”,销售卡片屏幕上显示“德国,联邦共和国” 我的流程: 1) 搜索发行前卡以从卡库存中获取国家/地区-将此变量分配到字符串,即aString 2) 卖掉那张卡 3) 在国家/地区下拉框中-如果aString
- 卡片库存-包含计划、库存位置和库存国家/地区的详细信息
- 客户销售卡屏幕-当您向客户销售新卡时,您必须输入他们的地址。一旦您输入他们的国家,特定字段(州/地址行2/邮政编码)要么成为强制性的,要么成为自愿的
aString
2) 卖掉那张卡
3) 在国家/地区下拉框中-如果aString
在该列表中,选择aString
,如果没有,则创建一个“else thens”列表以捕捉变化
我的代码一直告诉我字符串不在列表中,countrydropdown
被打印为false
,即使我在两个国家都匹配的情况下测试它
任何帮助都将不胜感激
Boolean countrydropdown = "xpath=//select[@id='address.country']/option]".indexOf(aString) > 0;
System.out.println("countrydropdown");
System.out.println(countrydropdown);
<tr>
<td class="labelFormReq">*</td>
<td class="labelForm">Country:</td>
<td>
<select id="address.country" onchange="validateAndSubmit(this, 'selectCountryEvent');" name="address.country">
<option value="">Please Select</option>
<option value="4">Afghanistan</option>
<option value="248">Alan Islands </option>
<option value="8">Albania</option>
<option value="12">Algeria</option>
<option value="16">American Samoa</option>
<option value="20">Andorra</option>
<option value="24">Angola</option>
<option value="660">Anguilla</option>
<option value="10">Antarctica</option>
<option value="28">Antigua and Barbuda</option>
<option value="32">Argentina</option>
<option value="51">Armenia</option>
<option value="533">Aruba</option>
<option value="36">Australia</option>
</select>
</td>
</tr>
Boolean countrydropdown=“xpath=//选择[@id='address.country']/option]”。indexOf(aString)>0;
System.out.println(“countrydropdown”);
System.out.println(countrydropdown);
*
国家:
请选择
阿富汗
阿兰群岛
阿尔巴尼亚
阿尔及利亚
美属萨摩亚
安道尔
安哥拉
安圭拉
南极洲
安提瓜和巴布达
阿根廷
亚美尼亚
阿鲁巴
澳大利亚
不会真正搜索元素。实际上,它在文本“xpath=//select[@id='address.country']/option]”
中查找aString
。为了使它返回任何有用的内容,必须使用方法调用对其进行包装。见此:
Boolean countrydropdown = selenium.isElementPresent("xpath=//select[@id='address.country']/option[text()='" + aString + "']");
要使其更具可读性和惯例意识,请执行以下操作:
boolean countryDropdown = selenium.isElementPresent("xpath=id('address.country')/option[text()='" + aString + "']");
当且仅当address.country
元素的
子元素存在且文本等于您的aString
时,它返回true
或者,如果您希望它不那么单一:
boolean countryDropdown = false;
String[] countryOptions = selenium.getSelectOptions("id=address.country");
for (String option : countryOptions) {
if (option.equals(aString)) {
countryDropdown = true;
break;
}
}
你知道写
“xpath=//select[@id='address.country']/option]”“
不会神奇地选择任何东西,你需要用方法调用来覆盖它,对吗?不,我没有,当我在xpather中键入//select[@id='address.country']/option时,它会显示国家的列表,所以我应该能够索引那些不应该的国家,我也,您是否绝对确定要使用Selenium RC而不是Selenium WebDriver?Selenium RC在一年前就被弃用了,现在还没有进行任何开发(与WebDriver不同),因为它受到了一些严重的技术限制…感谢slanec,我会在访问系统后尽快尝试。我尝试过使用web driver,但在我正在测试的系统上它无法正常工作。测试需要在ie中完成,web驱动程序一直在选择下一个无线电选项卡,在ff中它是完全好的。也许我会在rc中完成测试,然后切换到web驱动程序。我花了几天的时间试着让它工作!
boolean countryDropdown = false;
String[] countryOptions = selenium.getSelectOptions("id=address.country");
for (String option : countryOptions) {
if (option.equals(aString)) {
countryDropdown = true;
break;
}
}