Skip to content

Commit

Permalink
Implementing pageLoadingStrategy capability in Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Jan 5, 2014
1 parent afe029b commit 4203f10
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .idea/libraries/phantomjsdriver.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public interface CapabilityType {

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

String PAGE_LOADING_STRATEGY = "pageLoadingStrategy";

/**
* Moved InternetExplorer specific CapabilityTypes into InternetExplorerDriver.java for consistency
*/
Expand Down
69 changes: 37 additions & 32 deletions java/client/test/org/openqa/selenium/PageLoadingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.environment.GlobalTestEnvironment;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.Ignore;
Expand Down Expand Up @@ -63,6 +64,37 @@ public class PageLoadingTest extends JUnit4TestBase {

private WebDriver localDriver;

private void initLocalDriver(String strategy) {
if (localDriver != null) {
localDriver.quit();
}
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.PAGE_LOADING_STRATEGY, strategy);
localDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
}

@Ignore(value = {CHROME, IE, OPERA, SAFARI, MARIONETTE, PHANTOMJS, HTMLUNIT})
@NeedsLocalEnvironment
@Test
public void testShouldNotWaitForResourcesWithEagerStrategy() {
initLocalDriver("unstable");

String slowPage = appServer.whereIs("slowLoadingResourcePage.html");

long start = System.currentTimeMillis();
localDriver.get(slowPage);
// We discard the element, but want a check to make sure the GET actually
// completed.
localDriver.findElement(By.id("peas"));

long end = System.currentTimeMillis();
// The slow loading resource on that page takes 6 seconds to return. If we
// waited for it, our load time should be over 6 seconds.
long duration = end - start;

assertTrue("Took too long to load page: " + duration, duration < 5 * 1000);
}

@Test
public void testShouldWaitForDocumentToBeLoaded() {
driver.get(pages.simpleTestPage);
Expand Down Expand Up @@ -267,32 +299,6 @@ public void testShouldNotHangIfDocumentOpenCallIsNeverFollowedByDocumentCloseCal
waitFor(WaitingConditions.elementTextToContain(body, "world"));
}

@Ignore
@Test
public void testShouldNotWaitIndefinitelyIfAnExternalResourceFailsToLoad() {
String slowPage = appServer.whereIs("slowLoadingResourcePage.html");

Capabilities current = ((HasCapabilities) driver).getCapabilities();
DesiredCapabilities caps = new DesiredCapabilities(current);
caps.setCapability("webdriver.loading.strategy", "unstable");
WebDriver testDriver = new WebDriverBuilder().setDesiredCapabilities(caps).get();

long start = System.currentTimeMillis();
testDriver.get(slowPage);
// We discard the element, but want a check to make sure the GET actually
// completed.
testDriver.findElement(By.id("peas"));

long end = System.currentTimeMillis();
// The slow loading resource on that page takes 6 seconds to return. If we
// waited for it, our load time should be over 6 seconds.
long duration = end - start;

testDriver.quit(); // Clean up before making assertions

assertTrue("Took too long to load page: " + duration, duration < 5 * 1000);
}

@Ignore(value = {ANDROID, IPHONE, OPERA, SAFARI, OPERA_MOBILE, MARIONETTE},
reason = "Not implemented; Safari: see issue 687, comment 41",
issues = {687})
Expand All @@ -301,19 +307,18 @@ public void testShouldNotWaitIndefinitelyIfAnExternalResourceFailsToLoad() {
public void testShouldTimeoutIfAPageTakesTooLongToLoad() {
driver.manage().timeouts().pageLoadTimeout(2, SECONDS);

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

long start = System.currentTimeMillis();
try {
// Get the sleeping servlet with a pause of 5 seconds
String slowPage = appServer.whereIs("sleep?time=5");

driver.get(slowPage);

fail("I should have timed out");
} catch (RuntimeException e) {
long end = System.currentTimeMillis();

assertThat(e, is(instanceOf(TimeoutException.class)));

long end = System.currentTimeMillis();
int duration = (int) (end - start);
assertThat(duration, greaterThan(2000));
assertThat(duration, lessThan(3000));
Expand Down
3 changes: 2 additions & 1 deletion javascript/firefox-driver/js/firefoxDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ goog.require('goog.math.Coordinate');
goog.require('goog.math.Size');


FirefoxDriver = function(server, enableNativeEvents, win) {
FirefoxDriver = function(server, enableNativeEvents, win, opt_pageLoadingStrategy) {
this.server = server;
this.enableNativeEvents = enableNativeEvents;
this.window = win;
this.pageLoadingStrategy = opt_pageLoadingStrategy || 'normal';

// Default to a two second timeout.
this.alertTimeout = 2000;
Expand Down
15 changes: 5 additions & 10 deletions javascript/firefox-driver/js/nsCommandProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ goog.require('goog.array');
goog.require('wdSessionStoreService');


var loadStrategy_ = 'conservative';

/**
* When this component is loaded, load the necessary subscripts.
*/
Expand Down Expand Up @@ -186,7 +184,9 @@ DelayedCommand.DEFAULT_SLEEP_DELAY = 100;
* @param {Number} ms The delay in milliseconds.
*/
DelayedCommand.prototype.execute = function(ms) {
if ('unstable' != loadStrategy_ && !this.yieldedForBackgroundExecution_) {
var loadStrategy = Utils.getPageLoadingStrategy();
if ('unstable' != loadStrategy && 'eager' != loadStrategy && 'none' != loadStrategy
&& !this.yieldedForBackgroundExecution_) {
this.yieldedForBackgroundExecution_ = true;
fxdriver.profiler.log(
{'event': 'YIELD_TO_PAGE_LOAD', 'startorend': 'start'});
Expand All @@ -203,7 +203,8 @@ DelayedCommand.prototype.execute = function(ms) {
* command for a pending request in the current window's nsILoadGroup.
*/
DelayedCommand.prototype.shouldDelayExecutionForPendingRequest_ = function() {
if ('unstable' == loadStrategy_) {
var loadStrategy = Utils.getPageLoadingStrategy();
if ('unstable' == loadStrategy || 'eager' == loadStrategy && 'none' == loadStrategy) {
return false;
}

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

var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);

if (prefs.prefHasUserValue('webdriver.load.strategy')) {
loadStrategy_ = prefs.getCharPref('webdriver.load.strategy');
}

this.wrappedJSObject = this;
this.wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].
getService(Components.interfaces.nsIWindowMediator);
Expand Down
16 changes: 9 additions & 7 deletions javascript/firefox-driver/js/sessionstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ wdSessionStoreService.CAPABILITY_PREFERENCE_MAPPING = {
'locationContextEnabled': 'geo.enabled',
'browserConnectionEnabled': 'dom.network.enabled',
'acceptSslCerts': 'webdriver_accept_untrusted_certs',
'nativeEvents' : 'webdriver_enable_native_events'
'nativeEvents' : 'webdriver_enable_native_events',
'pageLoadingStrategy' : 'webdriver.load.strategy'
};
// TODO: Don't save firefox specific capability acceptSslCerts as preferences.

Expand Down Expand Up @@ -203,19 +204,20 @@ wdSessionStoreService.prototype.configure_ = function(response, desiredCaps,
* @param {!FirefoxDriver} driver The driver instance.
* @private
*/
wdSessionStoreService.prototype.configureCapabilities_ = function(capabilities,
wdSessionStoreService.prototype.configureCapabilities_ = function(capabilities,
driver) {
var prefStore = fxdriver.moz.getService('@mozilla.org/preferences-service;1',
'nsIPrefBranch');
goog.object.forEach(capabilities, function(value, key) {
if (!goog.isBoolean(value)) {
return;
}
if (key in wdSessionStoreService.CAPABILITY_PREFERENCE_MAPPING) {
var pref = wdSessionStoreService.CAPABILITY_PREFERENCE_MAPPING[key];
prefStore.setBoolPref(pref, value);
fxdriver.logging.info('Setting capability ' +
key + ' (' + pref + ') to ' + value);
key + ' (' + pref + ') to ' + value);
if (goog.isBoolean(value)) {
prefStore.setBoolPref(pref, value);
} else {
prefStore.setCharPref(pref, value);
}
if (key == 'nativeEvents') {
driver.enableNativeEvents = value;
}
Expand Down
7 changes: 7 additions & 0 deletions javascript/firefox-driver/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ Utils.useNativeEvents = function() {
return !!(enableNativeEvents && Utils.getNativeEvents());
};

Utils.getPageLoadingStrategy = function() {
var prefs =
fxdriver.moz.getService('@mozilla.org/preferences-service;1', 'nsIPrefBranch');
return prefs.prefHasUserValue('webdriver.load.strategy') ?
prefs.getCharPref('webdriver.load.strategy') : 'normal';
};

Utils.type = function(doc, element, text, opt_useNativeEvents, jsTimer, releaseModifiers,
opt_keysState) {

Expand Down
18 changes: 14 additions & 4 deletions javascript/firefox-driver/js/webLoadingListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ ImpatientListener.prototype.onProgressChange = function(webProgress) {
var readyState = this.win.document && this.win.document.readyState;
var location = this.win.document.location;

fxdriver.logging.info('readyState is ' + readyState);

if (('complete' == readyState || 'interactive' == readyState) &&
(location != 'about:blank')) {
this.active = false;
Expand All @@ -163,11 +165,19 @@ ImpatientListener.prototype.onProgressChange = function(webProgress) {
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);

function buildHandler(browser, toCall, opt_window) {
if (prefs.prefHasUserValue('webdriver.load.strategy')) {
if ('unstable' == prefs.getCharPref('webdriver.load.strategy')) {
return new ImpatientListener(browser, toCall, opt_window);
}
var strategy = Utils.getPageLoadingStrategy();
if ('normal' == strategy) {
return new PatientListener(browser, toCall, opt_window);
}
if ('unstable' == strategy || 'eager' == strategy) {
return new ImpatientListener(browser, toCall, opt_window);
}
if ('none' == strategy) {
return new DoNothing(browser, toCall, opt_window);
}

fxdriver.logging.warn('Unsupported page loading strategy: ' + strategy);
// Fall back to 'normal' strategy
return new PatientListener(browser, toCall, opt_window);
}

Expand Down

0 comments on commit 4203f10

Please sign in to comment.