Created
July 13, 2012 16:24
-
-
Save eliotsykes/3105781 to your computer and use it in GitHub Desktop.
selenium selenese helper functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Perform HTTP POST to the given URL using selenium. Put this in actions.js. | |
// Works with selenese: | post | http://url-to-http-post-to.com/ | | |
Selenium.prototype.doPost = function(url) { | |
var form = this.getCurrentWindow().document.createElement('form'); | |
form.id = 'formAddedBySeleniumDoPost'; | |
form.method = 'POST'; | |
form.action = url; | |
var body = this.browserbot.findElement("css=body"); | |
if (typeof body == "undefined") { | |
throw new SeleniumError("No body element to append form element to for performing POST to url '" + url + "'"); | |
} | |
body.appendChild(form); | |
form.submit(); | |
} | |
Selenium.prototype.doOpenViaAjax = function(url) { | |
var jQuery = this.getCurrentWindow().jQuery; | |
jQuery.ajax( { url: url }) | |
.done(function() {}) | |
.fail(function() { alert("doOpenViaAjax error"); }) | |
.always(function() {}); | |
}; | |
Selenium.prototype.isAjaxComplete = function() { | |
return 0 == this.getCurrentWindow().jQuery.active; | |
}; | |
Selenium.prototype.getLabel = function(locator) { | |
var labelledElement = this.browserbot.findElement(locator); | |
return this.getText("css=label[for='" + labelledElement.id + "']"); | |
}; | |
// Checks form element has required="required" or required="" | |
Selenium.prototype.isRequired = function(locator) { | |
return this._isBooleanAttributePresent('required', locator); | |
}; | |
Selenium.prototype.assertMaxLength = function(locator, expectedMaxLength) { | |
var el = this.browserbot.findElement(locator); | |
var actualMaxLength = -1; | |
if (el) { | |
actualMaxLength = el.getAttribute("maxlength"); | |
} | |
Assert.matches(expectedMaxLength, actualMaxLength); | |
}; | |
// Checks form element has autofocus="autofocus" or autofocus="" | |
Selenium.prototype.isAutofocus = function(locator) { | |
return this._isBooleanAttributePresent('autofocus', locator); | |
}; | |
Selenium.prototype.doTurnOffBrowserValidation = function(locator) { | |
locator = locator || 'css=form'; | |
this.browserbot.findElement(locator).setAttribute('novalidate', 'novalidate'); | |
} | |
Selenium.prototype.doTurnOnBrowserValidation = function(locator) { | |
locator = locator || 'css=form'; | |
this.browserbot.findElement(locator).removeAttribute('novalidate'); | |
} | |
Selenium.prototype._isBooleanAttributePresent = function(attributeName, locator) { | |
var el = this.browserbot.findElement(locator); | |
if (el) { | |
var attributeValue = el.getAttribute(attributeName); | |
return attributeValue === attributeName || attributeValue === ""; | |
} | |
return false; | |
} | |
// Stopwatch logs+echoes how long part of a test takes. | |
// Usage: | |
// | startStopwatch | Summary of step(s) being timed | | |
// \ ... step(s) you want to time ... | | |
// | stopStopwatch | | | |
Selenium.prototype.doStartStopwatch = function(label) { | |
var start = new Date(); | |
storedVars["stopwatch"] = { label: label, start: start }; | |
var msg = "Stopwatch started '" + label + "' " + start; | |
LOG.info(msg); | |
this.doEcho(msg); | |
}; | |
Selenium.prototype.doStopStopwatch = function() { | |
var end = new Date(); | |
var stopwatch = storedVars["stopwatch"]; | |
var durationInMillis = end - stopwatch.start; | |
var durationInSecs = durationInMillis / 1000; | |
var msg = "'" + stopwatch.label + "' took " + durationInSecs + " secs"; | |
LOG.info(msg); | |
this.doEcho(msg); | |
storedVars["stopwatch"] = null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment