我正在使用TestCafe 1.8.1,并且案例与https://devexpress.github.io/testcafe/documentation/recipes/test-select-elements.html上的文档略有不同-我的问题是示例假定value
一个<option>
和它的文本内容将是相同的,在我的情况下,value
是非常不可预测的值。
我可以毫无问题地在下拉菜单中选择一个项目,.withText(value)
用于过滤选项并.click(item)
选择它。但是,我的应用程序随后刷新了页面,并应在加载时重新选择相关项目。这是行不通的,我想测试一下。
所以我可能在选择中有一些选项,例如:
<select id="foo">
<option value="1234">100x100</option>
<option value="5432">200x100</option>
<option value="9999">100x200</option>
</select>
显然,如果我使用.expect(citySelect.value).eql('London');
docs进行测试,则会失败,因为这些值与文本内容完全不同,例如,在下拉列表中单击“ 200x100”,则该值将变为“ 5432”。
我是否需要使用aClientFunction
来获取所选项目的文本?我知道将数据传递到ClientFunction相当尴尬,我是否需要传递id
select的内容,以便ClientFunction可以getElementById
找到select并检索其selected选项的文本内容?听起来一切都做错了。
请检查以下使用ClientFunction
API获取option
值的示例:
import { Selector, ClientFunction } from 'testcafe';
fixture `Fixture 1`
.page `https://kys0l.csb.app/`;
test('Test 1', async t => {
const selector = Selector('select');
const getValue = ClientFunction((index) => {
const select = selector();
return select.options[index].value;
}, { dependencies: { selector } });
await t
.expect(getValue(0)).eql('1234')
.expect(getValue(1)).eql('5432')
.expect(getValue(2)).eql('9999');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句