View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.qa.glnm.interaction;
21  
22  import static io.wcm.qa.glnm.webdriver.WebDriverManagement.getWait;
23  
24  import java.util.function.Function;
25  
26  import org.openqa.selenium.JavascriptExecutor;
27  import org.openqa.selenium.WebDriver;
28  import org.openqa.selenium.support.ui.ExpectedConditions;
29  import org.openqa.selenium.support.ui.WebDriverWait;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Wraps WebDriverWait functionalities.
35   *
36   * @since 1.0.0
37   */
38  public final class Wait {
39  
40    private static final int DEFAULT_TIMEOUT = 1;
41  
42    private static final Logger LOG = LoggerFactory.getLogger(Wait.class);
43  
44    private Wait() {
45      // do not instantiate
46    }
47  
48    /**
49     * Wait for domReady for a maximum of one second.
50     *
51     * @since 1.0.0
52     */
53    public static void forDomReady() {
54      forDomReady(DEFAULT_TIMEOUT);
55    }
56  
57    /**
58     * Wait for "DOM ready" for a maximum of seconds specified by parameter.
59     *
60     * @param timeOutInSeconds how long to wait for "DOM ready"
61     * @since 1.0.0
62     */
63    public static void forDomReady(int timeOutInSeconds) {
64      WebDriverWait wait = getWait(timeOutInSeconds);
65      wait.until(driver -> ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"));
66    }
67  
68    /**
69     * Load URL and wait for it to be loaded.
70     *
71     * @param url to load
72     * @since 1.0.0
73     */
74    public static void forUrl(String url) {
75      forUrl(url, DEFAULT_TIMEOUT);
76    }
77  
78    /**
79     * Load URL and wait passed number of seconds for it to be loaded.
80     *
81     * @param url to load
82     * @param timeOutInSeconds how long to wait for URL to be current
83     * @since 1.0.0
84     */
85    public static void forUrl(String url, int timeOutInSeconds) {
86      if (LOG.isTraceEnabled()) {
87        LOG.trace("waiting for URL: '" + url + "'");
88      }
89      WebDriverWait wait = getWait(timeOutInSeconds);
90      wait.until((Function<? super WebDriver, Boolean>)ExpectedConditions.urlToBe(url));
91      if (LOG.isTraceEnabled()) {
92        LOG.trace("found URL: '" + url + "'");
93      }
94    }
95  
96  }