Skip to content

Commit 7bba7f6

Browse files
committed
[java] Minor performance and style improvements of ExpectedConditions
1 parent 579e508 commit 7bba7f6

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

+22-33
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.Arrays;
3333
import java.util.List;
3434
import java.util.Optional;
35-
import java.util.regex.Matcher;
3635
import java.util.regex.Pattern;
3736

3837
/**
@@ -144,16 +143,13 @@ public String toString() {
144143
*/
145144
public static ExpectedCondition<Boolean> urlMatches(final String regex) {
146145
return new ExpectedCondition<Boolean>() {
146+
private final Pattern pattern = Pattern.compile(regex);
147147
private String currentUrl;
148-
private Pattern pattern;
149-
private Matcher matcher;
150148

151149
@Override
152150
public Boolean apply(WebDriver driver) {
153151
currentUrl = driver.getCurrentUrl();
154-
pattern = Pattern.compile(regex);
155-
matcher = pattern.matcher(currentUrl);
156-
return matcher.find();
152+
return pattern.matcher(currentUrl).find();
157153
}
158154

159155
@Override
@@ -881,7 +877,7 @@ public Boolean apply(WebDriver driver) {
881877
@Override
882878
public String toString() {
883879
return String.format("element found by %s to have value \"%s\". Current value: \"%s\"",
884-
locator, value, currentValue);
880+
locator, value, currentValue);
885881
}
886882
};
887883
}
@@ -1051,8 +1047,8 @@ public Boolean apply(WebDriver driver) {
10511047

10521048
@Override
10531049
public String toString() {
1054-
return String.format(property + " to be \"%s\". Current " + property + ": \"%s\"", value,
1055-
currentValue);
1050+
return String.format("DOM property '%s' to be '%s'. Current value: '%s'",
1051+
property, value, currentValue);
10561052
}
10571053
};
10581054
}
@@ -1079,8 +1075,8 @@ public Boolean apply(WebDriver driver) {
10791075

10801076
@Override
10811077
public String toString() {
1082-
return String.format(attribute + " to be \"%s\". Current " + attribute + ": \"%s\"", value,
1083-
currentValue);
1078+
return String.format("DOM attribute '%s' to be '%s'. Current value: '%s'",
1079+
attribute, value, currentValue);
10841080
}
10851081
};
10861082
}
@@ -1110,8 +1106,8 @@ public Boolean apply(WebDriver driver) {
11101106

11111107
@Override
11121108
public String toString() {
1113-
return String.format(attribute + " to be \"%s\". Current " + attribute + ": \"%s\"", value,
1114-
currentValue);
1109+
return String.format("Attribute or property '%s' to be '%s'. Current value: '%s'",
1110+
attribute, value, currentValue);
11151111
}
11161112
};
11171113
}
@@ -1132,8 +1128,8 @@ public static ExpectedCondition<Boolean> attributeContains(final WebElement elem
11321128
@Override
11331129
public Boolean apply(WebDriver driver) {
11341130
return getAttributeOrCssValue(element, attribute)
1135-
.map(seen -> seen.contains(value))
1136-
.orElse(false);
1131+
.map(seen -> seen.contains(value))
1132+
.orElse(false);
11371133
}
11381134

11391135
@Override
@@ -1159,16 +1155,13 @@ public static ExpectedCondition<Boolean> attributeContains(final By locator,
11591155
@Override
11601156
public Boolean apply(WebDriver driver) {
11611157
return getAttributeOrCssValue(driver.findElement(locator), attribute)
1162-
.map(seen -> seen.contains(value))
1163-
.orElse(false);
1158+
.map(seen -> seen.contains(value))
1159+
.orElse(false);
11641160
}
11651161

11661162
@Override
11671163
public String toString() {
1168-
return String.format(
1169-
"value found by %s to contain \"%s\".",
1170-
locator,
1171-
value);
1164+
return String.format("value found by %s to contain \"%s\".", locator, value);
11721165
}
11731166
};
11741167
}
@@ -1271,8 +1264,8 @@ public String toString() {
12711264
* @return subelement
12721265
*/
12731266
public static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy(
1274-
final By locator,
1275-
final By childLocator) {
1267+
final By locator, final By childLocator)
1268+
{
12761269
return new ExpectedCondition<WebElement>() {
12771270

12781271
@Override
@@ -1295,8 +1288,8 @@ public String toString() {
12951288
* @return subelement
12961289
*/
12971290
public static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy(
1298-
final WebElement element,
1299-
final By childLocator) {
1291+
final WebElement element, final By childLocator)
1292+
{
13001293

13011294
return new ExpectedCondition<WebElement>() {
13021295

@@ -1321,8 +1314,8 @@ public String toString() {
13211314
* @return subelement
13221315
*/
13231316
public static ExpectedCondition<List<WebElement>> presenceOfNestedElementsLocatedBy(
1324-
final By parent,
1325-
final By childLocator) {
1317+
final By parent, final By childLocator)
1318+
{
13261319
return new ExpectedCondition<List<WebElement>>() {
13271320

13281321
@Override
@@ -1345,8 +1338,7 @@ public String toString() {
13451338
* @param elements used to check their invisibility
13461339
* @return Boolean true when all elements are not visible anymore
13471340
*/
1348-
public static ExpectedCondition<Boolean> invisibilityOfAllElements(
1349-
final WebElement... elements) {
1341+
public static ExpectedCondition<Boolean> invisibilityOfAllElements(final WebElement... elements) {
13501342
return invisibilityOfAllElements(Arrays.asList(elements));
13511343
}
13521344

@@ -1356,8 +1348,7 @@ public static ExpectedCondition<Boolean> invisibilityOfAllElements(
13561348
* @param elements used to check their invisibility
13571349
* @return Boolean true when all elements are not visible anymore
13581350
*/
1359-
public static ExpectedCondition<Boolean> invisibilityOfAllElements(
1360-
final List<WebElement> elements) {
1351+
public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements) {
13611352
return new ExpectedCondition<Boolean>() {
13621353

13631354
@Override
@@ -1446,7 +1437,6 @@ public String toString() {
14461437
};
14471438
}
14481439

1449-
14501440
/**
14511441
* An expectation with the logical and condition of the given list of conditions.
14521442
*
@@ -1484,7 +1474,6 @@ public String toString() {
14841474
};
14851475
}
14861476

1487-
14881477
/**
14891478
* An expectation to check if js executable.
14901479
*

0 commit comments

Comments
 (0)