使用 CodeCeption 测试脚本从 jQuery Selected 下拉列表中选择一个值



您好,我正在尝试使用codeception脚本从jQuery Chosed Dropdown中选择一个值,以下是开发代码

<div class="chosen-container chosen-container-single chosen-container-active" style="width: inherit;" title="" id="Merchantserviceproviders_country_chosen">
<a tabindex="-1" class="chosen-single"><span>United States</span><div><b></b></div></a>
<div class="chosen-drop"><div class="chosen-search"><input type="text" autocomplete="off"></div>
<ul class="chosen-results">
<li class="active-result" style="" data-option-array-index="0">Please select country</li>
<li class="active-result" style="" data-option-array-index="1">United States</li>
<li class="active-result" style="" data-option-array-index="2">Canada</li>
<li class="active-result" style="" data-option-array-index="3">United Kingdom</li>
<li class="active-result" style="" data-option-array-index="4">Ireland</li>
<li class="active-result" style="" data-option-array-index="5">South Africa</li>
<li class="active-result" style="" data-option-array-index="6">Turkey</li></ul></div></div>                                    
<span class="note"><div style="display:none" id="Merchantserviceproviders_country_em_" class="errorMessage"></div></span></div>

虽然我可以通过以下使用 xpath 选择它;(在这种情况下为美国)

$I->click(['css' => 'div#Merchantserviceproviders_country_chosen']);
$I->click(['xpath' => '//div[@id="Merchantserviceproviders_country_chosen"]/div/ul/li[2]']);

但问题是选择是通过列表中值的位置进行的,因此如果添加了任何新值(国家/地区),在我知道它的位置之前,我将无法选择它。

我想知道如何使用其值(在我的情况下为美国)而不是列表中的位置来选择国家/地区。

在这种情况下使用selectOption

$I->selectOption('dropdown-locator', 'option');

例:

$I->selectOption('div#Merchantserviceproviders_country_chosen', 'United States');
好吧,

我目前想出了以下解决方法,这对我来说似乎是一个更好的选择,

$I->click(['css' => 'div#Merchantserviceproviders_country_chosen']);
$I->fillField(['xpath' => '//div[@id="Merchantserviceproviders_country_chosen"]/div/div/input'], 'United States'); 
$I->click(['xpath' => '//div[@id="Merchantserviceproviders_country_chosen"]/div/ul/li[1]']);
在第 1 行中,我

单击下拉列表,在第 2 行中,我输入了我想要的国家/地区全名以缩小结果范围(请记住,这是一个带有搜索功能的下拉列表),,,在第 3 行中,在下拉列表中显示搜索结果后,我单击了第一个选项

最新更新