2525import org .json .JSONObject ;
2626import org .openqa .selenium .*;
2727import org .openqa .selenium .support .FindBy ;
28+ import org .openqa .selenium .support .ui .ExpectedCondition ;
2829import org .openqa .selenium .support .ui .Select ;
30+ import org .openqa .selenium .support .ui .Wait ;
31+ import org .openqa .selenium .support .ui .WebDriverWait ;
2932
3033import java .util .concurrent .Callable ;
3134
3235import static java .util .concurrent .TimeUnit .SECONDS ;
3336import static org .junit .Assert .assertNotNull ;
34- import static org .openqa .selenium .WaitingConditions .alertToBePresent ;
3537import static org .openqa .selenium .WaitingConditions .elementTextToContain ;
3638import static org .openqa .selenium .qtwebkit .visualizer_tests .WaitingConditions .elementToBeDisplayed ;
3739import static org .openqa .selenium .qtwebkit .visualizer_tests .WaitingConditions .pageUrlToBe ;
40+ import static org .openqa .selenium .support .ui .ExpectedConditions .alertIsPresent ;
3841
3942public class QtWebDriverJsPage {
4043 private static final long TIME_OUT = 20 ;
@@ -43,6 +46,9 @@ public class QtWebDriverJsPage {
4346 private WebDriver targetDriver ;
4447 private String webDriverJsWindowHandle ;
4548
49+ protected Wait <WebDriver > wait ;
50+ protected Wait <WebDriver > targetWait ;
51+
4652 private WebElement webDriverUrlPort ;
4753 private WebElement webPage ;
4854 private String webPageValue ;
@@ -101,12 +107,8 @@ public class QtWebDriverJsPage {
101107 @ FindBy (xpath = "//input[@value = 'Set window size']" )
102108 private WebElement windowSizeButton ;
103109
104- private static <X > X waitFor (Callable <X > until ) {
105- return TestWaiter .waitFor (until , TIME_OUT , SECONDS );
106- }
107-
108110 private Point getLocationFromAlert (String caption ) {
109- waitFor ( alertToBePresent ( driver ));
111+ wait . until ( alertIsPresent ( ));
110112 String value = driver .switchTo ().alert ().getText ().replace (caption , "" ).trim ();
111113 driver .switchTo ().alert ().accept ();
112114 try {
@@ -118,7 +120,7 @@ private Point getLocationFromAlert(String caption) {
118120 }
119121
120122 private Dimension getDimensionFromAlert (String caption ) {
121- waitFor ( alertToBePresent ( driver ));
123+ wait . until ( alertIsPresent ( ));
122124 String value = driver .switchTo ().alert ().getText ().replace (caption , "" ).trim ();
123125 driver .switchTo ().alert ().accept ();
124126 try {
@@ -130,14 +132,14 @@ private Dimension getDimensionFromAlert(String caption) {
130132 }
131133
132134 private String getStringFromAlert (String caption ) {
133- waitFor ( alertToBePresent ( driver ));
135+ wait . until ( alertIsPresent ( ));
134136 String value = driver .switchTo ().alert ().getText ().replace (caption , "" ).trim ();
135137 driver .switchTo ().alert ().accept ();
136138 return value ;
137139 }
138140
139141 private boolean getBooleanFromAlert (String caption ) {
140- waitFor ( alertToBePresent ( driver ));
142+ wait . until ( alertIsPresent ( ));
141143 String value = driver .switchTo ().alert ().getText ().replace (caption , "" ).trim ();
142144 driver .switchTo ().alert ().accept ();
143145 return Boolean .valueOf (value );
@@ -159,10 +161,12 @@ private String getVisualizerWindowHandle() {
159161
160162 public void setDriver (WebDriver driver ) {
161163 this .driver = driver ;
164+ wait = new WebDriverWait (driver , TIME_OUT );
162165 }
163166
164167 public void setTargetDriver (WebDriver targetDriver ) {
165168 this .targetDriver = targetDriver ;
169+ targetWait = new WebDriverWait (targetDriver , TIME_OUT );
166170 }
167171
168172 public String getWebDriverJsWindowHandle () {
@@ -175,10 +179,10 @@ public String getWebDriverUrlPort() {
175179
176180 public void setWebDriverUrl (String url ) {
177181 driver .get (url + "/WebDriverJsDemo.html" );
178- waitFor (new Callable <Object >() {
182+ wait . until (new ExpectedCondition <Object >() {
179183 @ Override
180- public Object call () throws Exception {
181- return ((JavascriptExecutor )driver ).executeScript ("return window.wd;" );
184+ public Object apply ( WebDriver driver ) {
185+ return ((JavascriptExecutor ) driver ).executeScript ("return window.wd;" );
182186 }
183187
184188 @ Override
@@ -203,12 +207,12 @@ public void setWebPage(String webPage) {
203207 webPageValue = webPage ;
204208 }
205209
206- public Callable <String > webPageIs (final String expectedWebPage ) {
207- return new Callable <String >() {
210+ public ExpectedCondition <String > webPageIs (final String expectedWebPage ) {
211+ return new ExpectedCondition <String >() {
208212 private String actualWebPage ;
209213
210214 @ Override
211- public String call () throws Exception {
215+ public String apply ( WebDriver driver ) {
212216 actualWebPage = getWebPage ();
213217
214218 if (expectedWebPage .equals (actualWebPage )) {
@@ -228,7 +232,7 @@ public String toString() {
228232
229233 public void clickGet () {
230234 getButton .click ();
231- waitFor (pageUrlToBe (targetDriver , webPageValue ));
235+ targetWait . until (pageUrlToBe (webPageValue ));
232236 }
233237
234238 public String clickSource () {
@@ -239,9 +243,9 @@ public String clickSource() {
239243
240244 driver .switchTo ().window (webDriverJsWindowHandle );
241245 sourceButton .click ();
242- visualizerWindow = waitFor (new Callable <String >() {
246+ visualizerWindow = wait . until (new ExpectedCondition <String >() {
243247 @ Override
244- public String call () throws Exception {
248+ public String apply ( WebDriver driver ) {
245249 return getVisualizerWindowHandle ();
246250 }
247251 });
@@ -262,13 +266,13 @@ public String findElement(String criteria, String key) {
262266 findElementKey .clear ();
263267 findElementKey .sendKeys ("c7c3179a38f864a463729657f15871326baccede" );
264268 findElementButton .click ();
265- waitFor (elementTextToContain (error , "The element could not be found" ));
269+ wait . until (elementTextToContain (error , "The element could not be found" ));
266270
267271 new Select (findElementCriteria ).selectByValue (criteria );
268272 findElementKey .clear ();
269273 findElementKey .sendKeys (key );
270274 findElementButton .click ();
271- waitFor (elementTextToContain (foundElement , "Found element" ));
275+ wait . until (elementTextToContain (foundElement , "Found element" ));
272276
273277 return foundElement .getText ().substring ("Found element" .length ()).trim ();
274278 }
@@ -324,9 +328,9 @@ public void keyPress(String label) {
324328 driver .findElement (By .xpath (getKeyXPath (label ))).click ();
325329
326330 if (label .equals ("Shift" )) {
327- waitFor (new Callable <WebElement >() {
331+ wait . until (new ExpectedCondition <WebElement >() {
328332 @ Override
329- public WebElement call () throws Exception {
333+ public WebElement apply ( WebDriver driver ) {
330334 return driver .findElement (By .xpath (getKeyXPath ("A" )));
331335 }
332336 });
@@ -344,7 +348,7 @@ private String getKeyXPath(String label) {
344348
345349 public void clickListWindowHandles () {
346350 listWindowButton .click ();
347- waitFor (elementToBeDisplayed (windowList ));
351+ wait . until (elementToBeDisplayed (windowList ));
348352 }
349353
350354 public Select getWindowListSelect () {
0 commit comments