2

I have while loop which is supposed to run while condition1 is true or while condition2 is true. 1 or 2 case depends on some initial boolean condition.

Now I implemented this as a ternary operator, but I'd like to understand how this can be done using a Predicate, and lambda expressions. Can someone provide an example please? How can I declare a Function variable and put it into the while loop condition?

boolean initialCondition = stopBtn.getText().length() > 0;

while(initialCondition ? stopBtn.waitToBeHidden() : stopBtn.isDisplayed()) {
    if (a++ < 5) {
        stopBtn.click();
    }
}
return this;
1
  • Could you show some pseudo-code of what you want to achieve? I don't quite know where you want to apply a predicate... Commented Feb 15, 2016 at 16:48

2 Answers 2

5

I think even in a predicate you'd need your if/else.

If you provide more code it would probably be easier to see what you're aiming to do. One way of using a predicate (that you can then pass around etc.) would be:

Predicate<StopBtnClass> pred = btn -> btn.getText().length() > 0 ? btn.waitToBeHidden() : btn.isDisplayed();

while(pred.test(stopBtn)) {
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I understand regarding if/else in predicate. I just confused when tried to apply predicate in 'while' condition. I forgot about .test(...) method. But your example works great for me, than you!
4

You can use a BooleanSupplier to model the condition:

java.util.function.BooleanSupplier condition = stopBtn.getText().length() > 0 ?
     stopBtn::waitToBeHidden : 
     stopBtn::isDisplayed;

while (condition.getAsBoolean()) {
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.