PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/public/funcunit/selenium/selenium.js

https://gitlab.com/vladRakin/javascriptmvc-site
JavaScript | 198 lines | 180 code | 9 blank | 9 comment | 15 complexity | 3c91b61acb82259fd26921b1ba58b92e MD5 | raw file
  1. steal('steal/browser', function(){
  2. steal.browser.selenium = function(options){
  3. // kill the server in case it was running from before
  4. this.killServer();
  5. steal.browser.call(this, options, 'selenium')
  6. this._startServer();
  7. this.clientPath = "funcunit/selenium";
  8. }
  9. steal.extend(steal.browser.selenium, {
  10. defaults: {
  11. serverPort: 4444,
  12. serverHost: "localhost"
  13. },
  14. trigger: function(){
  15. browser.trigger.apply(self, arguments);
  16. }
  17. });
  18. steal.browser.selenium.prototype = new steal.browser();
  19. steal.extend(steal.browser.selenium.prototype, {
  20. /**
  21. * Opens the browser, at each of the specified browsers, one by one
  22. * @param {Object} page
  23. */
  24. open: function(page){
  25. this.page = this._getPageUrl(page);
  26. this._browserStart();
  27. this._poll();
  28. // block until we're done
  29. this.browserOpen = true;
  30. while(this.browserOpen) {
  31. java.lang.Thread.currentThread().sleep(1000);
  32. }
  33. return this;
  34. },
  35. _loadDriverClass: function() {
  36. var URLClassLoader = Packages.java.net.URLClassLoader,
  37. URL = java.net.URL,
  38. File = java.io.File,
  39. ss = new File("funcunit/selenium/selenium-java-client-driver.jar"),
  40. ssurl = ss.toURL(),
  41. urls = java.lang.reflect.Array.newInstance(URL, 1);
  42. urls[0] = new URL(ssurl);
  43. var clazzLoader = new URLClassLoader(urls),
  44. mthds = clazzLoader.loadClass("com.thoughtworks.selenium.DefaultSelenium").getDeclaredConstructors(),
  45. rawMeth = null;
  46. //iterate through methods to find the one we are looking for
  47. for ( var i = 0; i < mthds.length; i++ ) {
  48. var meth = mthds[i];
  49. if ( meth.toString().match(/DefaultSelenium\(java.lang.String,int,java.lang.String,java.lang.String\)/) ) {
  50. constructor = meth;
  51. }
  52. }
  53. return function( serverHost, serverPort, browserStartCommand, browserURL ) {
  54. var host = new java.lang.String(serverHost),
  55. port = new java.lang.Integer(serverPort),
  56. cmd = new java.lang.String(browserStartCommand),
  57. url = new java.lang.String(browserURL);
  58. return constructor.newInstance(host, port, cmd, url);
  59. };
  60. },
  61. _startServer: function(){
  62. //first lets ping and make sure the server is up
  63. var port = this.options.serverPort,
  64. addr = java.net.InetAddress.getByName(this.options.serverHost)
  65. try {
  66. var s = new java.net.Socket(addr, port)
  67. }
  68. catch (ex) {
  69. spawn(function(){
  70. var jarCommand = 'java -jar '+
  71. 'funcunit/selenium/selenium-server-standalone-2.12.0.jar'+
  72. ' -userExtensions funcunit/selenium/user-extensions.js';
  73. if (java.lang.System.getProperty("os.name").indexOf("Windows") != -1) {
  74. var command = 'start "selenium" ' + jarCommand;
  75. runCommand("cmd", "/C", command.replace(/\//g, "\\"))
  76. }
  77. else {
  78. var command = jarCommand + " > selenium.log 2> selenium.log &";
  79. runCommand("sh", "-c", command);
  80. }
  81. })
  82. var timeouts = 0,
  83. started = false;
  84. var pollSeleniumServer = function(){
  85. try {
  86. var s = new java.net.Socket(addr, port)
  87. started = true;
  88. }
  89. catch (ex) {
  90. if (timeouts > 20) {
  91. print("Selenium is not running. Please use js -selenium to start it.")
  92. quit();
  93. } else {
  94. timeouts++;
  95. }
  96. }
  97. }
  98. while(!started){
  99. java.lang.Thread.currentThread().sleep(1000);
  100. pollSeleniumServer();
  101. }
  102. }
  103. this.DefaultSelenium = this._loadDriverClass();
  104. },
  105. killServer: function(){
  106. if (java.lang.System.getProperty("os.name").indexOf("Windows") != -1) {
  107. runCommand("cmd", "/C", 'taskkill /fi "Windowtitle eq selenium" > NUL')
  108. } else { // mac
  109. runCommand("sh", "-c", "ps aux | awk '/selenium\\// {print$2}' | xargs kill -9")
  110. }
  111. },
  112. // create new selenium instance, start it, open page, set FuncUnit.mode = "Selenium", start polling for data
  113. _browserStart: function(){
  114. this.selenium = this.DefaultSelenium(this.options.serverHost,
  115. this.options.serverPort,
  116. FuncUnit.browserName,
  117. this.page);
  118. this.selenium.start();
  119. this.selenium.open(this.page);
  120. selThreads.resumePolling();
  121. },
  122. close: function(data){
  123. selThreads.pausePolling();
  124. this.selenium.close();
  125. this.selenium.stop();
  126. selThreads.stopPolling();
  127. this.killServer();
  128. this.browserOpen = false;
  129. },
  130. evaluate: function(fn){
  131. var txt = fn.toString().replace(/\n|\r\n/g,""),
  132. evalText = "Selenium.evaluate('"+txt+"')";
  133. var res = this.selenium.getEval(evalText);
  134. res = ""+res;
  135. return res;
  136. },
  137. injectJS: function(file){
  138. var scriptText = readFile(file).replace(/\n|\r\n/g,""),
  139. evalText = "Selenium.injectJS('"+escape(scriptText)+"')";
  140. this.selenium.getEval(evalText);
  141. return true;
  142. },
  143. _poll: function(){
  144. selThreads.poll(this);
  145. }
  146. });
  147. var keepPolling = false,
  148. stopPolling = false;
  149. var selThreads = {
  150. poll : function(self){
  151. this.resumePolling();
  152. spawn(function(){
  153. var stop = selThreads.getResult(self);
  154. if(!stop){
  155. java.lang.Thread.currentThread().sleep(350);
  156. arguments.callee();
  157. }
  158. })
  159. },
  160. pausePolling : sync(function(){
  161. keepPolling = false;
  162. }),
  163. resumePolling : sync(function(){
  164. keepPolling = true;
  165. }),
  166. stopPolling : sync(function(){
  167. stopPolling = true;
  168. keepPolling = false;
  169. }),
  170. getResult : sync(function(self){
  171. if(!keepPolling){
  172. return stopPolling;
  173. }
  174. var resultJSON,
  175. res,
  176. evt;
  177. resultJSON = self.selenium.getEval("Selenium.getResult()");
  178. eval("res = "+resultJSON);
  179. if(res && res.length){
  180. for (var i = 0; i < res.length; i++) {
  181. evt = res[i];
  182. self.trigger(evt.type, evt.data);
  183. }
  184. }
  185. return stopPolling;
  186. }),
  187. keepPolling : false
  188. }
  189. })