Protractor if ,else with (expect(condition))

2019-06-12 03:49发布

问题:

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

回答1:

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


回答2:

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?