Protractor if ,else with (expect(condition))

2019-06-12 03:23发布

in my script i need to include if and else statement,here is the code

if((element(by.model('trt_model')).all(by.tagName('option')).get(0).getText()).toEqual('Select Contract'))
{
    element(by.model('trt_model')).get(1).click();
}
else
{
    element(by.model('trt_model')).get(0).click();
}

if the expect condition fails , i want the script to execute the else part, but this is not working. when expect condition fails,the script is not executing the else part

kindly suggest how this can be resolved Thank you

2条回答
啃猪蹄的小仙女
2楼-- · 2019-06-12 04:06

I have simplified your code and pasted below. You can try this one. Please let me know if you face any error with below code. Hope it will work.

element(by.model('trt_model')).all(by.tagName('option')).get(0).getText  
 ()).then(function(text){
        if(text==='Select Contract')
            element(by.model('trt_model')).get(1).click();
       else
           element(by.model('trt_model')).get(0).click();
});
查看更多
Deceive 欺骗
3楼-- · 2019-06-12 04:06

I don't know how long is your list, but it seems that you can simplify your code. How about use:

element(by.model('trt_model')).get(last).click();

instead of whole if statement?

查看更多
登录 后发表回答