|
31 | 31 | import java.util.List;
|
32 | 32 | import java.util.logging.Level;
|
33 | 33 | import java.util.logging.Logger;
|
| 34 | +import java.util.regex.Matcher; |
| 35 | +import java.util.regex.Pattern; |
34 | 36 |
|
35 | 37 | /**
|
36 | 38 | * Canned {@link ExpectedCondition}s which are generally useful within webdriver
|
@@ -91,6 +93,79 @@ public String toString() {
|
91 | 93 | };
|
92 | 94 | }
|
93 | 95 |
|
| 96 | + /** |
| 97 | + * An expectation for the URL of the current page to be a specific url. |
| 98 | + * |
| 99 | + * @param url the url that the page should be on |
| 100 | + * @return <code>true</code> when the URL is what it should be |
| 101 | + */ |
| 102 | + public static ExpectedCondition<Boolean> urlToBe(final String url) { |
| 103 | + return new ExpectedCondition<Boolean>() { |
| 104 | + private String currentUrl = ""; |
| 105 | + |
| 106 | + @Override |
| 107 | + public Boolean apply(WebDriver driver) { |
| 108 | + currentUrl = driver.getCurrentUrl(); |
| 109 | + return currentUrl != null && currentUrl.equals(url); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return String.format("url to be \"%s\". Current url: \"%s\"", url, currentUrl); |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * An expectation for the URL of the current page to contain specific text. |
| 121 | + * |
| 122 | + * @param fraction the fraction of the url that the page should be on |
| 123 | + * @return <code>true</code> when the URL contains the text |
| 124 | + */ |
| 125 | + public static ExpectedCondition<Boolean> urlContains(final String fraction) { |
| 126 | + return new ExpectedCondition<Boolean>() { |
| 127 | + private String currentUrl = ""; |
| 128 | + |
| 129 | + @Override |
| 130 | + public Boolean apply(WebDriver driver) { |
| 131 | + currentUrl = driver.getCurrentUrl(); |
| 132 | + return currentUrl != null && currentUrl.contains(fraction); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public String toString() { |
| 137 | + return String.format("url to contain \"%s\". Current url: \"%s\"", fraction, currentUrl); |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Expectation for the URL to match a specific regular expression |
| 144 | + * |
| 145 | + * @param regex the regular expression that the URL should match |
| 146 | + * @return <code>true</code> if the URL matches the specified regular expression |
| 147 | + */ |
| 148 | + public static ExpectedCondition<Boolean> urlMatches(final String regex) { |
| 149 | + return new ExpectedCondition<Boolean>() { |
| 150 | + private String currentUrl; |
| 151 | + private Pattern pattern; |
| 152 | + private Matcher matcher; |
| 153 | + |
| 154 | + @Override |
| 155 | + public Boolean apply(WebDriver driver) { |
| 156 | + currentUrl = driver.getCurrentUrl(); |
| 157 | + pattern = Pattern.compile(regex); |
| 158 | + matcher = pattern.matcher(currentUrl); |
| 159 | + return matcher.find(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String toString() { |
| 164 | + return String.format("url to match the regex \"%s\". Current url: \"%s\"", regex, currentUrl); |
| 165 | + } |
| 166 | + }; |
| 167 | + } |
| 168 | + |
94 | 169 | /**
|
95 | 170 | * An expectation for checking that an element is present on the DOM of a
|
96 | 171 | * page. This does not necessarily mean that the element is visible.
|
|
0 commit comments