-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Waiter for new window appeared after an action added. #153
base: master
Are you sure you want to change the base?
Changes from all commits
7005779
bb41d4b
d9a02c2
88b25a3
630e364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import com.wiley.autotest.services.StopTestExecutionException; | ||
import com.wiley.autotest.utils.TestUtils; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
|
@@ -11,7 +12,7 @@ | |
* <p> | ||
* If the condition does not become true after all attempts - AssertionError is thrown | ||
*/ | ||
public class RepeatableAction { | ||
public class RepeatableAction<T> { | ||
|
||
private Actions action; | ||
private Conditions condition; | ||
|
@@ -20,6 +21,8 @@ public class RepeatableAction { | |
private final int millisecondsBetweenAttempts; | ||
private int attemptCounter = 0; | ||
private String errorMessage; | ||
private Supplier<T> output; | ||
private Function<T, Boolean> dependedCondition; | ||
|
||
/** | ||
* By default will try 5 types and sleep 3 seconds after each attempt | ||
|
@@ -53,7 +56,18 @@ public RepeatableAction(Actions action, Conditions condition, int numberOfAttemp | |
this.millisecondsBetweenAttempts = millisecondsBetweenAttempts; | ||
} | ||
|
||
public RepeatableAction message(String errorMessage) { | ||
public RepeatableAction(Supplier<T> output, Function<T, Boolean> condition, int millisecondsBetweenAttempts) { | ||
this(output, condition, 5, millisecondsBetweenAttempts); | ||
} | ||
|
||
public RepeatableAction(Supplier<T> output, Function<T, Boolean> condition, int numberOfAttempts, int millisecondsBetweenAttempts) { | ||
this.output = output; | ||
this.dependedCondition = condition; | ||
this.numberOfAttempts = numberOfAttempts; | ||
this.millisecondsBetweenAttempts = millisecondsBetweenAttempts; | ||
} | ||
|
||
public RepeatableAction<T> message(String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
return this; | ||
} | ||
|
@@ -79,6 +93,22 @@ public void perform() { | |
} | ||
} | ||
|
||
public T performAndGet() { | ||
T result = null; | ||
for (int i = 0; i < numberOfAttempts; i++) { | ||
T localResult = output.get(); | ||
if (dependedCondition.apply(localResult)) { | ||
result = localResult; | ||
break; | ||
} else { | ||
TestUtils.waitForSomeTime(millisecondsBetweenAttempts, "Sleeping inside action repeater"); | ||
} | ||
} | ||
if (result == null) { | ||
throw new StopTestExecutionException(getErrorMessage()); | ||
} else return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add brackets |
||
} | ||
|
||
private String getErrorMessage() { | ||
String baseMessage = "Unable to perform actions after " + numberOfAttempts + " attempts"; | ||
if (errorMessage != null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.wiley.autotest.selenium.elements.upgrade; | ||
|
||
import com.wiley.autotest.actions.Actions; | ||
import com.wiley.autotest.selenium.Report; | ||
import com.wiley.autotest.selenium.elements.upgrade.conditions.PageLoaded; | ||
import com.wiley.autotest.selenium.elements.upgrade.conditions.window.WindowMatcher; | ||
import com.wiley.autotest.selenium.elements.upgrade.waitfor.CustomWaitFor; | ||
import io.appium.java_client.AppiumDriver; | ||
import org.openqa.selenium.*; | ||
|
||
import java.net.URL; | ||
import java.util.Iterator; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Represents browser window | ||
|
@@ -32,6 +35,36 @@ public void switchToLast() { | |
driver.switchTo().window(window); | ||
} | ||
|
||
/** | ||
* To be sure that new window appeared after an action. | ||
* For example, after clicking on a link with new | ||
* window target. | ||
* | ||
* Example usage: switchToLastAfter(() -> element.click()); | ||
* | ||
* @param action - an action to do before new window appeared | ||
* and before switching. | ||
*/ | ||
@Override | ||
public void switchToLastAfter(Actions action) { | ||
int windowCount = driver.getWindowHandles().size(); | ||
action.execute(); | ||
new CustomWaitFor().condition(newWindowAppeared(), windowCount); | ||
switchToLast(); | ||
} | ||
|
||
/** | ||
* To be sure that new window appeared after click on element. | ||
* For example, after clicking on a link with new | ||
* window target. | ||
* | ||
* @param element - an element to click to new window switch. | ||
*/ | ||
@Override | ||
public void switchToLastAfter(TeasyElement element) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a note about name being unclear. "switch to last after element" - says nothing about the click inside. |
||
switchToLastAfter(element::click); | ||
} | ||
|
||
@Override | ||
public void switchTo(WindowMatcher matcher) { | ||
fluentWait.waitFor(matcher.get().findAndSwitch()); | ||
|
@@ -106,4 +139,11 @@ public void maximize() { | |
public void scrollTo(TeasyElement element) { | ||
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); | ||
} | ||
|
||
private Function<Integer, Boolean> newWindowAppeared() { | ||
return count -> { | ||
int currentWindowsCount = driver.getWindowHandles().size(); | ||
return count != currentWindowsCount; | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.wiley.autotest.selenium.elements.upgrade; | ||
|
||
import com.wiley.autotest.actions.Actions; | ||
import com.wiley.autotest.selenium.elements.upgrade.conditions.window.WindowMatcher; | ||
|
||
import java.net.URL; | ||
|
@@ -11,6 +12,9 @@ public interface Window { | |
|
||
void switchToLast(); | ||
|
||
void switchToLastAfter(Actions action); | ||
void switchToLastAfter(TeasyElement element); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would prefer keeping Window api straightforward and clean. Window represents a browser window and keeping it independent from anything like after click, after action is a plus from my point of view. |
||
|
||
void switchTo(WindowMatcher matcher); | ||
|
||
void close(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whenever we need to add "and" in a name - it might be a sign that something might be wrong with our design