ide-plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 statement("is(" + this.e2.toString() + "," + 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 statement("isnt(" + this.e2.toString() + "," + this.e1.toString() + ")");
  53. };
  54. NotEquals.prototype.verify = function() {
  55. return verify(this.assert());
  56. };
  57. function joinExpression(expression) {
  58. return "join(\",\"," + expression.toString() + ")";
  59. }
  60. function statement(expression) {
  61. expression.noBraces = true;
  62. var s = expression.toString();
  63. if(s.length == 0) {
  64. return null;
  65. }
  66. return s + ';';
  67. }
  68. function assignToVariable(type, variable, expression) {
  69. return variable + " = " + expression.toString();
  70. }
  71. function ifCondition(expression, callback) {
  72. return "if (" + expression.toString() + ") {\n" + callback() + "}";
  73. }
  74. function tryCatch(tryStatement, catchStatement, exception) {
  75. return "eval {\n" +
  76. indents(1) + tryStatement + "\n" +
  77. "};\n if ($@) {\n" +
  78. indents(1) + catchStatement + "\n" +
  79. "}";
  80. }
  81. function assertTrue(expression) {
  82. var exp = expression.toString();
  83. //var r = exp.match(/^(.+)\.([0-9A-Za-z_]+)\?$/);
  84. //if (r && r.length == 3) {
  85. // return "ok(" + r[1] + ".should be_" + r[2];
  86. //} else {
  87. return statement("ok(" + exp + ")");
  88. //}
  89. }
  90. function assertFalse(expression) {
  91. //return expression.invert().toString() + ".should be_false";
  92. var exp = expression.toString();
  93. //var r = exp.match(/^(.+)\.([0-9A-Za-z_]+)\?$/);
  94. //if (r && r.length == 3) {
  95. // return r[1] + ".should_not be_" + r[2];
  96. //} else {
  97. return statement("ok(!" + exp + ")");
  98. //}
  99. }
  100. function verify(stmt) {
  101. return stmt;
  102. }
  103. function verifyTrue(expression) {
  104. return verify(assertTrue(expression));
  105. }
  106. function verifyFalse(expression) {
  107. return verify(assertFalse(expression));
  108. }
  109. RegexpMatch.patternAsRegEx = function(pattern) {
  110. var str = pattern.replace(/\//g, "\\/");
  111. if (str.match(/\n/)) {
  112. str = str.replace(/\n/g, '\\n');
  113. return '/' + str + '/m';
  114. } else {
  115. return str = '/' + str + '/';
  116. }
  117. };
  118. RegexpMatch.prototype.patternAsRegEx = function() {
  119. return RegexpMatch.patternAsRegEx(this.pattern);
  120. };
  121. RegexpMatch.prototype.toString = function() {
  122. return this.expression + " =~ " + this.patternAsRegEx();
  123. };
  124. RegexpMatch.prototype.assert = function() {
  125. return statement("like(qr" + this.patternAsRegEx() + "," + this.expression + ")");
  126. };
  127. RegexpMatch.prototype.verify = function() {
  128. return verify(this.assert());
  129. };
  130. RegexpNotMatch.prototype.patternAsRegEx = function() {
  131. return RegexpMatch.patternAsRegEx(this.pattern);
  132. };
  133. RegexpNotMatch.prototype.toString = function() {
  134. return this.expression + " !~ " + this.patternAsRegEx();
  135. };
  136. RegexpNotMatch.prototype.assert = function() {
  137. return statement("unlike(qr" + this.patternAsRegEx() + "," + this.expression + ")");
  138. };
  139. RegexpNotMatch.prototype.verify = function() {
  140. return verify(this.assert());
  141. };
  142. function waitFor(expression) {
  143. if (expression.negative) {
  144. return "for(0..60) { my $ret = 1; eval { $ret = (" + expression.invert().toString() + ") }; if($@ || !$ret) { break }; sleep 1 }"
  145. } else {
  146. return "!60.times{ break if (" + expression.toString() + " rescue false); sleep 1 }"
  147. }
  148. }
  149. function assertOrVerifyFailure(line, isAssert) {
  150. return "assert_raise(Kernel) { " + line + "}";
  151. }
  152. function pause(milliseconds) {
  153. return "sleep " + (parseInt(milliseconds) / 1000);
  154. }
  155. function echo(message) {
  156. return "note " + xlateArgument(message);
  157. }
  158. function formatComment(comment) {
  159. return comment.comment.replace(/.+/mg, function(str) {
  160. return "# " + str;
  161. });
  162. }
  163. /**
  164. * Returns a string representing the suite for this formatter language.
  165. *
  166. * @param testSuite the suite to format
  167. * @param filename the file the formatted suite will be saved as
  168. */
  169. function formatSuite(testSuite, filename) {
  170. formattedSuite = 'require "spec/ruby"\n' +
  171. 'require "spec/runner"\n' +
  172. '\n' +
  173. "# output T/F as Green/Red\n" +
  174. "ENV['RSPEC_COLOR'] = 'true'\n" +
  175. '\n';
  176. for (var i = 0; i < testSuite.tests.length; ++i) {
  177. // have saved or loaded a suite
  178. if (typeof testSuite.tests[i].filename != 'undefined') {
  179. formattedSuite += 'require File.join(File.dirname(__FILE__), "' + testSuite.tests[i].filename.replace(/\.\w+$/, '') + '")\n';
  180. } else {
  181. // didn't load / save as a suite
  182. var testFile = testSuite.tests[i].getTitle();
  183. formattedSuite += 'require "' + testFile + '"\n';
  184. }
  185. }
  186. return formattedSuite;
  187. }
  188. this.options = {
  189. receiver: "$driver",
  190. rcHost: "localhost",
  191. rcPort: "4444",
  192. environment: "firefox",
  193. showSelenese: 'false',
  194. header:
  195. "use strict;\n" +
  196. "use warnings;\n" +
  197. "use Selenium::Remote::Driver;\n" +
  198. "use Selenium::Firefox;\n" +
  199. "use Test::More;\n" +
  200. "\n" +
  201. 'my $driver = Selenium::Firefox->new();\n\n',
  202. footer:
  203. "\n$driver->quit();\n" +
  204. "done_testing();\n",
  205. indent: "0",
  206. initialIndents: "0"
  207. };
  208. this.configForm =
  209. '<description>Variable for Selenium instance</description>' +
  210. '<textbox id="options_receiver" />' +
  211. '<description>Selenium RC host</description>' +
  212. '<textbox id="options_rcHost" />' +
  213. '<description>Selenium RC port</description>' +
  214. '<textbox id="options_rcPort" />' +
  215. '<description>Environment</description>' +
  216. '<textbox id="options_environment" />' +
  217. '<description>Header</description>' +
  218. '<textbox id="options_header" multiline="true" flex="1" rows="4"/>' +
  219. '<description>Footer</description>' +
  220. '<textbox id="options_footer" multiline="true" flex="1" rows="4"/>' +
  221. '<description>Indent</description>' +
  222. '<menulist id="options_indent"><menupopup>' +
  223. '<menuitem label="Tab" value="tab"/>' +
  224. '<menuitem label="1 space" value="1"/>' +
  225. '<menuitem label="2 spaces" value="2"/>' +
  226. '<menuitem label="3 spaces" value="3"/>' +
  227. '<menuitem label="4 spaces" value="4"/>' +
  228. '<menuitem label="5 spaces" value="5"/>' +
  229. '<menuitem label="6 spaces" value="6"/>' +
  230. '<menuitem label="7 spaces" value="7"/>' +
  231. '<menuitem label="8 spaces" value="8"/>' +
  232. '</menupopup></menulist>' +
  233. '<checkbox id="options_showSelenese" label="Show Selenese"/>';
  234. this.name = "Perl Test::More(WebDriver)";
  235. this.testcaseExtension = ".t";
  236. this.suiteExtension = ".t";
  237. this.webdriver = true;
  238. WDAPI.Driver = function() {
  239. this.ref = options.receiver;
  240. };
  241. WDAPI.Driver.searchContext = function(locatorType, locator) {
  242. var locatorString = xlateArgument(locator).replace(/([@%$])/,"\\$1");
  243. switch (locatorType) {
  244. case 'xpath':
  245. return locatorString + ', "xpath"';
  246. case 'css':
  247. return locatorString + ', "css"';
  248. case 'id':
  249. return locatorString + ', "id"';
  250. case 'link':
  251. return locatorString + ', "link"';
  252. case 'name':
  253. return locatorString + ', "name"';
  254. case 'tag_name':
  255. return locatorString + ', "tag_name"';
  256. }
  257. throw 'Error: unknown strategy [' + locatorType + '] for locator [' + locator + ']';
  258. };
  259. WDAPI.Driver.prototype.back = function() {
  260. return this.ref + "->navigate->back";
  261. };
  262. WDAPI.Driver.prototype.close = function() {
  263. return this.ref + "->close";
  264. };
  265. WDAPI.Driver.prototype.findElement = function(locatorType, locator) {
  266. return new WDAPI.Element(this.ref + "->find_element(" + WDAPI.Driver.searchContext(locatorType, locator) + ")");
  267. };
  268. WDAPI.Driver.prototype.findElements = function(locatorType, locator) {
  269. return new WDAPI.ElementList(this.ref + "->find_elements(" + WDAPI.Driver.searchContext(locatorType, locator) + ")");
  270. };
  271. WDAPI.Driver.prototype.getCurrentUrl = function() {
  272. return this.ref + "->get_current_url";
  273. };
  274. WDAPI.Driver.prototype.get = function(url) {
  275. return this.ref + "->get(" + url + ")";
  276. };
  277. WDAPI.Driver.prototype.getTitle = function() {
  278. return this.ref + "->get_title";
  279. };
  280. WDAPI.Driver.prototype.refresh = function() {
  281. return this.ref + "->refresh";
  282. };
  283. WDAPI.Driver.prototype.frame = function(locator) {
  284. return this.ref + "->switch_to_frame(" + xlateArgument(locator) + ")";
  285. }
  286. WDAPI.Element = function(ref) {
  287. this.ref = ref;
  288. };
  289. WDAPI.Element.prototype.clear = function() {
  290. return this.ref + "->clear";
  291. };
  292. WDAPI.Element.prototype.click = function() {
  293. return this.ref + "->click";
  294. };
  295. WDAPI.Element.prototype.getAttribute = function(attributeName) {
  296. return this.ref + "->get_attribute(" + xlateArgument(attributeName) + ")";
  297. };
  298. WDAPI.Element.prototype.getText = function() {
  299. return this.ref + "->get_text";
  300. };
  301. WDAPI.Element.prototype.isDisplayed = function() {
  302. return this.ref + "->is_displayed";
  303. };
  304. WDAPI.Element.prototype.isSelected = function() {
  305. return this.ref + "->is_selected";
  306. };
  307. WDAPI.Element.prototype.select = function(selectLocator) {
  308. if (selectLocator.type == 'index') {
  309. return "$driver->find_child_element(" + this.ref +
  310. ", \"//option[" + selectLocator.string + "]\" , \"xpath\")->click";
  311. }
  312. if (selectLocator.type == 'value') {
  313. return "$driver->find_child_element(" + this.ref +
  314. ", \"//*[\\@value='" + selectLocator.string+ "']\" , \"xpath\")->click";
  315. }
  316. return "$driver->find_child_element(" + this.ref +
  317. ", \"//*[text()='" + selectLocator.string+ "']\" , \"xpath\")->click";
  318. };
  319. WDAPI.Element.prototype.sendKeys = function(text) {
  320. return this.ref + "->send_keys(" + xlateArgument(text) + ")";
  321. };
  322. WDAPI.Element.prototype.submit = function() {
  323. return this.ref + "->submit";
  324. };
  325. WDAPI.ElementList = function(ref) {
  326. this.ref = ref;
  327. };
  328. WDAPI.ElementList.prototype.getItem = function(index) {
  329. return this.ref + "[" + index + "]";
  330. };
  331. WDAPI.ElementList.prototype.getSize = function() {
  332. return this.ref + "->size";
  333. };
  334. WDAPI.ElementList.prototype.isEmpty = function() {
  335. return this.ref + "->is_empty";
  336. };
  337. WDAPI.Utils = function() {
  338. };
  339. WDAPI.Utils.isElementPresent = function(how, what) {
  340. return this.ref + "->is_element_present(" + xlateArgument(what) + ", \"" + how + "\")";
  341. };