Skip to content

Commit 966795a

Browse files
committed
Added implicit waits for extracting data to stabilize tests
1 parent 642b6de commit 966795a

File tree

7 files changed

+153
-36
lines changed

7 files changed

+153
-36
lines changed

pogen4selenium-api/src/main/java/io/toolisticon/pogen4selenium/runtime/PageObjectParentImpl.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.lang.reflect.InvocationTargetException;
44
import java.time.Duration;
5-
import java.util.Arrays;
6-
import java.util.stream.Collectors;
75

86
import org.openqa.selenium.By;
97
import org.openqa.selenium.ElementNotInteractableException;
@@ -77,38 +75,27 @@ public <APO extends PageObjectParent<APO>> APO changePageObjectType(Class<APO> t
7775

7876
}
7977

80-
81-
78+
8279
protected void waitUntilUrl(String urlRegex) {
83-
Wait<WebDriver> wait =
84-
new FluentWait<>(driver)
85-
.withTimeout(Duration.ofSeconds(25))
86-
.pollingEvery(Duration.ofMillis(300));
87-
88-
wait.until(ExpectedConditions.urlMatches(urlRegex));
80+
PageObjectUtilities.waitForiPageToHaveMatchingUrl(driver, urlRegex);
8981
}
9082

91-
9283
protected void waitForElementToBeInteractable(String xpath) {
9384
waitForElementToBeInteractable(By.xpath(xpath));
9485
}
9586

87+
88+
9689
@Override
9790
public WebElement waitForElementToBeInteractable(By by) {
98-
Wait<WebDriver> wait =
99-
new FluentWait<>(driver)
100-
.withTimeout(Duration.ofSeconds(15))
101-
.pollingEvery(Duration.ofMillis(300))
102-
.ignoring(NoSuchElementException.class,ElementNotInteractableException.class);
103-
104-
return wait.until(ExpectedConditions.elementToBeClickable(by));
91+
return PageObjectUtilities.waitForElementToBeInteractable(driver, by);
10592
}
10693

10794

10895

10996
@Override
11097
public PAGEOBJECT waitForPageToContainText(String text) {
111-
waitForMessage(text);
98+
PageObjectUtilities.waitUntilPageSourceContains(driver, text);
11299
return (PAGEOBJECT) this;
113100
}
114101

@@ -125,23 +112,17 @@ public WebElement waitForElementToBeInteractable(ExpectedCondition<WebElement> e
125112
}
126113

127114
protected WebElement waitForElementToBeInteractable(WebElement element) {
128-
if(element == null) {
129-
return null;
130-
}
131-
132-
Wait<WebDriver> wait =
133-
new FluentWait<>(driver)
134-
.withTimeout(Duration.ofSeconds(15))
135-
.pollingEvery(Duration.ofMillis(300))
136-
.ignoring(ElementNotInteractableException.class);
137-
138-
return wait.until(ExpectedConditions.elementToBeClickable(element));
115+
return PageObjectUtilities.waitForElementToBeInteractable(driver, element);
139116
}
140117

118+
119+
141120
protected void waitForElementToBePresent(String xpath) {
142121
waitForElementToBePresent(By.xpath(xpath));
143122
}
144123

124+
125+
145126
@Override
146127
public WebElement waitForElementToBePresent(By by) {
147128
Wait<WebDriver> wait =
@@ -154,6 +135,7 @@ public WebElement waitForElementToBePresent(By by) {
154135

155136
}
156137

138+
/*-
157139
158140
public WebElement waitForElementToBePresent(ExpectedCondition<WebElement> expectedCondition) {
159141
Wait<WebDriver> wait =
@@ -165,7 +147,7 @@ public WebElement waitForElementToBePresent(ExpectedCondition<WebElement> expect
165147
return wait.until(expectedCondition);
166148
167149
}
168-
150+
*/
169151

170152

171153

@@ -175,6 +157,7 @@ public SearchContext getSearchContext() {
175157
}
176158

177159

160+
/*-
178161
179162
protected void waitForElementToBeAbsent(String xpath) {
180163
Wait<WebDriver> wait =
@@ -194,13 +177,14 @@ protected void waitForElementToBeAbsent(WebElement element) {
194177
195178
wait.until(ExpectedConditions.stalenessOf(element));
196179
}
197-
180+
198181
protected void waitForMessage(String message) {
199182
200183
waitForElementToBePresent("//*[text()[contains(.,'" + message+ "')]]");
201184
202185
}
203186
187+
204188
protected void waitForMessageToVanish(String ... messages) {
205189
206190
String criterias = Arrays.stream(messages).map(e -> "contains(.,'" + e + "')").collect(Collectors.joining(" or "));
@@ -211,6 +195,7 @@ protected void waitForMessageToVanish(String ... messages) {
211195
waitForElementToBeAbsent(element);
212196
213197
}
198+
*/
214199

215200

216201
protected void writeToElement(WebElement webElement, String toSet) {
@@ -224,6 +209,6 @@ protected void writeToElement(WebElement webElement, String toSet) {
224209
protected void doPageRefresh() {
225210
driver.navigate().refresh();
226211
}
227-
212+
228213

229214
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.toolisticon.pogen4selenium.runtime;
2+
3+
import java.time.Duration;
4+
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.ElementNotInteractableException;
7+
import org.openqa.selenium.NoSuchElementException;
8+
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.WebElement;
10+
import org.openqa.selenium.support.ui.ExpectedConditions;
11+
import org.openqa.selenium.support.ui.FluentWait;
12+
import org.openqa.selenium.support.ui.Wait;
13+
14+
public class PageObjectUtilities {
15+
16+
/**
17+
* Hidden constructor
18+
*/
19+
private PageObjectUtilities(){
20+
21+
}
22+
23+
24+
/**
25+
* Waits for page to have a matching url.
26+
* @param driver the web driver
27+
* @param urlRegex the regular expression that the url must match
28+
*/
29+
public static void waitForiPageToHaveMatchingUrl(WebDriver driver, String urlRegex) {
30+
Wait<WebDriver> wait =
31+
new FluentWait<>(driver)
32+
.withTimeout(Duration.ofSeconds(25))
33+
.pollingEvery(Duration.ofMillis(300));
34+
35+
wait.until(ExpectedConditions.urlMatches(urlRegex));
36+
}
37+
38+
39+
40+
/**
41+
* Wait for an element to exist and to be interactable.
42+
* @param driver the web driver
43+
* @param by the locator for the element
44+
* @return the web element
45+
*/
46+
public static WebElement waitForElementToBeInteractable(WebDriver driver, By by) {
47+
Wait<WebDriver> wait =
48+
new FluentWait<>(driver)
49+
.withTimeout(Duration.ofSeconds(15))
50+
.pollingEvery(Duration.ofMillis(300))
51+
.ignoring(NoSuchElementException.class,ElementNotInteractableException.class);
52+
53+
return wait.until(ExpectedConditions.elementToBeClickable(by));
54+
}
55+
56+
57+
/**
58+
* Wait if passed element is interactable.
59+
* @param driver the web driver
60+
* @param element the element to wait for the condition
61+
* @return the web element
62+
*/
63+
protected static WebElement waitForElementToBeInteractable(WebDriver driver, WebElement element) {
64+
if(element == null) {
65+
return null;
66+
}
67+
68+
Wait<WebDriver> wait =
69+
new FluentWait<>(driver)
70+
.withTimeout(Duration.ofSeconds(15))
71+
.pollingEvery(Duration.ofMillis(300))
72+
.ignoring(ElementNotInteractableException.class);
73+
74+
return wait.until(ExpectedConditions.elementToBeClickable(element));
75+
}
76+
77+
78+
/**
79+
* Wait for element to be present.
80+
* @param driver web driver
81+
* @param by the locator to use
82+
* @return the web element
83+
*/
84+
public static WebElement waitForElementToBePresent(WebDriver driver, By by) {
85+
Wait<WebDriver> wait =
86+
new FluentWait<>(driver)
87+
.withTimeout(Duration.ofSeconds(15))
88+
.pollingEvery(Duration.ofMillis(300))
89+
.ignoring(NoSuchElementException.class);
90+
91+
return wait.until(ExpectedConditions.presenceOfElementLocated(by));
92+
93+
}
94+
95+
/**
96+
* Wait for element to be absent
97+
* @param driver the web driver
98+
* @param element the web element to check
99+
*/
100+
public static void waitForElementToBeAbsent(WebDriver driver, WebElement element) {
101+
Wait<WebDriver> wait =
102+
new FluentWait<>(driver)
103+
.withTimeout(Duration.ofSeconds(15))
104+
.pollingEvery(Duration.ofMillis(300));
105+
106+
wait.until(ExpectedConditions.stalenessOf(element));
107+
}
108+
109+
110+
/**
111+
* Wait until page source contains.
112+
* @param driver the web driver
113+
* @param text the test to search
114+
*/
115+
public static void waitUntilPageSourceContains(WebDriver driver, String text) {
116+
117+
waitForElementToBePresent(driver, By.xpath("//*[text()[contains(.,'" + text + "')]]"));
118+
119+
}
120+
121+
122+
123+
124+
public static void doPageRefresh(WebDriver driver) {
125+
driver.navigate().refresh();
126+
}
127+
128+
129+
}

pogen4selenium-processor/dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>pogen4selenium</artifactId>
55
<groupId>io.toolisticon.pogen4selenium</groupId>
6-
<version>0.9.2-SNAPSHOT</version>
6+
<version>0.10.1-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99
<artifactId>pogen4selenium-processor</artifactId>

pogen4selenium-processor/src/main/java/io/toolisticon/pogen4selenium/processor/pageobject/ExtractDataValueWrapperExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static String getFinalMethodCall(ExtractDataValueWrapper extractDataValue
1212
String command = (
1313
extractDataValueWrapper.by() == _By.ELEMENT ?
1414
extractDataValueWrapper.value() + "Element"
15-
: "getDriver().findElement(By." + extractDataValueWrapper.by().getCorrespondingByMethodName() + "(\"" + extractDataValueWrapper.value() + "\"))"
15+
: "PageObjectUtilities.waitForElementToBePresent(getDriver(), By." + extractDataValueWrapper.by().getCorrespondingByMethodName() + "(\"" + extractDataValueWrapper.value() + "\"))"
1616
)
1717
+ ".";
1818

pogen4selenium-processor/src/main/java/io/toolisticon/pogen4selenium/processor/pageobject/ExtractDataWrapperExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static String getFinalMethodCall(ExtractDataWrapper dataToExtractWrapper)
6464
if (isList(dataToExtractWrapper)) {
6565
return "getDriver().findElements(By." + dataToExtractWrapper.by().getCorrespondingByMethodName() + "(\""+ dataToExtractWrapper.value() + "\")).stream().map(e -> new " + dataToExtractWrapper.getExtractedDataImplName() + "(getDriver(), e)).collect(Collectors.toList());";
6666
} else {
67-
return "new " + dataToExtractWrapper.getExtractedDataImplName() +"(getDriver(), getDriver().findElement(By." + dataToExtractWrapper.by().getCorrespondingByMethodName() + "(\"" + dataToExtractWrapper.value() + "\")));";
67+
return "new " + dataToExtractWrapper.getExtractedDataImplName() +"(getDriver(), PageObjectUtilities.waitForElementToBePresent(getDriver(), By." + dataToExtractWrapper.by().getCorrespondingByMethodName() + "(\"" + dataToExtractWrapper.value() + "\")));";
6868
}
6969

7070
}

pogen4selenium-processor/src/main/resources/DataToExtract.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.openqa.selenium.support.How;
1414
import io.toolisticon.pogen4selenium.api._By;
1515
import io.toolisticon.pogen4selenium.api.ExtractDataValue;
1616
import io.toolisticon.pogen4selenium.runtime.DataObjectParentImpl;
17+
import io.toolisticon.pogen4selenium.runtime.PageObjectUtilities;
1718

1819
/**
1920
* An empty class.

pogen4selenium-processor/src/main/resources/PageObject.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ${import};
55
!{/for}
66
import java.util.stream.Collectors;
77

8+
import io.toolisticon.pogen4selenium.runtime.PageObjectUtilities;
9+
810
import org.openqa.selenium.By;
911
import org.openqa.selenium.WebDriver;
1012
import org.openqa.selenium.WebElement;

0 commit comments

Comments
 (0)