Using PhantomJS with Karma (Win7 x64) - angularjs

Does anyone have a simple getting started guide on how to configure Karma to use PhantomJS?
Using the phonecat sample, I have Karma running with Chrome fine and although the Karma docs mention PhantomJS (which I now have installed) I can't figure out how to amend the config file to get it to run.
I've tried putting PhantomJS in the the browsers array of testacular.conf.js but I get;
{ [Error: spawn OK] code: 'OK', errno: 'OK', syscall: 'spawn' }
Which I think means it's launching OK but it appears to me that (as a PhantomJS noob) it requires a different command line. I've also downloaded phantomjs-launcher but it's not obvious how to use that.
(I'm running Windows 7 64-Bit if that makes a difference.)
test.bat
#echo off
REM Windows script for running unit tests
REM You have to run server and capture some browser first
REM
REM Requirements:
REM -NodeJS (http://nodejs.org/)
REM -Testacular (npm install -g karma)
set BASE_DIR= % ~dp0
karma start "%BASE_DIR%\..\config\testacular.conf.js" %*
testacular.conf.js
basePath = '../';
files =[
JASMINE,
JASMINE_ADAPTER,
'app/lib/angular/angular.js',
'app/lib/angular/angular-*.js',
'test/lib/angular/angular-mocks.js',
'app/js/**/*.js',
'test/unit/**/*.js'
];
autoWatch = true;
browsers =['Chrome', 'phantomjs'];
junitReporter = {
outputFile: 'test_out/unit.xml',
suite: 'unit'
};
According to procmon.exe PhantomJS wasn't launching at all, so to circumvent environmental issues, I've since amended my config thus;
browsers = ['Chrome','%USERPROFILE%\\AppData\\Roaming\\npm\\phantomjs.cmd'];
where %userprofile% is expanded, which seems to launch it, but now I get this;
INFO [launcher]: Starting browser %USERPROFILE%\AppData\Roaming\npm\phantomjs.cmd
ERROR [launcher]: Cannot start %USERPROFILE%\AppData\Roaming\npm\phantomjs.cmd
Can't open 'http://localhost:9876/?id=16572367'
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn OK
at errnoException (child_process.js:975:11)
at Process.ChildProcess._handle.onexit (child_process.js:766:34)
That error seems to be coming from PhantomJS.exe now.

First, install PhantomJS using npm:
npm install -g phantomjs
You may then need to specify the location of the PhantomJS executable for karma. The npm install will tell you where it put the executable. For me, running karma in Git Bash, I added the following to ~/.profile:
export PHANTOMJS_BIN ='C:/Users/JohnSmith/AppData/Roaming/npm/node_modules/phantomjs/lib/phantom/phantomjs.exe'`
Those two steps, plus adding PhantomJS to the browsers entry, were sufficient to get karma to successfully invoke Phantom and run all of my tests.

Use PhantomJS launcher and set env PHANTOMJS_BIN to the correct location of your phantomjs binary. That is phantomjs.exe on windows, not the .cmd file (the cmd file is just a npm wrapper on windows).
In your code, you are using a script browser launcher (a custom shell script to launch a browser). That is possible, but the script has to accept a single argument which is the url that it should open. PhantomJS does not behave like that.

I was seeing this error when using a relative path in node's child_process.spawn() function --
var spawn = require('child_process').spawn;
var child = spawn('phantomjs', ['./suspendmonitors.js']);
The solution for me was to use the absolute path:
var spawn = require('child_process').spawn;
var child = spawn('phantomjs', ['C:/Users/kkhalsa/workspace/misc_scripts/phantomjs/suspendmonitors.js']);
For some reason, the relative path worked when calling the node script from a windows command prompt, but didn't when invoking the node script in Windows powershell.

Are you using cmd.exe or Powershell?
Try manually adding the PHANTOMJS_BIN and point it to the phantomjs.exe, not the .cmd.

Related

I am getting issue phantomjs and karma

I am facing issue while running grunt, it throwing me following issue:
ERROR [launcher]: PhantomJS2 failed 2 times (timeout). Giving up.
DEBUG [karma]: Run complete, exiting.
DEBUG [launcher]: Disconnecting all browsers
Warning: Task "karma:continuous" failed. Use --force to continue.
I tried to run npm install phantomjs
Which is successful. After that i run npm install as well but again getting above issue
This is because you will need to run the PhantomJS standalone version for karma to connect to it.
I would suggest a better option according to my opinion, which is to use karma-phantomjs2-launcher. You can get that by: npm install --save-dev karma-phantomjs2-launcher.
Please note that it is not a stable version. If it suits your requirement, you can use karma-phantomjs-launcher, which works perfectly fine. But it does not have support for PhantomJS 2.
I got solution for this, if we get this error so first need to check karm.conf.js file, there you have to search for key browsers and in that section you have to see whether PhantomJS2 is defined or not.It would be something like this:
browsers : [
'PhantomJS2'
],
In my case it was there but still i was getting error, i then checked my hosts file which you can find in C:\Windows\System32\drivers\etc if you are using windows machine and i placed
127.0.0.1 localhost and there i go.... its worked for me.

How to set the environment variable CHROME_BIN when working in an IDE?

I'm trying to follow the AngularJS tutorial: https://docs.angularjs.org/tutorial. I use the IDE Cloud9.
When I enter npm test it returns No binary for Chrome browser on your platform. Please, set "CHROME_BIN" env variable. And the same message for FIREFOX_BIN.
How am I supposed to set these environment variables?
I've tried the following:
In the command prompt of Windows (on my pc, not in cloud9) I entered SET CHROME_BIN = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" but this didn't make a difference.
I also tried it when changing karma.conf.js to:
browsers : ['C:/Program Files (x86)/Google/Chrome/Application/chrome.exe', 'C:/Program Files (x86)/Mozilla Firefox/firefox.exe'],
but then I got the error Can not load "Script", it is not registered! Perhaps you are missing some plugin? so that doesn't seem to be the right way to go either.
If in Cloud9 I enter env SET CHROME_BIN = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" it returns env: SET: No such file or directory.
I also ran into the same problem using the Cloud9 IDE.
While, I didn't solve the Chrome problem, I went with PhantomJS, which worked with Cloud9.
To install on the command line:
npm install karma-phantomjs-launcher --save-dev
In the karma.conf.js file:
// start these browsers
browsers: ['PhantomJS'],
Add variable for the current user using powershell:
[Environment]::SetEnvironmentVariable("CHROME_BIN",'...', "User")
This is not exactly the same as what you want as variable will not be temporary but the effect will be the same.
Your own command will work if you execute it in the same shell as npm:
PS> $Env:CHROME_BIN = '...'
PS> npm test
...
I am using here Powershell syntax exclusively.
I am not familiar with Cloud9 but generally, to set variable up for the IDE you would need to run it from the script that sets the variable prior to launching the IDE (the same as I showed you with nmp above).

Unable to run Protractor - ECONNREFUSED connect ECONNREFUSED

I'm trying to learn AngularJS. As part of this, I want to learn to use end-to-end testing. Currently, I have a directory structure like this:
node_modules
.bin
...
protractor
...
node_modules
.bin
adam-zip
glob
minijasminenode
optimist
saucelabs
selenium-webdriver
protractor
config.js
src
tests
test.e2e.js
My config.js file looks like the following:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
specs: [
'../src/tests/test.e2e.js'
],
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
test.e2e.js looks like the following:
'use strict';
describe('My Sample', function () {
driver = protractor.getInstance();
beforeEach(function () {
driver.get('#/');
});
it('My First Test', function () {
message = "Hello.";
expect(message).toEqual('World.');
});
});
When I attempt to run my end-to-end tests using protractor, I run the following command from the command-line:
node_modules\.bin\protractor protractor\config.js
When I run that command, I receive the following error:
C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1542
throw error;
^
Error: ECONNREFUSED connect ECONNREFUSED
at ClientRequest.<anonymous> (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver\http\index.js:12
7:16)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1528:9)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
==== async task ====
WebDriver.createSession()
at Function.webdriver.WebDriver.acquireSession_ (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriv
er\lib\webdriver\webdriver.js:130:49)
at Function.webdriver.WebDriver.createSession (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver
\lib\webdriver\webdriver.js:110:30)
at Builder.build (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver\builder.js:105:20)
at runJasmineTests (C:\Src\MyProject\node_modules\protractor\lib\runner.js:191:45)
at C:\Src\MyProject\node_modules\protractor\lib\runner.js:255:5
at C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\base.js:1178:15
at webdriver.promise.ControlFlow.runInNewFrame_ (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriv
er\lib\webdriver\promise.js:1438:20)
at notify (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:328:12)
at then (C:\Src\MyProject\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:377:7)
What am I doing wrong?
I solved this with --standalone flag:
webdriver-manager start --standalone
I got it working by removing the following line from my config.js
seleniumAddress: 'http://localhost:4444/wd/hub',
Are you running a selenium server? The git README states the following:
WebdriverJS does not natively include the selenium server - you must start a standalone selenium server. All you need is the latest selenium-server-standalone.
source: https://github.com/angular/protractor
The error message is due to the following:
[ECONNREFUSED] The attempt to connect was ignored (because the target is not listening for connections) or explicitly rejected.
Check the URL of the Webdriver manager. The default URL is:
http://localhost:4444/wd/hub
Use a background process to run the webdriver-manager, then run protractor:
Start-Process webdriver-manager start -passthru
protractor conf.js
This will start up a Selenium Server and will output a bunch of info logs. Your Protractor test will send requests to this server to control a local browser. Leave this server running
References
Protractor Tutorial
Protractor Docs: Config File Reference
CONNECT Man Page
POSIX Man Page
For me, this had happened due to incompatible versions of Node and Protractor.
My fix-
Update Node to latest version (v7.0.0 in my case)
Follow steps given here https://stackoverflow.com/a/19333717/1902831
Install latest protractor version (4.0.10 in my case) using:
npm install -g protractor
Open another terminal and execute these command:
webdriver-manager update
webdriver-manager start
Run tests in another terminal using:
protractor conf.js
If you are using the npm protractor-webdriver grunt plugin (https://www.npmjs.org/package/grunt-protractor-webdriver) you may exeprience same kind of error.
This is due to webdriver termination just before test ends. The test runs successfully and then you have a message like :
Session deleted: Going to shut down the Selenium server
Shutting down Selenium server: http://127.0.0.1:4444
Shut down Selenium server: http://127.0.0.1:4444 (OKOK)
d:\Projets\Clouderial\nodeProjects\cld-apps\node_modules\grunt-protractor-runner\node_modules\protractor\node_modules\selenium-webdriver\http\index.js:145
callback(new Error(message));
^
Error: ECONNREFUSED connect ECONNREFUSED
at ClientRequest.<anonymous> (d:\Projets\Clouderial\nodeProjects\cld-apps\node_modules\grunt-protractor-runner\node_modules\protractor\node_modules\selenium-webdriver\http\index.js:145:16)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1547:9)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:419:13)
I resolve this using the keepAlive option in the grunt plugin.
Here is my Gruntfile.js config :
protractor_webdriver: {
options: {
keepAlive : true // True to keep the webdriver alive
},
start: {
},
},
...
I hope this will help someone.
JM.
I also faced the same problem,the trick that worked for me is to use two cmd windows,keeping the one open after typing webdriver-manager start and without pressing enter key(if enter key is pressed the selenium server is shutdown,don't know why) open another cmd window and call your tests.
#Alexandros Spyropoulos, it took me quite some time to figure out how to run protractor and I think we had the same problem. You should open one terminal tab and run webdriver-manager start --standalone. Then open another terminal tag and run protractor ***.conf.js
In the hopes that it might help someone: I'd been having the same problem - encountering ECONNREFUSED using grunt-protractor-runner. The nuance to my case is that I was running my entire E2E environment (test files, web application and entire backend) within a Docker container.
I tried running protractor
with and without additional grunt-protractor-webdriver task to get webdriver up and running 'manually' (no difference);
with and without enabling directConnect and keepAlive settings (bypassing Selenium and resulting in crashes related to Chromedriver, one of which was described here).
The solution was rather simple: increase the amount of memory allocated to the container. On my Windows 10 host machine, I performed the following steps:
Run VBoxManage.exe modifyvm default --memory 8192 (via custom shell script) before starting the docker-machine (via Docker Quickstart script, which is equivalent to docker-machine start). (Thanks to this SO answer).
Changing my shell script to run my default container, adding the --shm-size=4G argument to my docker run command. (See docs)
You can verify if it worked by running df -h in your guest machine, by checking the amount of memory mounted on /dev/shm.
As a result, I no longer have seemingly inexplicable errors such as ECONNREFUSED.
If you run the provided protractor demo, you should try running the protractor config in the same command prompt as selenium. Try running both selenium server and protractor separately.
Make Sure first selenium runs by following command.
webdriver-manager start --standalone
And run the protractor in a separate command window.
protractor conf.js
(In my case conf.js was the config file )
I faced a similar issue to the one #David Remie faced with the Selenium Docker grid/standalone. With minimal RAM/CPU, the tests would start before the webdriver was up. A less resource consuming approach is to wait a few seconds before testing (run 'sleep 5' or something like that).
Increasing RAM was sometimes enough as a workaround for the issue, but the real problem was that the Selenium CMD (/opt/bin/entry_point.sh, starts a supervisor that runs the webdriver) from the image based on https://hub.docker.com/r/selenium/node-base/dockerfile was taking time to start the Selenium webdriver.
webdriver-manager start ----- didn't help, But below one helped
webdriver-manager start --standalone

Access error running chrome from grunt test

I am trying to run karma tests on my angular project. I used yeoman to create the angular project. However when I am trying to run tests, I am getting following errors
grunt test
Running "clean:server" (clean) task
Running "concurrent:test" (concurrent) task
Running "coffee:dist" (coffee) task
Running "coffee:test" (coffee) task
Done, without errors.
Running "connect:test" (connect) task
Started connect web server on localhost:9000.
Running "karma:unit" (karma) task
INFO [karma]: Karma server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern "/home/arpit/proj/proj1/views/test/mock/**/*.js" does not
match any file.
ERROR [launcher]: Cannot start Chrome
Error: spawn EACCES
INFO [launcher]: Trying to start Chrome again.
ERROR [launcher]: Cannot start Chrome
Error: spawn EACCES
INFO [launcher]: Trying to start Chrome again.
ERROR [launcher]: Cannot start Chrome
Error: spawn EACCES
Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
I can run chrome from bash using same account successfully with which I am trying to run grunt test
Also, sudo grunt test is running fine.
On the Mac, it's important to set CHROME_CANARY_BIN
to binary file, not just to app package.
In my case, problem emerged from installing Chrome via brew cask, so I had to set
export CHROME_CANARY_BIN="/opt/homebrew-cask/Caskroom/google-chrome-canary/latest/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
In most cases you have to set a environment variable "CHROME_BIN" to point at your chrome.exe
Windows:
set CHROME_BIN=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Mac 10.7.5
I was able to set it using the following
export CHROME_BIN=/Applications/Chrome.app/Contents/MacOS/Google\ Chrome
You get to the exec file by right clicking on the .app and selecting the Show Package Contents option. Follow this down to the executable and click it. It brought up the path in the terminal.

Angularjs tutorial step2 test chrome not start with Error write EIO

My pc is Windows7 32bit.
When I test Angularjs tutorial step2.
Chrome not start with error.
--- versions ---
Angularjs:1.0.2
testacular:0.4.0
--- messages ----
info: Testacular server started at http://localhost:9876/
info (launcher): Starting browser chrome
error (launcher):
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: write EIO
at errnoException (net.js:770:11)
at Object.afterWrite (net.js:594:19)
Refer: https://github.com/angular/ang...
start scripts/test.sh (on windows: scripts\test.bat)
In the Git Shell / command window, go to the angular-phonecat directory. Enter this command
scripts\test.bat
Please note it test.bat and not .sh file.
You can see that the server
started but it may throw error that Chrome cannot be started and it is unable to find the path. I too was stopped at this point and anyhow after several reference, got this solution.
Open config/testacular.conf.js in some editor. Change the
browsers = ['Chrome'] to
browsers = ['C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'];
(This is where my Chrome is installed in Windows 8). You may need to get the correct installation path along with the chrome.exe in path and should use forward-slash
otherwise it will not work.
It is working for me now!

Resources