Created
May 9, 2012 09:07
-
-
Save piotrplenik/2643182 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Formatter for Selenium 2 / WebDriver PHP client. | |
*/ | |
var subScriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader); | |
subScriptLoader.loadSubScript('chrome://selenium-ide/content/formats/webdriver.js', this); | |
function useSeparateEqualsForArray() { | |
return true; | |
} | |
function testClassName(testName) { | |
return testName.split(/[^0-9A-Za-z]+/).map( | |
function(x) { | |
return capitalize(x); | |
}).join('Test'); | |
} | |
function testMethodName(testName) { | |
return "test" + testClassName(testName); | |
} | |
function nonBreakingSpace() { | |
return "\"\\u00a0\""; | |
} | |
function array(value) { | |
var str = 'array('; | |
for ( var i = 0; i < value.length; i++) { | |
str += string(value[i]); | |
if (i < value.length - 1) str += ", "; | |
} | |
str += ')'; | |
return str; | |
}; | |
Equals.prototype.toString = function() { | |
return this.e1.toString() + " == " + this.e2.toString(); | |
}; | |
Equals.prototype.assert = function() { | |
return "$this->assertEquals(" + this.e1.toString() + ", " | |
+ this.e2.toString() + ");"; | |
}; | |
Equals.prototype.verify = function() { | |
return verify(this.assert()); | |
}; | |
NotEquals.prototype.toString = function() { | |
return "!" + this.e1.toString() + " == " + this.e2.toString(); | |
}; | |
NotEquals.prototype.assert = function() { | |
return "$this->assertThat(" + this.e1.toString() + " != " + this.e2.toString() + ");"; | |
}; | |
NotEquals.prototype.verify = function() { | |
return verify(this.assert()); | |
}; | |
function joinExpression(expression) { | |
return "implode(',', " + expression.toString() + ")"; | |
} | |
function statement(expression) { | |
var s = expression.toString(); | |
if (s.length == 0) { | |
return null; | |
} | |
return s + ';'; | |
} | |
function assignToVariable(type, variable, expression) { | |
return "$" + variable + " = " + expression.toString(); | |
} | |
function ifCondition(expression, callback) { | |
return "if (" + expression.toString() + ") {\n" + callback() + "}"; | |
} | |
function assertTrue(expression) { | |
return "$this->assertTrue(" + expression.toString() + ");"; | |
} | |
function assertFalse(expression) { | |
return "$this->assertFalse(" + expression.toString() + ");"; | |
} | |
function verify(statement) { | |
return "try {\n" + | |
indents(1) + statement + "\n" + | |
"} catch ($exception $e) {\n" + | |
indents(1) + "$this->verificationErrors[] = $e->__toString();\n" + | |
"}"; | |
} | |
function verifyTrue(expression) { | |
return verify(assertTrue(expression)); | |
} | |
function verifyFalse(expression) { | |
return verify(assertFalse(expression)); | |
} | |
RegexpMatch.prototype.toString = function() { | |
var str = this.pattern; | |
if (str.match(/\"/) || str.match(/\n/)) { | |
str = str.replace(/\\/g, "\\\\"); | |
str = str.replace(/\"/g, '\\"'); | |
str = str.replace(/\n/g, '\\n'); | |
return '"' + str + '"'; | |
} else { | |
str = '"/' + str + '/"'; | |
} | |
return "(preg_match(" + str + ", " + this.expression + ") == 1);"; | |
}; | |
function waitFor(expression) { | |
return "for ($second = 0; ; $second++) {\n" + indent(1) | |
+ 'if ($second >= 60) $this->fail("timeout");\n' + indent(1) | |
+ "try {\n" | |
+ indent(2) + (expression.setup ? expression.setup() + " " : "") | |
+ indent(2) + "if (" + expression.toString() + ") break;\n" + indent(1) | |
+ "} catch (Exception $e) {}\n" + indent(1) + "sleep(1);\n" + "}\n"; | |
} | |
function assertOrVerifyFailure(line, isAssert) { | |
var message = '"expected failure"'; | |
var failStatement = "$this->fail(" + message + ");"; | |
return "try { " + line + " " + failStatement + " } catch (Exception $e) {}"; | |
}; | |
function pause(milliseconds) { | |
return "usleep(" + (parseInt(milliseconds, 10)) + ");"; | |
}; | |
function echo(message) { | |
return "print(" + xlateArgument(message) + ');'; | |
}; | |
function formatComment(comment) { | |
return comment.comment.replace(/.+/mg, function(str) { | |
return "// " + str; | |
}); | |
} | |
function defaultExtension() { | |
return this.options.defaultExtension; | |
} | |
this.options = { | |
environment : "*chrome", | |
indent : "tab", | |
initialIndents : '2', | |
showSelenese: 'false', | |
defaultExtension : "php" | |
}; | |
options.header = | |
'<?php\n' + | |
'\n' + | |
'class ${className} extends PHPUnit_Extensions_Selenium2TestCase\n' + | |
'{\n' + | |
'\n' + | |
indents(1) + 'protected $verificationErrors = array();' + | |
indents(1) + 'protected $baseUrl = "";' + | |
'\n' + | |
'\n' + | |
indents(1) + 'protected function setUp()\n' + | |
indents(1) + '{\n' + | |
indents(2) + '$this->baseUrl = "${baseURL}";\n' + | |
indents(2) + '$this->setBrowser("${environment}");\n' + | |
indents(2) + '$this->setBrowserUrl($this->baseUrl);\n' + | |
indents(1) + '}\n' + | |
'\n' + | |
indents(1) + '/** \n' + | |
indents(1) + ' * @Test \n' + | |
indents(1) + ' **/ \n' + | |
indents(1) + 'public function ${methodName}() \n' + | |
indents(1) + '{\n' + | |
indents(2) + '$this->url("${baseURL}");\n'; | |
options.footer = | |
indents(1) + '}\n' + | |
'\n' + | |
indents(1) + '/** \n' + | |
indents(1) + ' * After \n' + | |
indents(1) + ' **/ \n' + | |
indents(1) + 'protected function setDown()\n' + | |
indents(1) + '{\n' + | |
indents(2) + '$verificationErrorsString = implode(",", $this->verificationErrors); \n' + | |
indents(2) + 'if($verificationErrorsString != "") $this->fail($verificationErrorsString); \n' + | |
indents(2) + '$this->setBrowserUrl("${baseURL}");\n' + | |
indents(1) + '}\n' + | |
'}\n' + | |
'\n' + | |
"?>"; | |
this.configForm = | |
'<description>Variable for Selenium instance</description>' + | |
'<description>Environment</description>' + | |
'<textbox id="options_environment" />' + | |
'<checkbox id="options_showSelenese" label="Show Selenese"/>'; | |
this.name = "PHPUnit (WebDriver)"; | |
this.testcaseExtension = ".php"; | |
this.suiteExtension = ".php"; | |
this.webdriver = true; | |
WDAPI.Driver = function() { | |
this.ref = 'this'; | |
}; | |
WDAPI.Driver.searchContext = function(locatorType, locator) { | |
var locatorString = xlateArgument(locator); | |
switch (locatorType) { | |
case 'xpath': | |
return '$this->byXPath(' + locatorString + ')'; | |
case 'css': | |
return '$this->byCssSelector(' + locatorString + ')'; | |
case 'id': | |
return '$this->byId(' + locatorString + ')'; | |
case 'link': | |
return '$this->byLinkText(' + locatorString + ')'; | |
case 'name': | |
return '$this->byName(' + locatorString + ')'; | |
case 'tag_name': | |
return '$this->byTagName(' + locatorString + ')'; | |
} | |
throw 'Error: unknown strategy [' + locatorType + '] for locator [' + locator + ']'; | |
}; | |
WDAPI.Driver.prototype.back = function() { | |
return this.ref + "->navigate()->back()"; | |
}; | |
WDAPI.Driver.prototype.close = function() { | |
return this.ref + "->close()"; | |
}; | |
WDAPI.Driver.prototype.findElement = function(locatorType, locator) { | |
return new WDAPI.Element(this.ref + "->findElement(" + WDAPI.Driver.searchContext(locatorType, locator) + ")"); | |
}; | |
WDAPI.Driver.prototype.findElements = function(locatorType, locator) { | |
return new WDAPI.ElementList(this.ref + "->findElements(" + WDAPI.Driver.searchContext(locatorType, locator) + ")"); | |
}; | |
WDAPI.Driver.prototype.getCurrentUrl = function() { | |
return this.ref + "->getCurrentUrl()"; | |
}; | |
WDAPI.Driver.prototype.get = function(url) { | |
if (url.length > 1 && (url.substring(1,8) == "http://" || url.substring(1,9) == "https://")) { // url is quoted | |
return this.ref + "->get(" + url + ")"; | |
} else { | |
return this.ref + "->get($this->baseUrl + " + url + ")"; | |
} | |
}; | |
WDAPI.Driver.prototype.getTitle = function() { | |
return this.ref + "->getTitle()"; | |
}; | |
WDAPI.Driver.prototype.refresh = function() { | |
return this.ref + "->navigate()->refresh()"; | |
}; | |
WDAPI.Element = function(ref) { | |
this.ref = ref; | |
}; | |
WDAPI.Element.prototype.clear = function() { | |
return this.ref + "->clear()"; | |
}; | |
WDAPI.Element.prototype.click = function() { | |
return this.ref + "->click()"; | |
}; | |
WDAPI.Element.prototype.getAttribute = function(attributeName) { | |
return this.ref + "->getAttribute(" + xlateArgument(attributeName) + ")"; | |
}; | |
WDAPI.Element.prototype.getText = function() { | |
return this.ref + "->getText()"; | |
}; | |
WDAPI.Element.prototype.isDisplayed = function() { | |
return this.ref + "->isDisplayed()"; | |
}; | |
WDAPI.Element.prototype.isSelected = function() { | |
return this.ref + "->isSelected()"; | |
}; | |
WDAPI.Element.prototype.sendKeys = function(text) { | |
return this.ref + "->sendKeys(" + xlateArgument(text) + ")"; | |
}; | |
WDAPI.Element.prototype.submit = function() { | |
return this.ref + "->submit()"; | |
}; | |
WDAPI.Element.prototype.select = function(label) { | |
return "new Select(" + this.ref + ").selectByVisibleText(" + xlateArgument(label) + ")"; | |
}; | |
WDAPI.ElementList = function(ref) { | |
this.ref = ref; | |
}; | |
WDAPI.ElementList.prototype.getItem = function(index) { | |
return this.ref + "[" + index + "]"; | |
}; | |
WDAPI.ElementList.prototype.getSize = function() { | |
return this.ref + "->size()"; | |
}; | |
WDAPI.ElementList.prototype.isEmpty = function() { | |
return this.ref + "->isEmpty()"; | |
}; | |
WDAPI.Utils = function() { | |
}; | |
WDAPI.Utils.isElementPresent = function(how, what) { | |
return "isElementPresent(" + WDAPI.Driver.searchContext(how, what) + ")"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment