Skip to content

Commit 80ccbb8

Browse files
committed
Implementing alert and confirmation handling in WDBS (Java)
1 parent b3e61c4 commit 80ccbb8

File tree

3 files changed

+96
-32
lines changed

3 files changed

+96
-32
lines changed

Diff for: java/client/src/org/openqa/selenium/internal/seleniumemulation/AlertOverride.java

+76-23
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,39 @@ public void replaceAlertMethod(WebDriver driver) {
3737
}
3838

3939
((JavascriptExecutor) driver).executeScript(
40-
"if (window.__webdriverAlerts) { return; } " +
41-
"window.__webdriverAlerts = []; " +
42-
"window.alert = function(msg) { window.__webdriverAlerts.push(msg); }; " +
43-
"window.__webdriverConfirms = []; " +
44-
"window.__webdriverNextConfirm = true; " +
45-
"window.confirm = function(msg) { " +
46-
" window.__webdriverConfirms.push(msg); " +
47-
" var res = window.__webdriverNextConfirm; " +
40+
"if (window.localStorage) { " +
41+
" window.localStorage.setItem('__webdriverAlerts', JSON.stringify([])); " +
42+
" window.alert = function(msg) { " +
43+
" var alerts = JSON.parse(window.localStorage.getItem('__webdriverAlerts')); " +
44+
" alerts.push(msg); " +
45+
" window.localStorage.setItem('__webdriverAlerts', JSON.stringify(alerts)); " +
46+
" }; " +
47+
" window.localStorage.setItem('__webdriverConfirms', JSON.stringify([])); " +
48+
" if (!('__webdriverNextConfirm' in window.localStorage)) { " +
49+
" window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(true)); " +
50+
" } " +
51+
" window.confirm = function(msg) { " +
52+
" var confirms = JSON.parse(window.localStorage.getItem('__webdriverConfirms')); " +
53+
" confirms.push(msg); " +
54+
" window.localStorage.setItem('__webdriverConfirms', JSON.stringify(confirms)); " +
55+
" var res = JSON.parse(window.localStorage.getItem('__webdriverNextConfirm')); " +
56+
" window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(true)); " +
57+
" return res; " +
58+
" }; " +
59+
"} else { " +
60+
" if (window.__webdriverAlerts) { return; } " +
61+
" window.__webdriverAlerts = []; " +
62+
" window.alert = function(msg) { window.__webdriverAlerts.push(msg); }; " +
63+
" window.__webdriverConfirms = []; " +
4864
" window.__webdriverNextConfirm = true; " +
49-
" return res; " +
50-
"};"
51-
);
65+
" window.confirm = function(msg) { " +
66+
" window.__webdriverConfirms.push(msg); " +
67+
" var res = window.__webdriverNextConfirm; " +
68+
" window.__webdriverNextConfirm = true; " +
69+
" return res; " +
70+
" }; " +
71+
"}"
72+
);
5273
}
5374

5475
private void checkOverridesEnabled(){
@@ -59,11 +80,21 @@ private void checkOverridesEnabled(){
5980
public String getNextAlert(WebDriver driver) {
6081
checkOverridesEnabled();
6182
String result = (String) ((JavascriptExecutor) driver).executeScript(
62-
"if (!window.__webdriverAlerts) { return null }; " +
63-
"var t = window.__webdriverAlerts.shift();" +
64-
"if (t) { t = t.replace(/\\n/g, ' '); } " +
65-
"return t;"
66-
);
83+
"if (window.localStorage) { " +
84+
" if (!('__webdriverAlerts' in window.localStorage)) { return null } " +
85+
" var alerts = JSON.parse(window.localStorage.getItem('__webdriverAlerts')); " +
86+
" if (! alerts) { return null } " +
87+
" var t = alerts.shift(); " +
88+
" window.localStorage.setItem('__webdriverAlerts', JSON.stringify(alerts)); " +
89+
" if (t) { t = t.replace(/\\n/g, ' '); } " +
90+
" return t; " +
91+
"} else { " +
92+
" if (!window.__webdriverAlerts) { return null } " +
93+
" var t = window.__webdriverAlerts.shift(); " +
94+
" if (t) { t = t.replace(/\\n/g, ' '); } " +
95+
" return t; " +
96+
"}"
97+
);
6798

6899
if (result == null) {
69100
throw new SeleniumException("There were no alerts");
@@ -75,16 +106,32 @@ public String getNextAlert(WebDriver driver) {
75106
public boolean isAlertPresent(WebDriver driver) {
76107
checkOverridesEnabled();
77108
return Boolean.TRUE.equals(((JavascriptExecutor) driver).executeScript(
78-
"return window.__webdriverAlerts && window.__webdriverAlerts.length > 0;"
79-
));
109+
"if (window.localStorage) { " +
110+
" if (!('__webdriverAlerts' in window.localStorage)) { return false } " +
111+
" var alerts = JSON.parse(window.localStorage.getItem('__webdriverAlerts')); " +
112+
" return alerts && alerts.length > 0; " +
113+
"} else { " +
114+
" return window.__webdriverAlerts && window.__webdriverAlerts.length > 0; " +
115+
"}"
116+
));
80117
}
81118

82119
public String getNextConfirmation(WebDriver driver) {
83120
checkOverridesEnabled();
84121
String result = (String) ((JavascriptExecutor) driver).executeScript(
85-
"if (!window.__webdriverConfirms) { return null; } " +
86-
"return window.__webdriverConfirms.shift();"
87-
);
122+
"if (window.localStorage) { " +
123+
" if (!('__webdriverConfirms' in window.localStorage)) { return null } " +
124+
" var confirms = JSON.parse(window.localStorage.getItem('__webdriverConfirms')); " +
125+
" if (! confirms) { return null } " +
126+
" var t = confirms.shift(); " +
127+
" window.localStorage.setItem('__webdriverConfirms', JSON.stringify(confirms)); " +
128+
" if (t) { t = t.replace(/\\n/g, ' '); } " +
129+
" return t; " +
130+
"} else { " +
131+
" if (!window.__webdriverConfirms) { return null; } " +
132+
" return window.__webdriverConfirms.shift(); " +
133+
"}"
134+
);
88135

89136
if (result == null) {
90137
throw new SeleniumException("There were no confirmations");
@@ -96,7 +143,13 @@ public String getNextConfirmation(WebDriver driver) {
96143
public boolean isConfirmationPresent(WebDriver driver) {
97144
checkOverridesEnabled();
98145
return Boolean.TRUE.equals(((JavascriptExecutor) driver).executeScript(
99-
"return window.__webdriverConfirms && window.__webdriverConfirms.length > 0;"
100-
));
146+
"if (window.localStorage) { " +
147+
" if (!('__webdriverConfirms' in window.localStorage)) { return false } " +
148+
" var confirms = JSON.parse(window.localStorage.getItem('__webdriverConfirms')); " +
149+
" return confirms && confirms.length > 0; " +
150+
"} else { " +
151+
" return window.__webdriverConfirms && window.__webdriverConfirms.length > 0; " +
152+
"}"
153+
));
101154
}
102155
}

Diff for: java/client/src/org/openqa/selenium/internal/seleniumemulation/SetNextConfirmationState.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public SetNextConfirmationState(boolean result) {
2929
@Override
3030
protected Void handleSeleneseCommand(WebDriver driver, String locator, String value) {
3131
((JavascriptExecutor) driver).executeScript(
32-
"window.__webdriverNextConfirm = arguments[0]", result);
32+
"if (window.localStorage) { " +
33+
" window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(arguments[0])); " +
34+
"} else { " +
35+
" window.__webdriverNextConfirm = arguments[0];" +
36+
"}"
37+
, result);
3338
return null;
3439
}
3540
}

Diff for: java/client/test/com/thoughtworks/selenium/WebDriverSeleniumTestSuite.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
import com.thoughtworks.selenium.corebased.SeleniumMouseTest;
2323
import com.thoughtworks.selenium.corebased.TestAddLocationStrategy;
2424
import com.thoughtworks.selenium.corebased.TestAddSelection;
25+
import com.thoughtworks.selenium.corebased.TestAlerts;
2526
import com.thoughtworks.selenium.corebased.TestBasicAuth;
2627
import com.thoughtworks.selenium.corebased.TestBrowserVersion;
2728
import com.thoughtworks.selenium.corebased.TestCheckUncheck;
2829
import com.thoughtworks.selenium.corebased.TestClick;
2930
import com.thoughtworks.selenium.corebased.TestClickAt;
3031
import com.thoughtworks.selenium.corebased.TestClickJavascriptHref;
32+
import com.thoughtworks.selenium.corebased.TestClickJavascriptHrefChrome;
3133
import com.thoughtworks.selenium.corebased.TestCommandError;
3234
import com.thoughtworks.selenium.corebased.TestComments;
35+
import com.thoughtworks.selenium.corebased.TestConfirmations;
3336
import com.thoughtworks.selenium.corebased.TestCssLocators;
3437
import com.thoughtworks.selenium.corebased.TestDojoDragAndDrop;
3538
import com.thoughtworks.selenium.corebased.TestEditable;
@@ -41,7 +44,9 @@
4144
import com.thoughtworks.selenium.corebased.TestEvilClosingWindow;
4245
import com.thoughtworks.selenium.corebased.TestFailingAssert;
4346
import com.thoughtworks.selenium.corebased.TestFailingVerifications;
47+
import com.thoughtworks.selenium.corebased.TestFocusOnBlur;
4448
import com.thoughtworks.selenium.corebased.TestFramesClick;
49+
import com.thoughtworks.selenium.corebased.TestFramesClickJavascriptHref;
4550
import com.thoughtworks.selenium.corebased.TestFramesOpen;
4651
import com.thoughtworks.selenium.corebased.TestFramesSpecialTargets;
4752
import com.thoughtworks.selenium.corebased.TestFunkEventHandling;
@@ -53,6 +58,7 @@
5358
import com.thoughtworks.selenium.corebased.TestHighlight;
5459
import com.thoughtworks.selenium.corebased.TestHtmlSource;
5560
import com.thoughtworks.selenium.corebased.TestImplicitLocators;
61+
import com.thoughtworks.selenium.corebased.TestJavaScriptAttributes;
5662
import com.thoughtworks.selenium.corebased.TestLocators;
5763
import com.thoughtworks.selenium.corebased.TestMultiSelect;
5864
import com.thoughtworks.selenium.corebased.TestOpen;
@@ -84,19 +90,19 @@
8490
SeleniumMouseTest.class,
8591
TestAddLocationStrategy.class,
8692
TestAddSelection.class,
87-
// TestAlerts.class, // alerts
93+
TestAlerts.class,
8894
TestBasicAuth.class,
8995
TestBrowserVersion.class,
9096
TestCheckUncheck.class,
9197
TestClick.class,
9298
TestClickAt.class,
9399
// TestClickBlankTarget.class,
94-
TestClickJavascriptHref.class, // fails in IE
95-
// TestClickJavascriptHrefChrome.class, // alerts
100+
TestClickJavascriptHref.class,
101+
TestClickJavascriptHrefChrome.class, // alerts
96102
// TestClickJavascriptHrefWithVoidChrome.class, // fails in IE
97103
TestCommandError.class,
98104
TestComments.class,
99-
// TestConfirmations.class, // alerts
105+
TestConfirmations.class, // fails in IE
100106
// TestCookie.class,
101107
TestCssLocators.class,
102108
// TestCursorPosition.class,
@@ -112,9 +118,9 @@
112118
TestEvilClosingWindow.class,
113119
TestFailingAssert.class,
114120
TestFailingVerifications.class,
115-
// TestFocusOnBlur.class, // alerts
121+
TestFocusOnBlur.class,
116122
TestFramesClick.class,
117-
// TestFramesClickJavascriptHref.class, // alerts
123+
TestFramesClickJavascriptHref.class,
118124
// TestFramesNested.class,
119125
TestFramesOpen.class,
120126
TestFramesSpecialTargets.class,
@@ -127,7 +133,7 @@
127133
TestHighlight.class,
128134
TestHtmlSource.class,
129135
TestImplicitLocators.class,
130-
// TestJavaScriptAttributes.class, // alerts
136+
TestJavaScriptAttributes.class,
131137
// TestJavascriptParameters.class,
132138
TestLocators.class,
133139
TestMultiSelect.class,
@@ -136,7 +142,7 @@
136142
TestOpenInTargetFrame.class,
137143
TestPatternMatching.class,
138144
TestPause.class,
139-
// TestPrompt.class, // alerts
145+
// TestPrompt.class,
140146
TestProxy.class,
141147
TestQuickOpen.class,
142148
// TestRefresh.class,

0 commit comments

Comments
 (0)