简体   繁体   English

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

[英]Protractor dropdown list elemente e2e test

i am new with Protractor, i am executing some e2e test and i have problems in this last one, when i try to call a dropdown list and select one of those options. 我是Protractor的新手,我正在执行一些e2e测试,而当我尝试调用下拉列表并选择其中一个选项时,我在上一个问题中遇到了问题。

this is my code: 这是我的代码:

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

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

  e.selectedIndex = 2;
});

The referenceError that i get every time is: 我每次得到的referenceError是:

document is not defined

how is possible that error ? 这个错误怎么可能?

thanks for your help in advance. 谢谢您的帮助。

In Protractor specs you should use element to locate your DOM elements: 在量角器规格中,应使用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');
});

This might help you : 这可能对您有帮助:

I also advise you to read Protractor docs before starting. 我还建议您在开始之前阅读量角器文档

I am also working on e2e test, and I have several functions in order to choose a dropdown option: 我也在进行e2e测试,并且为了选择一个下拉选项,我有几个功能:

Select a drop down by value. 选择一个按值下拉列表。

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

Select a drop down by index: 按索引选择一个下拉列表:

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

Select a drop down by partial label: 按部分标签选择一个下拉列表:

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

Select a drop down by label: 按标签选择一个下拉列表:

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

And the function that is used inside each drop down function is: 每个下拉函数中使用的函数是:

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();
        });
    };

I hope this helps. 我希望这有帮助。

I came across the same issue when trying to interact with dropdowns and I managed to use this format which has helped me select the exact dropdown elements i want 尝试与下拉菜单互动时遇到了相同的问题,我设法使用了这种格式,这有助于我选择所需的确切下拉列表元素

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

So I have basically put in the location of the dropdown, click it, and directly after that locate the dropdown element via its xpath and click it in one line not separated. 因此,我基本上已经放置了下拉菜单的位置,单击它,然后直接通过它的xpath找到下拉菜单元素,并在一行中单击它(不分隔)。

Hope this helps 希望这可以帮助

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

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