@@ -77,8 +77,7 @@ boolean compareText(String expectedValue, String actualValue) {
7777 }
7878
7979 public static ExpectedCondition <String > elementTextToEqual (final By locator , final String value ) {
80- return new ExpectedCondition <String >() {
81-
80+ return new ExpectedCondition <>() {
8281 @ Override
8382 public String apply (WebDriver driver ) {
8483 String text = driver .findElement (locator ).getText ();
@@ -96,19 +95,46 @@ public String toString() {
9695 };
9796 }
9897
98+ public static ExpectedCondition <String > elementTextToContain (
99+ final By locator , final String expected ) {
100+ return new ExpectedCondition <>() {
101+ @ Override
102+ public String apply (WebDriver driver ) {
103+ String text = driver .findElement (locator ).getText ();
104+ return text .contains (expected ) ? text : null ;
105+ }
106+
107+ @ Override
108+ public String toString () {
109+ return String .format ("element text did not contain \" %s\" " , expected );
110+ }
111+ };
112+ }
113+
114+ public static ExpectedCondition <String > elementTextToMatch (final By locator , final String regex ) {
115+ return new ExpectedCondition <>() {
116+ @ Override
117+ public String apply (WebDriver driver ) {
118+ String text = driver .findElement (locator ).getText ();
119+ return text .matches (regex ) ? text : null ;
120+ }
121+
122+ @ Override
123+ public String toString () {
124+ return String .format ("element text did not match \" %s\" " , regex );
125+ }
126+ };
127+ }
128+
99129 public static ExpectedCondition <String > elementValueToEqual (
100130 final WebElement element , final String expectedValue ) {
101- return new ExpectedCondition <String >() {
102-
131+ return new ExpectedCondition <>() {
103132 private String lastValue = "" ;
104133
105134 @ Override
106135 public String apply (WebDriver ignored ) {
107136 lastValue = element .getAttribute ("value" );
108- if (expectedValue .equals (lastValue )) {
109- return lastValue ;
110- }
111- return null ;
137+ return expectedValue .equals (lastValue ) ? lastValue : null ;
112138 }
113139
114140 @ Override
@@ -119,15 +145,11 @@ public String toString() {
119145 }
120146
121147 public static ExpectedCondition <String > pageSourceToContain (final String expectedText ) {
122- return new ExpectedCondition <String >() {
148+ return new ExpectedCondition <>() {
123149 @ Override
124150 public String apply (WebDriver driver ) {
125151 String source = driver .getPageSource ();
126-
127- if (source .contains (expectedText )) {
128- return source ;
129- }
130- return null ;
152+ return source .contains (expectedText ) ? source : null ;
131153 }
132154
133155 @ Override
@@ -139,17 +161,13 @@ public String toString() {
139161
140162 public static ExpectedCondition <Point > elementLocationToBe (
141163 final WebElement element , final Point expectedLocation ) {
142- return new ExpectedCondition <Point >() {
164+ return new ExpectedCondition <>() {
143165 private Point currentLocation = new Point (0 , 0 );
144166
145167 @ Override
146168 public Point apply (WebDriver ignored ) {
147169 currentLocation = element .getLocation ();
148- if (currentLocation .equals (expectedLocation )) {
149- return expectedLocation ;
150- }
151-
152- return null ;
170+ return currentLocation .equals (expectedLocation ) ? expectedLocation : null ;
153171 }
154172
155173 @ Override
@@ -182,7 +200,7 @@ public static ExpectedCondition<String> newWindowIsOpened(final Set<String> orig
182200 }
183201
184202 public static ExpectedCondition <WebDriver > windowToBeSwitchedToWithName (final String windowName ) {
185- return new ExpectedCondition <WebDriver >() {
203+ return new ExpectedCondition <>() {
186204
187205 @ Override
188206 public WebDriver apply (WebDriver driver ) {
0 commit comments