如何从硒(NodeJs)下拉菜单中选择选项

时间:2020-05-04 05:56:47

标签: node.js selenium selenium-webdriver select dropdown

我想写一个脚本来测试网页内的下拉菜单。但是,我无法使用Nodejs在Selenium中做到这一点。有没有什么方法可以测试硒(NodeJs)中的下拉列表。

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以创建一个这样的选择类,然后根据您的选择使用方法。

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.js');
var mySelect = new SelectWrapper(by.id("validSelectTagID"));

describe("Select Wrapper",function(){
    it("Handling the dropdown list",function(){
        browser.get("http://validURL");
        mySelect.selectByText("TextinDrop");
        mySelect.selectByValue("ValueInDrop");
    }) ;