select – >选项抽象

在Python,Java和其他一些Selenium绑定中, select->option结构是一个Select类 ,这是一个非常方便的抽象。

例如,假设有以下select标签:

 <select id="fruits" class="select" name="fruits"> <option value="1">Banana</option> <option value="2">Mango</option> </select> 

以下是我们如何在Python中操作它:

 from selenium.webdriver.support.ui import Select select = Select(driver.find_element_by_id('fruits')) # get all options print select.options # get all selected options print select.all_selected_options # select an option by value select.select_by_value('1') # select by visible text select.select_by_visible_text('Mango') 

换句话说,这是一个非常透明和易于使用的抽象

是否有可能以类似的方式操纵量angular器中的select标签?


这不是如何select下拉protractorjs e2etesting或如何点击在量angular器testingselect框中的选项的副本?

量angular器中没有这样的东西,但我们可以自己写:

select-wrapper.js

 'use strict'; var SelectWrapper = function(selector) { this.webElement = element(selector); }; SelectWrapper.prototype.getOptions = function() { return this.webElement.all(by.tagName('option')); }; SelectWrapper.prototype.getSelectedOptions = function() { return this.webElement.all(by.css('option[selected="selected"]')); }; SelectWrapper.prototype.selectByValue = function(value) { return this.webElement.all(by.css('option[value="' + value + '"]')).click(); }; SelectWrapper.prototype.selectByPartialText = function(text) { return this.webElement.all(by.cssContainingText('option', text)).click(); }; SelectWrapper.prototype.selectByText = function(text) { return this.webElement.all(by.xpath('option[.="' + text + '"]')).click(); }; module.exports = SelectWrapper; 

用法

 var SelectWrapper = require('select-wrapper'); var mySelect = new SelectWrapper(by.id('fruits')); # select an option by value mySelect.selectByValue('1'); # select by visible text mySelect.selectByText('Mango'); 

请注意, Select是JavaScript中的保留字

启动Protractor v.0.22.0,你可以使用新的By.cssContainingText定位器 :

 element(by.cssContainingText('option', 'Mango')); 

请参阅API参考 。

使用手稿编码:

标记名:

by.tagName( '选项')

by.tagName( 'MD-选项')

by.tagName( '礼')

 selectOption(selector: string, item: string) { let selectList: any; let desiredOption: any; selectList = element(by.css(selector)); selectList.click(); selectList.findElements(by.tagName('option')) .then(function findMatchingOption(options: any) { options.some(function (option: any) { option.getText().then(function doesOptionMatch(text: string) { if (item === text) { desiredOption = option; return true; } }); }); }) .then(function clickOption() { if (desiredOption) { desiredOption.click(); } }); } 

用过的:

 selectOption('//select[@id="food"]', 'Pizza'); 
Interesting Posts