Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/main/java/com/wiley/autotest/actions/RepeatableAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -79,6 +93,22 @@ public void perform() {
}
}

public T performAndGet() {
Copy link
Contributor

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

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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Expand Down
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
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
Expand Down Expand Up @@ -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;
Expand All @@ -11,6 +12,9 @@ public interface Window {

void switchToLast();

void switchToLastAfter(Actions action);
void switchToLastAfter(TeasyElement element);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
However, I like the idea of adding such layer of actions+conditions. Lets discuss in skype a potential place where we can have such layer of actions.


void switchTo(WindowMatcher matcher);

void close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.wiley.autotest.actions.RepeatableAction;
import com.wiley.autotest.selenium.context.AbstractPage;
import com.wiley.autotest.selenium.context.SearchStrategy;
import com.wiley.autotest.selenium.elements.upgrade.TeasyElement;
import org.assertj.core.api.Assertions;
import org.openqa.selenium.By;
import org.springframework.stereotype.Component;

Expand All @@ -13,7 +15,9 @@
public class RepeaterPage extends AbstractPage {

public RepeaterPage negativeCheck() {
new RepeatableAction(this::clickOnFailure).perform();
new RepeatableAction(this::clickOnFailure)
.message("The Div3 isn't displayed.")
.perform();
return this;
}

Expand All @@ -22,6 +26,26 @@ public RepeaterPage positiveCheck() {
return this;
}

public RepeaterPage outputCheck() {
String string = new RepeatableAction<>(
() -> {
element(By.cssSelector("[id='testDiv2']")).click();
TeasyElement element = element(By.cssSelector("[id='testDiv3']"), new SearchStrategy(1).nullOnFailure());
if (element != null) {
return element.getText();
} else {
return "No text";
}
},
(text) -> text.equals("Div3"),
10,
1000)
.message("No text for element.")
.performAndGet();
Assertions.assertThat(string).isEqualTo("Div3");
return this;
}

private boolean clickOnFailure() {
clickElement("[id='testDiv1']");
return isElementDisplayed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ public void performAction() {
.positiveCheck();
}

@Test()
public void performActionWithOutput() {
openPage("repeater.html", RepeaterPage.class)
.outputCheck();
}

@Test(expectedExceptions = StopTestExecutionException.class)
public void unableToPerformAction() {
openPage("repeater.html", RepeaterPage.class)
.negativeCheck();
}

}