繁体   English   中英

量角器下拉列表元素e2e测试

[英]Protractor dropdown list elemente e2e test

我是Protractor的新手,我正在执行一些e2e测试,而当我尝试调用下拉列表并选择其中一个选项时,我在上一个问题中遇到了问题。

这是我的代码:

it('filter', function() {
  console.log('test log');

  var e = document.getElementById('Develop');
  var strUser = e.options[e.selectedIndex].value;

  e.selectedIndex = 2;
});

我每次得到的referenceError是:

document is not defined

这个错误怎么可能?

谢谢您的帮助。

在量角器规格中,应使用element来定位DOM元素:

it('filter', function() {
  console.log('test log');

  var e = element(by.id('Develop');
  var strUser = element(by.css('#Develop option:2')).getAttribute('value');
});

这可能对您有帮助:

我还建议您在开始之前阅读量角器文档

我也在进行e2e测试,并且为了选择一个下拉选项,我有几个功能:

选择一个按值下拉列表。

this.selectDropdownByValue = function (dropdownElement, value) {
        return this.selectDropdownByAttribute(dropdownElement, 'value', value);
};

按索引选择一个下拉列表:

this.selectDropdownByIndex = function (dropdownElement, index) {
        dropdownElement.click();
        dropdownElement.all(by.css('option')).get(index).click();
};

按部分标签选择一个下拉列表:

this.selectDropdownByPartialLabel = function (dropdownElement, partialLabel) {
        return this.selectDropdownByAttribute(dropdownElement, 'label', partialLabel, true);
};

按标签选择一个下拉列表:

this.selectDropdownByLabel = function (dropdownElement, label) {
        return this.selectDropdownByAttribute(dropdownElement, 'label', label);
    };

每个下拉函数中使用的函数是:

this.selectDropdownByAttribute = function (dropdownElement, attribute, value, partial, index) {
        var pathModifier = '';

        if (typeof index === 'undefined') {
            index = 0;
        }

        if (typeof partial === 'undefined') {
            partial = false;
        }

        if (partial) {
            pathModifier = '*';
        }

        dropdownElement.click().then(function () {
            dropdownElement.all(by.css('option[' + attribute + pathModifier + '="' + value + '"]')).get(index).click();
        });
    };

我希望这有帮助。

尝试与下拉菜单互动时遇到了相同的问题,我设法使用了这种格式,这有助于我选择所需的确切下拉列表元素

。元素(by.model( 'Model.name'))点击()元件(By.xpath( '的Xpath'))点击();

因此,我基本上已经放置了下拉菜单的位置,单击它,然后直接通过它的xpath找到下拉菜单元素,并在一行中单击它(不分隔)。

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM