@@ -37,18 +37,39 @@ public void replaceAlertMethod(WebDriver driver) {
37
37
}
38
38
39
39
((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 = []; " +
48
64
" 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
+ );
52
73
}
53
74
54
75
private void checkOverridesEnabled (){
@@ -59,11 +80,21 @@ private void checkOverridesEnabled(){
59
80
public String getNextAlert (WebDriver driver ) {
60
81
checkOverridesEnabled ();
61
82
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
+ );
67
98
68
99
if (result == null ) {
69
100
throw new SeleniumException ("There were no alerts" );
@@ -75,16 +106,32 @@ public String getNextAlert(WebDriver driver) {
75
106
public boolean isAlertPresent (WebDriver driver ) {
76
107
checkOverridesEnabled ();
77
108
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
+ ));
80
117
}
81
118
82
119
public String getNextConfirmation (WebDriver driver ) {
83
120
checkOverridesEnabled ();
84
121
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
+ );
88
135
89
136
if (result == null ) {
90
137
throw new SeleniumException ("There were no confirmations" );
@@ -96,7 +143,13 @@ public String getNextConfirmation(WebDriver driver) {
96
143
public boolean isConfirmationPresent (WebDriver driver ) {
97
144
checkOverridesEnabled ();
98
145
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
+ ));
101
154
}
102
155
}
0 commit comments