如何在selenium(NodeJs)的下拉菜单中选择选项?

问题描述 投票:0回答:1

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

先谢谢你

node.js selenium selenium-webdriver select dropdown
1个回答
0
投票

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

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");
    }) ;
© www.soinside.com 2019 - 2024. All rights reserved.