Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/JavascriptEnabledDriverTest.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static org.assertj.core.api.Assertions.assertThat;
21
import static org.junit.jupiter.api.Assumptions.assumeTrue;
22
import static org.openqa.selenium.WaitingConditions.elementTextToEqual;
23
import static org.openqa.selenium.WaitingConditions.elementValueToEqual;
24
import static org.openqa.selenium.WaitingConditions.windowToBeSwitchedToWithName;
25
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
26
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
27
28
import org.junit.jupiter.api.Test;
29
import org.openqa.selenium.interactions.Locatable;
30
import org.openqa.selenium.support.ui.ExpectedConditions;
31
import org.openqa.selenium.testing.JupiterTestBase;
32
import org.openqa.selenium.testing.NoDriverAfterTest;
33
import org.openqa.selenium.testing.NotYetImplemented;
34
35
class JavascriptEnabledDriverTest extends JupiterTestBase {
36
37
@Test
38
void testDocumentShouldReflectLatestTitle() {
39
driver.get(pages.javascriptPage);
40
41
assertThat(driver.getTitle()).isEqualTo("Testing Javascript");
42
driver.findElement(By.linkText("Change the page title!")).click();
43
waitForTitleChange("Changed");
44
assertThat(driver.getTitle()).isEqualTo("Changed");
45
}
46
47
@Test
48
@NotYetImplemented(SAFARI)
49
public void testDocumentShouldReflectLatestDom() {
50
driver.get(pages.javascriptPage);
51
String currentText = driver.findElement(By.xpath("//div[@id='dynamo']")).getText();
52
assertThat(currentText).isEqualTo("What's for dinner?");
53
54
WebElement webElement = driver.findElement(By.linkText("Update a div"));
55
webElement.click();
56
57
WebElement dynamo = driver.findElement(By.xpath("//div[@id='dynamo']"));
58
59
wait.until(elementTextToEqual(dynamo, "Fish and chips!"));
60
assertThat(dynamo.getText()).isEqualTo("Fish and chips!");
61
}
62
63
@Test
64
void testShouldWaitForLoadsToCompleteAfterJavascriptCausesANewPageToLoad() {
65
driver.get(pages.formPage);
66
67
driver.findElement(By.id("changeme")).click();
68
69
waitForTitleChange("Page3");
70
assertThat(driver.getTitle()).isEqualTo("Page3");
71
}
72
73
@Test
74
void testShouldBeAbleToFindElementAfterJavascriptCausesANewPageToLoad() {
75
driver.get(pages.formPage);
76
77
driver.findElement(By.id("changeme")).click();
78
79
waitForTitleChange("Page3");
80
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("3");
81
}
82
83
@Test
84
@NotYetImplemented(value = SAFARI, reason = "getText does not normalize spaces")
85
public void testShouldFireOnChangeEventWhenSettingAnElementsValue() {
86
driver.get(pages.javascriptPage);
87
driver.findElement(By.id("change")).sendKeys("foo");
88
String result = driver.findElement(By.id("result")).getText();
89
90
assertThat(result).isEqualTo("change");
91
}
92
93
@Test
94
@NotYetImplemented(SAFARI)
95
public void testShouldBeAbleToSubmitFormsByCausingTheOnClickEventToFire() {
96
driver.get(pages.javascriptPage);
97
WebElement element = driver.findElement(By.id("jsSubmitButton"));
98
element.click();
99
100
waitForTitleChange("We Arrive Here");
101
102
assertThat(driver.getTitle()).isEqualTo("We Arrive Here");
103
}
104
105
private void waitForTitleChange(String newTitle) {
106
wait.until(titleIs(newTitle));
107
}
108
109
@Test
110
@NotYetImplemented(SAFARI)
111
public void testShouldBeAbleToClickOnSubmitButtons() {
112
driver.get(pages.javascriptPage);
113
WebElement element = driver.findElement(By.id("submittingButton"));
114
element.click();
115
116
waitForTitleChange("We Arrive Here");
117
118
assertThat(driver.getTitle()).isEqualTo("We Arrive Here");
119
}
120
121
@Test
122
void testIssue80ClickShouldGenerateClickEvent() {
123
driver.get(pages.javascriptPage);
124
WebElement element = driver.findElement(By.id("clickField"));
125
assertThat(element.getAttribute("value")).isEqualTo("Hello");
126
127
element.click();
128
129
String elementValue = wait.until(elementValueToEqual(element, "Clicked"));
130
131
assertThat(elementValue).isEqualTo("Clicked");
132
}
133
134
@Test
135
@NotYetImplemented(SAFARI)
136
public void testShouldBeAbleToSwitchToFocusedElement() {
137
driver.get(pages.javascriptPage);
138
139
driver.findElement(By.id("switchFocus")).click();
140
141
WebElement element = driver.switchTo().activeElement();
142
assertThat(element.getAttribute("id")).isEqualTo("theworks");
143
}
144
145
@Test
146
void testIfNoElementHasFocusTheActiveElementIsTheBody() {
147
driver.get(pages.simpleTestPage);
148
149
WebElement element = driver.switchTo().activeElement();
150
151
assertThat(element.getAttribute("name")).isEqualTo("body");
152
}
153
154
@Test
155
@NotYetImplemented(value = SAFARI)
156
public void testChangeEventIsFiredAppropriatelyWhenFocusIsLost() {
157
driver.get(pages.javascriptPage);
158
159
WebElement input = driver.findElement(By.id("changeable"));
160
input.sendKeys("test");
161
moveFocus();
162
assertThat(driver.findElement(By.id("result")).getText().trim())
163
.isIn("focus change blur", "focus blur change");
164
165
input.sendKeys(Keys.BACK_SPACE, "t");
166
moveFocus();
167
168
// I weep.
169
assertThat(driver.findElement(By.id("result")).getText().trim())
170
.isIn(
171
"focus change blur focus blur", "focus blur change focus blur",
172
"focus blur change focus blur change", "focus change blur focus change blur");
173
}
174
175
/** If the click handler throws an exception, the firefox driver freezes. This is suboptimal. */
176
@Test
177
void testShouldBeAbleToClickIfEvenSomethingHorribleHappens() {
178
driver.get(pages.javascriptPage);
179
180
driver.findElement(By.id("error")).click();
181
182
// If we get this far then the test has passed, but let's do something basic to prove the point
183
String text = driver.findElement(By.id("error")).getText();
184
assertThat(text).isNotNull();
185
}
186
187
@Test
188
void testShouldBeAbleToGetTheLocationOfAnElement() {
189
assumeTrue(driver instanceof JavascriptExecutor);
190
191
driver.get(pages.javascriptPage);
192
193
((JavascriptExecutor) driver).executeScript("window.focus();");
194
WebElement element = driver.findElement(By.id("keyUp"));
195
196
assumeTrue(element instanceof Locatable);
197
198
Point point = ((Locatable) element).getCoordinates().inViewPort();
199
200
assertThat(point.getX()).as("X coordinate").isGreaterThan(1);
201
// Element's Y coordinates can be 0, as the element is scrolled right to the top of the window.
202
assertThat(point.getY()).as("Y coordinate").isGreaterThanOrEqualTo(0);
203
}
204
205
/*
206
* There's a weird issue with this test, which means that I've added the needs fresh driver
207
* annotation. To see it in action, try running the single test suite with only these tests
208
* running: "ImplicitWaitTest", "TemporaryFilesystemTest", "JavascriptEnabledDriverTest".
209
* SimonStewart 2010-10-04
210
*/
211
@NoDriverAfterTest
212
@Test
213
@NotYetImplemented(SAFARI)
214
public void testShouldBeAbleToClickALinkThatClosesAWindow() {
215
driver.get(pages.javascriptPage);
216
217
String handle = driver.getWindowHandle();
218
driver.findElement(By.id("new_window")).click();
219
220
// Depending on the Android emulator platform this can take a while.
221
wait.until(windowToBeSwitchedToWithName("close_me"));
222
223
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("close")));
224
driver.findElement(By.id("close")).click();
225
226
driver.switchTo().window(handle);
227
228
// If we haven't seen an exception or hung the test has passed
229
}
230
231
private void moveFocus() {
232
driver.findElement(By.id("clickField")).click();
233
}
234
}
235
236