Skip to content

Commit 4203f10

Browse files
committed
Implementing pageLoadingStrategy capability in Firefox
1 parent afe029b commit 4203f10

File tree

8 files changed

+77
-55
lines changed

8 files changed

+77
-55
lines changed

.idea/libraries/phantomjsdriver.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/client/src/org/openqa/selenium/remote/CapabilityType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public interface CapabilityType {
4545

4646
String ENABLE_PROFILING_CAPABILITY = "webdriver.logging.profiler.enabled";
4747

48+
String PAGE_LOADING_STRATEGY = "pageLoadingStrategy";
49+
4850
/**
4951
* Moved InternetExplorer specific CapabilityTypes into InternetExplorerDriver.java for consistency
5052
*/

java/client/test/org/openqa/selenium/PageLoadingTest.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.After;
2121
import org.junit.Test;
2222
import org.openqa.selenium.environment.GlobalTestEnvironment;
23+
import org.openqa.selenium.remote.CapabilityType;
2324
import org.openqa.selenium.remote.DesiredCapabilities;
2425
import org.openqa.selenium.support.ui.WebDriverWait;
2526
import org.openqa.selenium.testing.Ignore;
@@ -63,6 +64,37 @@ public class PageLoadingTest extends JUnit4TestBase {
6364

6465
private WebDriver localDriver;
6566

67+
private void initLocalDriver(String strategy) {
68+
if (localDriver != null) {
69+
localDriver.quit();
70+
}
71+
DesiredCapabilities caps = new DesiredCapabilities();
72+
caps.setCapability(CapabilityType.PAGE_LOADING_STRATEGY, strategy);
73+
localDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
74+
}
75+
76+
@Ignore(value = {CHROME, IE, OPERA, SAFARI, MARIONETTE, PHANTOMJS, HTMLUNIT})
77+
@NeedsLocalEnvironment
78+
@Test
79+
public void testShouldNotWaitForResourcesWithEagerStrategy() {
80+
initLocalDriver("unstable");
81+
82+
String slowPage = appServer.whereIs("slowLoadingResourcePage.html");
83+
84+
long start = System.currentTimeMillis();
85+
localDriver.get(slowPage);
86+
// We discard the element, but want a check to make sure the GET actually
87+
// completed.
88+
localDriver.findElement(By.id("peas"));
89+
90+
long end = System.currentTimeMillis();
91+
// The slow loading resource on that page takes 6 seconds to return. If we
92+
// waited for it, our load time should be over 6 seconds.
93+
long duration = end - start;
94+
95+
assertTrue("Took too long to load page: " + duration, duration < 5 * 1000);
96+
}
97+
6698
@Test
6799
public void testShouldWaitForDocumentToBeLoaded() {
68100
driver.get(pages.simpleTestPage);
@@ -267,32 +299,6 @@ public void testShouldNotHangIfDocumentOpenCallIsNeverFollowedByDocumentCloseCal
267299
waitFor(WaitingConditions.elementTextToContain(body, "world"));
268300
}
269301

270-
@Ignore
271-
@Test
272-
public void testShouldNotWaitIndefinitelyIfAnExternalResourceFailsToLoad() {
273-
String slowPage = appServer.whereIs("slowLoadingResourcePage.html");
274-
275-
Capabilities current = ((HasCapabilities) driver).getCapabilities();
276-
DesiredCapabilities caps = new DesiredCapabilities(current);
277-
caps.setCapability("webdriver.loading.strategy", "unstable");
278-
WebDriver testDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
279-
280-
long start = System.currentTimeMillis();
281-
testDriver.get(slowPage);
282-
// We discard the element, but want a check to make sure the GET actually
283-
// completed.
284-
testDriver.findElement(By.id("peas"));
285-
286-
long end = System.currentTimeMillis();
287-
// The slow loading resource on that page takes 6 seconds to return. If we
288-
// waited for it, our load time should be over 6 seconds.
289-
long duration = end - start;
290-
291-
testDriver.quit(); // Clean up before making assertions
292-
293-
assertTrue("Took too long to load page: " + duration, duration < 5 * 1000);
294-
}
295-
296302
@Ignore(value = {ANDROID, IPHONE, OPERA, SAFARI, OPERA_MOBILE, MARIONETTE},
297303
reason = "Not implemented; Safari: see issue 687, comment 41",
298304
issues = {687})
@@ -301,19 +307,18 @@ public void testShouldNotWaitIndefinitelyIfAnExternalResourceFailsToLoad() {
301307
public void testShouldTimeoutIfAPageTakesTooLongToLoad() {
302308
driver.manage().timeouts().pageLoadTimeout(2, SECONDS);
303309

304-
long start = System.currentTimeMillis();
310+
// Get the sleeping servlet with a pause of 5 seconds
311+
String slowPage = appServer.whereIs("sleep?time=5");
305312

313+
long start = System.currentTimeMillis();
306314
try {
307-
// Get the sleeping servlet with a pause of 5 seconds
308-
String slowPage = appServer.whereIs("sleep?time=5");
309-
310315
driver.get(slowPage);
311-
312316
fail("I should have timed out");
313317
} catch (RuntimeException e) {
318+
long end = System.currentTimeMillis();
319+
314320
assertThat(e, is(instanceOf(TimeoutException.class)));
315321

316-
long end = System.currentTimeMillis();
317322
int duration = (int) (end - start);
318323
assertThat(duration, greaterThan(2000));
319324
assertThat(duration, lessThan(3000));

javascript/firefox-driver/js/firefoxDriver.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ goog.require('goog.math.Coordinate');
4343
goog.require('goog.math.Size');
4444

4545

46-
FirefoxDriver = function(server, enableNativeEvents, win) {
46+
FirefoxDriver = function(server, enableNativeEvents, win, opt_pageLoadingStrategy) {
4747
this.server = server;
4848
this.enableNativeEvents = enableNativeEvents;
4949
this.window = win;
50+
this.pageLoadingStrategy = opt_pageLoadingStrategy || 'normal';
5051

5152
// Default to a two second timeout.
5253
this.alertTimeout = 2000;

javascript/firefox-driver/js/nsCommandProcessor.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ goog.require('goog.array');
3939
goog.require('wdSessionStoreService');
4040

4141

42-
var loadStrategy_ = 'conservative';
43-
4442
/**
4543
* When this component is loaded, load the necessary subscripts.
4644
*/
@@ -186,7 +184,9 @@ DelayedCommand.DEFAULT_SLEEP_DELAY = 100;
186184
* @param {Number} ms The delay in milliseconds.
187185
*/
188186
DelayedCommand.prototype.execute = function(ms) {
189-
if ('unstable' != loadStrategy_ && !this.yieldedForBackgroundExecution_) {
187+
var loadStrategy = Utils.getPageLoadingStrategy();
188+
if ('unstable' != loadStrategy && 'eager' != loadStrategy && 'none' != loadStrategy
189+
&& !this.yieldedForBackgroundExecution_) {
190190
this.yieldedForBackgroundExecution_ = true;
191191
fxdriver.profiler.log(
192192
{'event': 'YIELD_TO_PAGE_LOAD', 'startorend': 'start'});
@@ -203,7 +203,8 @@ DelayedCommand.prototype.execute = function(ms) {
203203
* command for a pending request in the current window's nsILoadGroup.
204204
*/
205205
DelayedCommand.prototype.shouldDelayExecutionForPendingRequest_ = function() {
206-
if ('unstable' == loadStrategy_) {
206+
var loadStrategy = Utils.getPageLoadingStrategy();
207+
if ('unstable' == loadStrategy || 'eager' == loadStrategy && 'none' == loadStrategy) {
207208
return false;
208209
}
209210

@@ -358,12 +359,6 @@ var nsCommandProcessor = function() {
358359
eval(Utils.loadUrl('resource://fxdriver/json2.js'));
359360
}
360361

361-
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
362-
363-
if (prefs.prefHasUserValue('webdriver.load.strategy')) {
364-
loadStrategy_ = prefs.getCharPref('webdriver.load.strategy');
365-
}
366-
367362
this.wrappedJSObject = this;
368363
this.wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].
369364
getService(Components.interfaces.nsIWindowMediator);

javascript/firefox-driver/js/sessionstore.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ wdSessionStoreService.CAPABILITY_PREFERENCE_MAPPING = {
162162
'locationContextEnabled': 'geo.enabled',
163163
'browserConnectionEnabled': 'dom.network.enabled',
164164
'acceptSslCerts': 'webdriver_accept_untrusted_certs',
165-
'nativeEvents' : 'webdriver_enable_native_events'
165+
'nativeEvents' : 'webdriver_enable_native_events',
166+
'pageLoadingStrategy' : 'webdriver.load.strategy'
166167
};
167168
// TODO: Don't save firefox specific capability acceptSslCerts as preferences.
168169

@@ -203,19 +204,20 @@ wdSessionStoreService.prototype.configure_ = function(response, desiredCaps,
203204
* @param {!FirefoxDriver} driver The driver instance.
204205
* @private
205206
*/
206-
wdSessionStoreService.prototype.configureCapabilities_ = function(capabilities,
207+
wdSessionStoreService.prototype.configureCapabilities_ = function(capabilities,
207208
driver) {
208209
var prefStore = fxdriver.moz.getService('@mozilla.org/preferences-service;1',
209210
'nsIPrefBranch');
210211
goog.object.forEach(capabilities, function(value, key) {
211-
if (!goog.isBoolean(value)) {
212-
return;
213-
}
214212
if (key in wdSessionStoreService.CAPABILITY_PREFERENCE_MAPPING) {
215213
var pref = wdSessionStoreService.CAPABILITY_PREFERENCE_MAPPING[key];
216-
prefStore.setBoolPref(pref, value);
217214
fxdriver.logging.info('Setting capability ' +
218-
key + ' (' + pref + ') to ' + value);
215+
key + ' (' + pref + ') to ' + value);
216+
if (goog.isBoolean(value)) {
217+
prefStore.setBoolPref(pref, value);
218+
} else {
219+
prefStore.setCharPref(pref, value);
220+
}
219221
if (key == 'nativeEvents') {
220222
driver.enableNativeEvents = value;
221223
}

javascript/firefox-driver/js/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ Utils.useNativeEvents = function() {
249249
return !!(enableNativeEvents && Utils.getNativeEvents());
250250
};
251251

252+
Utils.getPageLoadingStrategy = function() {
253+
var prefs =
254+
fxdriver.moz.getService('@mozilla.org/preferences-service;1', 'nsIPrefBranch');
255+
return prefs.prefHasUserValue('webdriver.load.strategy') ?
256+
prefs.getCharPref('webdriver.load.strategy') : 'normal';
257+
};
258+
252259
Utils.type = function(doc, element, text, opt_useNativeEvents, jsTimer, releaseModifiers,
253260
opt_keysState) {
254261

javascript/firefox-driver/js/webLoadingListener.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ ImpatientListener.prototype.onProgressChange = function(webProgress) {
143143
var readyState = this.win.document && this.win.document.readyState;
144144
var location = this.win.document.location;
145145

146+
fxdriver.logging.info('readyState is ' + readyState);
147+
146148
if (('complete' == readyState || 'interactive' == readyState) &&
147149
(location != 'about:blank')) {
148150
this.active = false;
@@ -163,11 +165,19 @@ ImpatientListener.prototype.onProgressChange = function(webProgress) {
163165
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
164166

165167
function buildHandler(browser, toCall, opt_window) {
166-
if (prefs.prefHasUserValue('webdriver.load.strategy')) {
167-
if ('unstable' == prefs.getCharPref('webdriver.load.strategy')) {
168-
return new ImpatientListener(browser, toCall, opt_window);
169-
}
168+
var strategy = Utils.getPageLoadingStrategy();
169+
if ('normal' == strategy) {
170+
return new PatientListener(browser, toCall, opt_window);
171+
}
172+
if ('unstable' == strategy || 'eager' == strategy) {
173+
return new ImpatientListener(browser, toCall, opt_window);
170174
}
175+
if ('none' == strategy) {
176+
return new DoNothing(browser, toCall, opt_window);
177+
}
178+
179+
fxdriver.logging.warn('Unsupported page loading strategy: ' + strategy);
180+
// Fall back to 'normal' strategy
171181
return new PatientListener(browser, toCall, opt_window);
172182
}
173183

0 commit comments

Comments
 (0)