TypeError:单击不是下拉列表的功能

时间:2018-12-05 20:57:16

标签: typescript protractor

有人可以更正我的代码吗,我需要从下拉列表中单击给定值?它确实会打印值,但是我无法成功单击匹配的值。提前致谢。下面是我的代码。

async getDropDownHeaderMenu(selectedOption:string){
    let headersCols = element( by.css('#abc'));
    await browser.wait(until.presenceOf(headersCols), this.DEFAULT_WAIT_TIME_SECONDS * 1000, 'failed to find dropdowns!');
    headersCols.click();
    let valuesSet: Set<string> = new Set();
    let promisesArray = [];
    return new Promise((resolve, reject) => {
        element.all(by.css('div.abc')).map((option) => {
            promisesArray.push(option.getText());
        }).then(() => {
            protractor.promise.all(promisesArray).then((results) => {
                for(let result of results) {
                    console.log("Getting the drop down values = " + result);
                    // store the values in result and match it with given input
                    valuesSet.add(result);
                    if(result === selectedOption){
                        browser.executeScript('window.scrollTo(0,document.body.scrollHeight)').then(()=>{
                        //// This is where the error pops up
                            result.click();
                            browser.sleep(10000);

                        });
                        //result.click();
                      // break;
                    }
                }

            }).then(() => {
                expect(valuesSet.size).not.toEqual(0,
                    "The returned string set from drop down list was empty!");
                resolve(valuesSet);
            });
        }).catch((error) => {
            reject(error);
        });
    });
}

1 个答案:

答案 0 :(得分:0)

async getDropDownHeaderMenu(selectedOption:string){
    let headersCols = element( by.css('#abc-select'));
    await browser.wait(until.presenceOf(headersCols), this.DEFAULT_WAIT_TIME_SECONDS * 1000, 'failed to find dropdowns arrow!');
    headersCols.click(); //locate the dropdown arrow and click to populate the list
    let valuesSet: Set<string> = new Set();
    let promisesArray = [];
    return new Promise((resolve, reject) => {
        //locate the items in the list panel
        element.all(by.css('span.abc-text')).map((option) => {
            promisesArray.push(option.getText()); // add the text of each item in the list
        }).then(() => {
            protractor.promise.all(promisesArray).then((results) => {
                for(let result of results) {
                    console.log("Getting the drop down values = " + result);
                    // store the values in result and match it with given input
                    valuesSet.add(result);
                    if(result === selectedOption){
                    //locate the item to be clicked in the list array based on index after matching the selectedOption
                         let itemTobeSelected = element.all( by.css(span.abc-text)).get(results.indexOf(result));
                        browser.wait(until.presenceOf(itemTobeSelected), this.DEFAULT_WAIT_TIME_SECONDS * 1000, 'failed to load the matched item in the drop down list.');
                        //scroll down the list panel to get all the items
                        browser.executeScript("arguments[0].scrollIntoView();", itemTobeSelected.getWebElement());
                        itemTobeSelected.click(); //once the item is matched with selectedOption stop iteration
                        break;
                    }
                }

            }).then(() => {
                expect(valuesSet.size).not.toEqual(0,
                    "The returned string set from drop down list was empty!");
                resolve(valuesSet);
            });
        }).catch((error) => {
            reject(error);
        });
    });
}
相关问题