选择 - >选项抽象

ale*_*cxe 14 python testing selenium selenium-webdriver protractor

在Python,Java和其他几个selenium绑定中,对select->optionHTML结构(一个Select类)有一个非常方便的抽象.

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

<select id="fruits" class="select" name="fruits">
    <option value="1">Banana</option>
    <option value="2">Mango</option>
</select>
Run Code Online (Sandbox Code Playgroud)

以下是我们如何在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')
Run Code Online (Sandbox Code Playgroud)

换句话说,它是一个非常透明且易于使用的抽象.

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


这与如何在下拉量程器e2e测试中选择选项如何在量角器测试中的选择框中单击选项不重复.

Del*_*kin 24

在Protractor中没有这样的东西,但我们可以写自己的:

选择-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;
Run Code Online (Sandbox Code Playgroud)


用法

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');
Run Code Online (Sandbox Code Playgroud)


请注意,SelectJavaScript中保留字