|
4 | 4 | import java.io.InputStream; |
5 | 5 | import java.net.URL; |
6 | 6 | import java.nio.charset.StandardCharsets; |
7 | | -import java.time.Duration; |
8 | | -import java.util.Objects; |
9 | 7 | import java.util.logging.Level; |
10 | 8 | import java.util.logging.Logger; |
11 | | -import org.jenkinsci.test.acceptance.junit.Wait; |
12 | | -import org.openqa.selenium.By; |
13 | 9 | import org.openqa.selenium.JavascriptExecutor; |
14 | 10 | import org.openqa.selenium.NoAlertPresentException; |
15 | | -import org.openqa.selenium.TimeoutException; |
16 | 11 | import org.openqa.selenium.UnhandledAlertException; |
17 | 12 | import org.openqa.selenium.WebDriver; |
18 | 13 | import org.openqa.selenium.WebDriver.Navigation; |
19 | | -import org.openqa.selenium.WebElement; |
20 | 14 | import org.openqa.selenium.support.events.WebDriverListener; |
21 | 15 |
|
22 | 16 | /** |
|
74 | 68 | * @author Kohsuke Kawaguchi |
75 | 69 | */ |
76 | 70 | public class Scroller implements WebDriverListener { |
77 | | - |
78 | 71 | private final Logger LOGGER = Logger.getLogger(Scroller.class.getName()); |
79 | 72 |
|
80 | | - private final String scrollJs; |
81 | 73 | private final String disableStickyElementsJs; |
82 | 74 | private final WebDriver driver; |
83 | 75 |
|
84 | 76 | public Scroller(WebDriver driver) { |
85 | 77 | this.driver = driver; |
86 | | - try (InputStream scroller = getClass().getResourceAsStream("scroller.js"); |
87 | | - InputStream sticky = getClass().getResourceAsStream("disable-sticky-elements.js")) { |
88 | | - scrollJs = new String(scroller.readAllBytes(), StandardCharsets.UTF_8); |
| 78 | + try (InputStream sticky = getClass().getResourceAsStream("disable-sticky-elements.js")) { |
89 | 79 | disableStickyElementsJs = new String(sticky.readAllBytes(), StandardCharsets.UTF_8); |
90 | 80 | } catch (IOException e) { |
91 | 81 | throw new Error("Failed to load the JavaScript file", e); |
92 | 82 | } |
93 | 83 | } |
94 | 84 |
|
95 | | - @Override |
96 | | - public void beforeClick(WebElement element) { |
97 | | - scrollIntoView(element); |
98 | | - } |
99 | | - |
100 | | - @Override |
101 | | - public void beforeSendKeys(WebElement element, CharSequence[] keysToSend) { |
102 | | - scrollIntoView(element); |
103 | | - } |
104 | | - |
105 | | - @Override |
106 | | - public void beforeClear(WebElement element) { |
107 | | - scrollIntoView(element); |
108 | | - } |
109 | | - |
110 | 85 | @Override |
111 | 86 | public void afterGet(WebDriver driver, String url) { |
112 | 87 | disableStickyElementsIgnoringDialogs(); |
@@ -150,34 +125,4 @@ public void disableStickyElements() { |
150 | 125 | unexpected); |
151 | 126 | } |
152 | 127 | } |
153 | | - |
154 | | - /** |
155 | | - * The framework is expected to take care of the correct scrolling. When you are tempted to scroll from PageObjects |
156 | | - * or tests, there is likely a framework problem to be fixed. |
157 | | - */ |
158 | | - public void scrollIntoView(WebElement e) { |
159 | | - WebElement element = e; |
160 | | - if (Objects.equals(element.getTagName(), "option")) { |
161 | | - element = e.findElement(By.xpath("..")); // scroll select into view not option |
162 | | - } |
163 | | - |
164 | | - final int eYCoord = element.getLocation().getY(); |
165 | | - final int eXCoord = element.getLocation().getX(); |
166 | | - final String id = element.getAttribute("id"); |
167 | | - final JavascriptExecutor executor = (JavascriptExecutor) driver; |
168 | | - // Wait until web element is successfully scrolled. |
169 | | - try { |
170 | | - new Wait<>(Boolean.TRUE) |
171 | | - .withTimeout(Duration.ofSeconds(5)) // Wall-clock time |
172 | | - .until(() -> (Boolean) executor.executeScript(scrollJs, eYCoord, eXCoord, id)); |
173 | | - } catch (TimeoutException ex) { |
174 | | - // Scrolling failed, but sometimes the element to click is already visible, let the test continue and |
175 | | - // eventually fail later |
176 | | - // This log message should be sufficient to diagnose the issue |
177 | | - LOGGER.log( |
178 | | - Level.WARNING, |
179 | | - "Scrolling failed, letting the test to continue anyways, but \"Element is not clickable\" error will likely be thrown", |
180 | | - ex); |
181 | | - } |
182 | | - } |
183 | 128 | } |
0 commit comments