在if语句中

时间:2019-05-26 12:07:38

标签: typescript protractor

我想如果未选择元素,则只有我应该选择它,这里我在打字稿中使用else,但对我不起作用

let inputEle = element(by.xpath('//div[@class="pq-select-menu"]//span[text()="Users(1003)"]/preceding-sibling::input'));
console.log(await inputEle.isSelected()); //true or false
let val=inputEle.isSelected();

if (val== true){
    console.log('Is Already selected');
}else{
    await element(by.xpath('//*[@id="quincbody"]/*//div[contains(@class, "pq-select-menu")]//*[contains(text(),"Users(1003)")]')).click();
}

1 个答案:

答案 0 :(得分:1)

量角器履行承诺。因此,val将属于Promise<boolean> 您可以尝试以下方法:

        let inputEle = element(by.xpath('//div[@class="pq-select-menu"]//span[text()="Users(1003)"]/preceding-sibling::input'));
        inputEle.isSelected().then(selected => {
            if (selected) {
                console.log('Is Already selected');
            } else {
                await element(by.xpath('//*[@id="quincbody"]/*//div[contains(@class, "pq-select-menu")]//*[contains(text(),"Users(1003)")]')).click();
            }
            // the rest of the code if there's any.
        });