1
0

ide-plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Formatter for Selenium 2 / WebDriver perl-rc client.
  3. * To install...
  4. 1. Open the Selenium IDE
  5. 2. Options >> Options
  6. 3. Formats Tab
  7. 4. Click Add at the bottom
  8. 5. In the name field call it 'Perl-Webdriver'
  9. 6. Paste this entire source in the main textbox
  10. 7. Click 'Save'
  11. 8. Click 'Ok'
  12. */
  13. var subScriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
  14. subScriptLoader.loadSubScript('chrome://selenium-ide/content/formats/webdriver.js', this);
  15. function testClassName(testName) {
  16. return testName.split(/[^0-9A-Za-z]+/).map(
  17. function(x) {
  18. return capitalize(x);
  19. }).join('');
  20. }
  21. function testMethodName(testName) {
  22. return "test_" + underscore(testName);
  23. }
  24. function nonBreakingSpace() {
  25. return "\"\\xa0\"";
  26. }
  27. function array(value) {
  28. var str = '[';
  29. for (var i = 0; i < value.length; i++) {
  30. str += string(value[i]);
  31. if (i < value.length - 1) str += ", ";
  32. }
  33. str += ']';
  34. return str;
  35. }
  36. notOperator = function() {
  37. return "not ";
  38. };
  39. Equals.prototype.toString = function() {
  40. return this.e2.toString() + " == " + this.e1.toString();
  41. };
  42. Equals.prototype.assert = function() {
  43. return "(" + this.e2.toString() + ").should == " + this.e1.toString();
  44. };
  45. Equals.prototype.verify = function() {
  46. return verify(this.assert());
  47. };
  48. NotEquals.prototype.toString = function() {
  49. return this.e1.toString() + " != " + this.e2.toString();
  50. };
  51. NotEquals.prototype.assert = function() {
  52. return "(" + this.e2.toString() + ").should_not == " + this.e1.toString();
  53. };
  54. NotEquals.prototype.verify = function() {
  55. return verify(this.assert());
  56. };
  57. function joinExpression(expression) {
  58. return expression.toString() + ".join(\",\")";
  59. }
  60. function statement(expression) {
  61. expression.noBraces = true;
  62. return expression.toString();
  63. }
  64. function assignToVariable(type, variable, expression) {
  65. return variable + " = " + expression.toString();
  66. }
  67. function ifCondition(expression, callback) {
  68. return "if (" + expression.toString() + ") {\n" + callback() + "}";
  69. }
  70. function tryCatch(tryStatement, catchStatement, exception) {
  71. return "eval {\n" +
  72. indents(1) + tryStatement + "\n" +
  73. "};\n if ($@) {\n" +
  74. indents(1) + catchStatement + "\n" +
  75. "}";
  76. }
  77. function assertTrue(expression) {
  78. var exp = expression.toString();
  79. var r = exp.match(/^(.+)\.([0-9A-Za-z_]+)\?$/);
  80. if (r && r.length == 3) {
  81. return r[1] + ".should be_" + r[2];
  82. } else {
  83. return exp + ".should be_true";
  84. }
  85. }
  86. function assertFalse(expression) {
  87. //return expression.invert().toString() + ".should be_false";
  88. var exp = expression.toString();
  89. var r = exp.match(/^(.+)\.([0-9A-Za-z_]+)\?$/);
  90. if (r && r.length == 3) {
  91. return r[1] + ".should_not be_" + r[2];
  92. } else {
  93. return exp + ".should be_false";
  94. }
  95. }
  96. function verify(statement) {
  97. return "verify { " + statement + " }";
  98. }
  99. function verifyTrue(expression) {
  100. return verify(assertTrue(expression));
  101. }
  102. function verifyFalse(expression) {
  103. return verify(assertFalse(expression));
  104. }
  105. RegexpMatch.patternAsRegEx = function(pattern) {
  106. var str = pattern.replace(/\//g, "\\/");
  107. if (str.match(/\n/)) {
  108. str = str.replace(/\n/g, '\\n');
  109. return '/' + str + '/m';
  110. } else {
  111. return str = '/' + str + '/';
  112. }
  113. };
  114. RegexpMatch.prototype.patternAsRegEx = function() {
  115. return RegexpMatch.patternAsRegEx(this.pattern);
  116. };
  117. RegexpMatch.prototype.toString = function() {
  118. return this.expression + " =~ " + this.patternAsRegEx();
  119. };
  120. RegexpMatch.prototype.assert = function() {
  121. return this.expression + ".should =~ " + this.patternAsRegEx();
  122. };
  123. RegexpMatch.prototype.verify = function() {
  124. return verify(this.assert());
  125. };
  126. RegexpNotMatch.prototype.patternAsRegEx = function() {
  127. return RegexpMatch.patternAsRegEx(this.pattern);
  128. };
  129. RegexpNotMatch.prototype.toString = function() {
  130. return this.expression + " !~ " + this.patternAsRegEx();
  131. };
  132. RegexpNotMatch.prototype.assert = function() {
  133. return this.expression + ".should_not =~ " + this.patternAsRegEx();
  134. };
  135. RegexpNotMatch.prototype.verify = function() {
  136. return verify(this.assert());
  137. };
  138. function waitFor(expression) {
  139. if (expression.negative) {
  140. return "for(0..60) { my $ret = 1; eval { $ret = (" + expression.invert().toString() + ") }; if($@ || !$ret) { break }; sleep 1 }"
  141. } else {
  142. return "!60.times{ break if (" + expression.toString() + " rescue false); sleep 1 }"
  143. }
  144. }
  145. function assertOrVerifyFailure(line, isAssert) {
  146. return "assert_raise(Kernel) { " + line + "}";
  147. }
  148. function pause(milliseconds) {
  149. return "sleep " + (parseInt(milliseconds) / 1000) + ";";
  150. }
  151. function echo(message) {
  152. return "print " + xlateArgument(message) + ";";
  153. }
  154. function formatComment(comment) {
  155. return comment.comment.replace(/.+/mg, function(str) {
  156. return "# " + str;
  157. });
  158. }
  159. /**
  160. * Returns a string representing the suite for this formatter language.
  161. *
  162. * @param testSuite the suite to format
  163. * @param filename the file the formatted suite will be saved as
  164. */
  165. function formatSuite(testSuite, filename) {
  166. formattedSuite = 'require "spec/ruby"\n' +
  167. 'require "spec/runner"\n' +
  168. '\n' +
  169. "# output T/F as Green/Red\n" +
  170. "ENV['RSPEC_COLOR'] = 'true'\n" +
  171. '\n';
  172. for (var i = 0; i < testSuite.tests.length; ++i) {
  173. // have saved or loaded a suite
  174. if (typeof testSuite.tests[i].filename != 'undefined') {
  175. formattedSuite += 'require File.join(File.dirname(__FILE__), "' + testSuite.tests[i].filename.replace(/\.\w+$/, '') + '")\n';
  176. } else {
  177. // didn't load / save as a suite
  178. var testFile = testSuite.tests[i].getTitle();
  179. formattedSuite += 'require "' + testFile + '"\n';
  180. }
  181. }
  182. return formattedSuite;
  183. }
  184. this.options = {
  185. receiver: "$driver",
  186. rcHost: "localhost",
  187. rcPort: "4444",
  188. environment: "firefox",
  189. showSelenese: 'false',
  190. header:
  191. "use strict;\n" +
  192. "use warnings;\n" +
  193. "use Selenium::Remote::Driver;\n" +
  194. "use Test::More;\n" +
  195. "\n" +
  196. 'my ${receiver} = Selenium::Remote::Driver->new( remote_server_addr => "${rcHost}",\n' +
  197. ' port => ${rcPort},\n' +
  198. ' browser_name => "${environment}");\n' +
  199. "\n",
  200. footer:
  201. "$driver->quit();\n" +
  202. "done_testing();\n",
  203. indent: "0",
  204. initialIndents: "0"
  205. };
  206. this.configForm =
  207. '<description>Variable for Selenium instance</description>' +
  208. '<textbox id="options_receiver" />' +
  209. '<description>Selenium RC host</description>' +
  210. '<textbox id="options_rcHost" />' +
  211. '<description>Selenium RC port</description>' +
  212. '<textbox id="options_rcPort" />' +
  213. '<description>Environment</description>' +
  214. '<textbox id="options_environment" />' +
  215. '<description>Header</description>' +
  216. '<textbox id="options_header" multiline="true" flex="1" rows="4"/>' +
  217. '<description>Footer</description>' +
  218. '<textbox id="options_footer" multiline="true" flex="1" rows="4"/>' +
  219. '<description>Indent</description>' +
  220. '<menulist id="options_indent"><menupopup>' +
  221. '<menuitem label="Tab" value="tab"/>' +
  222. '<menuitem label="1 space" value="1"/>' +
  223. '<menuitem label="2 spaces" value="2"/>' +
  224. '<menuitem label="3 spaces" value="3"/>' +
  225. '<menuitem label="4 spaces" value="4"/>' +
  226. '<menuitem label="5 spaces" value="5"/>' +
  227. '<menuitem label="6 spaces" value="6"/>' +
  228. '<menuitem label="7 spaces" value="7"/>' +
  229. '<menuitem label="8 spaces" value="8"/>' +
  230. '</menupopup></menulist>' +
  231. '<checkbox id="options_showSelenese" label="Show Selenese"/>';
  232. this.name = "Perl-Webdriver";
  233. this.testcaseExtension = ".pl";
  234. this.suiteExtension = ".pl";
  235. this.webdriver = true;
  236. WDAPI.Driver = function() {
  237. this.ref = options.receiver;
  238. };
  239. WDAPI.Driver.searchContext = function(locatorType, locator) {
  240. var locatorString = xlateArgument(locator);
  241. switch (locatorType) {
  242. case 'xpath':
  243. return locatorString + ', "xpath"';
  244. case 'css':
  245. return locatorString + ', "css"';
  246. case 'id':
  247. return locatorString + ', "id"';
  248. case 'link':
  249. return locatorString + ', "link"';
  250. case 'name':
  251. return locatorString + ', "name"';
  252. case 'tag_name':
  253. return locatorString + ', "tag_name"';
  254. }
  255. throw 'Error: unknown strategy [' + locatorType + '] for locator [' + locator + ']';
  256. };
  257. WDAPI.Driver.prototype.back = function() {
  258. return this.ref + "->navigate->back;";
  259. };
  260. WDAPI.Driver.prototype.close = function() {
  261. return this.ref + "->close;";
  262. };
  263. WDAPI.Driver.prototype.findElement = function(locatorType, locator) {
  264. return new WDAPI.Element(this.ref + "->find_element(" + WDAPI.Driver.searchContext(locatorType, locator) + ")");
  265. };
  266. WDAPI.Driver.prototype.findElements = function(locatorType, locator) {
  267. return new WDAPI.ElementList(this.ref + "->find_elements(" + WDAPI.Driver.searchContext(locatorType, locator) + ");");
  268. };
  269. WDAPI.Driver.prototype.getCurrentUrl = function() {
  270. return this.ref + "->current_url;";
  271. };
  272. WDAPI.Driver.prototype.get = function(url) {
  273. return this.ref + "->get(" + url + ");";
  274. };
  275. WDAPI.Driver.prototype.getTitle = function() {
  276. return this.ref + "->title;";
  277. };
  278. WDAPI.Driver.prototype.refresh = function() {
  279. return this.ref + "->navigate->refresh;";
  280. };
  281. WDAPI.Element = function(ref) {
  282. this.ref = ref;
  283. };
  284. WDAPI.Element.prototype.clear = function() {
  285. return this.ref + "->clear;";
  286. };
  287. WDAPI.Element.prototype.click = function() {
  288. return this.ref + "->click;";
  289. };
  290. WDAPI.Element.prototype.getAttribute = function(attributeName) {
  291. return this.ref + "->attribute(" + xlateArgument(attributeName) + ");";
  292. };
  293. WDAPI.Element.prototype.getText = function() {
  294. return this.ref + "->text;";
  295. };
  296. WDAPI.Element.prototype.isDisplayed = function() {
  297. return this.ref + "->is_displayed;";
  298. };
  299. WDAPI.Element.prototype.isSelected = function() {
  300. return this.ref + "->is_selected;";
  301. };
  302. WDAPI.Element.prototype.sendKeys = function(text) {
  303. return this.ref + "->send_keys(" + xlateArgument(text) + ");";
  304. };
  305. WDAPI.Element.prototype.submit = function() {
  306. return this.ref + "->submit;";
  307. };
  308. WDAPI.ElementList = function(ref) {
  309. this.ref = ref;
  310. };
  311. WDAPI.ElementList.prototype.getItem = function(index) {
  312. return this.ref + "[" + index + "]";
  313. };
  314. WDAPI.ElementList.prototype.getSize = function() {
  315. return this.ref + "->size;";
  316. };
  317. WDAPI.ElementList.prototype.isEmpty = function() {
  318. return this.ref + "->is_empty;";
  319. };
  320. WDAPI.Utils = function() {
  321. };
  322. WDAPI.Utils.isElementPresent = function(how, what) {
  323. return "element_present?(:" + how + ", " + xlateArgument(what) + ")";
  324. };