1
0

ide-plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 Test::More;\n" +
  199. "\n" +
  200. 'my ${receiver} = Selenium::Remote::Driver->new( remote_server_addr => "${rcHost}",\n' +
  201. ' port => ${rcPort},\n' +
  202. ' browser_name => "${environment}");\n' +
  203. "\n",
  204. footer:
  205. "${receiver}->quit();\n" +
  206. "done_testing();\n",
  207. indent: "0",
  208. initialIndents: "0"
  209. };
  210. this.configForm =
  211. '<description>Variable for Selenium instance</description>' +
  212. '<textbox id="options_receiver" />' +
  213. '<description>Selenium RC host</description>' +
  214. '<textbox id="options_rcHost" />' +
  215. '<description>Selenium RC port</description>' +
  216. '<textbox id="options_rcPort" />' +
  217. '<description>Environment</description>' +
  218. '<textbox id="options_environment" />' +
  219. '<description>Header</description>' +
  220. '<textbox id="options_header" multiline="true" flex="1" rows="4"/>' +
  221. '<description>Footer</description>' +
  222. '<textbox id="options_footer" multiline="true" flex="1" rows="4"/>' +
  223. '<description>Indent</description>' +
  224. '<menulist id="options_indent"><menupopup>' +
  225. '<menuitem label="Tab" value="tab"/>' +
  226. '<menuitem label="1 space" value="1"/>' +
  227. '<menuitem label="2 spaces" value="2"/>' +
  228. '<menuitem label="3 spaces" value="3"/>' +
  229. '<menuitem label="4 spaces" value="4"/>' +
  230. '<menuitem label="5 spaces" value="5"/>' +
  231. '<menuitem label="6 spaces" value="6"/>' +
  232. '<menuitem label="7 spaces" value="7"/>' +
  233. '<menuitem label="8 spaces" value="8"/>' +
  234. '</menupopup></menulist>' +
  235. '<checkbox id="options_showSelenese" label="Show Selenese"/>';
  236. this.name = "Perl Test::More(WebDriver)";
  237. this.testcaseExtension = ".t";
  238. this.suiteExtension = ".t";
  239. this.webdriver = true;
  240. WDAPI.Driver = function() {
  241. this.ref = options.receiver;
  242. };
  243. WDAPI.Driver.searchContext = function(locatorType, locator) {
  244. var locatorString = xlateArgument(locator).replace(/([@%$])/,"\\$1");
  245. switch (locatorType) {
  246. case 'xpath':
  247. return locatorString + ', "xpath"';
  248. case 'css':
  249. return locatorString + ', "css"';
  250. case 'id':
  251. return locatorString + ', "id"';
  252. case 'link':
  253. return locatorString + ', "link"';
  254. case 'name':
  255. return locatorString + ', "name"';
  256. case 'tag_name':
  257. return locatorString + ', "tag_name"';
  258. }
  259. throw 'Error: unknown strategy [' + locatorType + '] for locator [' + locator + ']';
  260. };
  261. WDAPI.Driver.prototype.back = function() {
  262. return this.ref + "->navigate->back";
  263. };
  264. WDAPI.Driver.prototype.close = function() {
  265. return this.ref + "->close";
  266. };
  267. WDAPI.Driver.prototype.findElement = function(locatorType, locator) {
  268. return new WDAPI.Element(this.ref + "->find_element(" + WDAPI.Driver.searchContext(locatorType, locator) + ")");
  269. };
  270. WDAPI.Driver.prototype.findElements = function(locatorType, locator) {
  271. return new WDAPI.ElementList(this.ref + "->find_elements(" + WDAPI.Driver.searchContext(locatorType, locator) + ")");
  272. };
  273. WDAPI.Driver.prototype.getCurrentUrl = function() {
  274. return this.ref + "->get_current_url";
  275. };
  276. WDAPI.Driver.prototype.get = function(url) {
  277. return this.ref + "->get(" + url + ")";
  278. };
  279. WDAPI.Driver.prototype.getTitle = function() {
  280. return this.ref + "->get_title";
  281. };
  282. WDAPI.Driver.prototype.refresh = function() {
  283. return this.ref + "->refresh";
  284. };
  285. WDAPI.Driver.prototype.frame = function(locator) {
  286. return this.ref + "->switch_to_frame(" + xlateArgument(locator) + ")";
  287. }
  288. WDAPI.Element = function(ref) {
  289. this.ref = ref;
  290. };
  291. WDAPI.Element.prototype.clear = function() {
  292. return this.ref + "->clear";
  293. };
  294. WDAPI.Element.prototype.click = function() {
  295. return this.ref + "->click";
  296. };
  297. WDAPI.Element.prototype.getAttribute = function(attributeName) {
  298. return this.ref + "->attribute(" + xlateArgument(attributeName) + ")";
  299. };
  300. WDAPI.Element.prototype.getText = function() {
  301. return this.ref + "->get_text";
  302. };
  303. WDAPI.Element.prototype.isDisplayed = function() {
  304. return this.ref + "->is_displayed";
  305. };
  306. WDAPI.Element.prototype.isSelected = function() {
  307. return this.ref + "->is_selected";
  308. };
  309. WDAPI.Element.prototype.select = function(selectLocator) {
  310. if (selectLocator.type == 'index') {
  311. return "$driver->find_child_element(" + this.ref +
  312. ", \"//option[" + selectLocator.string + "]\" , \"xpath\")->click";
  313. }
  314. if (selectLocator.type == 'value') {
  315. return "$driver->find_child_element(" + this.ref +
  316. ", \"//*[\\@value='" + selectLocator.string+ "']\" , \"xpath\")->click";
  317. }
  318. return "$driver->find_child_element(" + this.ref +
  319. ", \"//*[text()='" + selectLocator.string+ "']\" , \"xpath\")->click";
  320. };
  321. WDAPI.Element.prototype.sendKeys = function(text) {
  322. return this.ref + "->send_keys(" + xlateArgument(text) + ")";
  323. };
  324. WDAPI.Element.prototype.submit = function() {
  325. return this.ref + "->submit";
  326. };
  327. WDAPI.ElementList = function(ref) {
  328. this.ref = ref;
  329. };
  330. WDAPI.ElementList.prototype.getItem = function(index) {
  331. return this.ref + "[" + index + "]";
  332. };
  333. WDAPI.ElementList.prototype.getSize = function() {
  334. return this.ref + "->size";
  335. };
  336. WDAPI.ElementList.prototype.isEmpty = function() {
  337. return this.ref + "->is_empty";
  338. };
  339. WDAPI.Utils = function() {
  340. };
  341. WDAPI.Utils.isElementPresent = function(how, what) {
  342. return this.ref + "->is_element_present(" + xlateArgument(what) + ", \"" + how + "\")";
  343. };