I'm delving into the world of Protractor tests for AngularJS.
All the tutorials recommend I execute the following after webdriver-manager update and prior to executing the test:
webdriver-manager start
According to the webdriver-manager man, the start command will 'start up the selenium server'. True enough, once I run the above command, I can see something at http://127.0.0.1:4444/wd/hub
My questions is: is the above necessary?
I currently run my tests without the above command.
All I do is:
webdriver-manager update
php -S localhost:8000 -t dist/
protractor ./test/protractor.config.js
My tests run as expected even though I've excluded webdriver-manager start.
Can someone please explain why webdriver-manager start is necessary?
:EDIT:
My protractor/fooTests.js:
exports.config = {
directConnect: true,
capabilities: {
'browserName': 'chrome'
},
specs: ['protractor/fooTests.js'],
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
My protractor/fooTests.js:
describe('test for the bar code', function() {
it('should login', function() {
browser.get('http://localhost:8000');
element(by.model('password')).sendKeys('123456');
element(by.css('[type="submit"]')).click();
});
it('should inspect element ', function() {
expect(element(by.id('foo-script')).isPresent()).toBe(true);
console.log('Login Success');
});
});
Protractor is sending commands to Selenium and Selenium is communicating with the browsers using its drivers.
webdriver-manager start
is starting Selenium.
There are 3 basic options:
directConnect. That makes protractor communicate with the selenium drivers directly, without using the Selenium server. However, the functionality of this option is limited:
directConnect: true - Your test script communicates directly Chrome Driver or Firefox Driver, bypassing any Selenium Server. If this is true, settings for seleniumAddress and seleniumServerJar will be ignored. If you attempt to use a browser other than Chrome or Firefox an error will be thrown.
Connecting to an already running selenium server (either local or remote), specified by seleniumAddress. A server can be started using the webdriver-manager start script.
Starting the server from the test script.
You can explore all the options in the documentation https://github.com/angular/protractor/blob/master/docs/server-setup.md
Related
When I issue the protractor command at the command line, with the following configuration:
'use strict';
// Protractor configuration
var config = {
specs: ['test/e2e/*spec.js']
};
if (process.env.TRAVIS) {
config.capabilities = {
browserName: 'firefox'
};
}
exports.config = config;
I get this:
$ protractor
[12:22:23] I/launcher - Running 1 instances of WebDriver
[12:22:23] I/local - Starting selenium standalone server...
[12:22:24] I/local - Selenium standalone server started at http://10.0.0.242:55414/wd/hub
Started
.
1 spec, 0 failures
Finished in 8.223 seconds
[12:22:33] I/local - Shutting down selenium standalone server.
[12:22:33] I/launcher - 0 instance(s) of WebDriver still running
[12:22:33] I/launcher - chrome #01 passed
the problem is that it takes 5+ seconds to start up the "selenium standalone server".
Two questions - (1) do I need this server to run the tests? And (2), is there a way to run the server in the background without having to restart the server everytime?
You also use selenium server jar in the protractor configuration. Checkout the sample bellow. this also comes in handy while using phantojs.
seleniumServerJar: '../utils/selenium-server-standalone-2.53.1.jar',
seleniumPort: 4444,
make user seleniumAddress is commented in the config
Do you use the protractor DirectConnect option? If so, you can also use the standalone webdriver-manager. Protractor also uses it as a dependency.
For local development I installed it as a global with npm install webdriver-manager -g, then update it with webdriver-manager update and start it with webdriver-manager start. It will then run on the background on the default port 4444, run webdriver-manager to see all the options.
You then don't need to start the webdriver for each test / suite.
Hope it helps
INFOS
Node v4.4.7
Protractor v4.0.2
Gulp CLI version 3.9.1
Gulp Local version 3.9.1
Chromedriver chromedriver_2.22.exe
SeleniumServerJar selenium-server-standalone-2.53.1.jar
Chrome Browser version=52.0.2743.82 m
SYSTEM INFO:
host: 'win10', ip: '**', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0',
java.version: '1.8.0_102'
I am using the Bamboo server to run my Protractor e2e tests via gulp and the tests PASS but at the very end throw this error: "UnknownError: Error communicating with the remote browser. It may have died."
I have tried adding the following code snippet at the very end of my feature step JavaScript code as a way to tell Protractor to NOT end prematurely and wait for Angular to finish all of its asynchronous work but this attempt also failed:
return browser.waitForAngular().then(function () {
console.log('\n Waiting for angular to avoid this issue:
UnknownError: Error communicating with the remote browser. It may have died.');
browser.driver.sleep(10000);
});
Also notice there's a sleep in there, still didn't work.
This error only happens when I'm executing my e2e tests via the Bamboo agent in our Windows VM; however if I run same test locally on the command prompt, they pass with no problem.
Lastly when my bamboo agent runs these e2e tests in my Windows10 VM, I do NOT see a browser open so looks like they're executing headlessly see protractor config below:
exports.config = {
framework: 'cucumber',
chromeDriver: "C:/Users/someUser/AppData/Roaming/npm/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.22.exe",
seleniumServerJar: "C:/Users/someUser/AppData/Roaming/npm/node_modules/protractor/node_modules/webdriver-manager/selenium/selenium-server-standalone-2.53.1.jar",
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--no-sandbox']
}
},
Notice there's no PhantomJs browser as it's discouraged by the Protractor team, they don't use it themselves; so I use Chrome it seems to run headlessly because I never see it open up but I know the test are running from the log file output, see image below and notice how the test PASSED but the Bamboo test plan failed with that error in image below.
Any ideas on this?
I'm developing an AngularJS app and want to do end-2-end testing with Protractor. I would like to benefit from the suite of test browsers available at Browserstack and run the tests on Browserstack Automate instead of a local Selenium server.
How do I set up a system to run these tests?
NOTE: These instructions are only relevant for Protractor versions older than v3.0. Protractor 3.0 includes built-in support for Browserstack.
Prerequisites
You will need to have node and npm installed. Check your node version with node --version to ensure it is greater than v0.10.0.
Ready?
1. Install Protractor
Use npm to install Protractor globally with:
npm install -g protractor
If you get errors, you might need to run the above command as sudo.
Here's a more detailed tutorial for installing and using Protractor.
2. Install Browserstack Selenium web driver
EDIT: #elgalu pointed out in the comments this step is not necessary. Apparently the BrowserStackLocal tunnel (set up in step 4) is enough.
Following Browserstack's instructions for setting up node.js, install the seleniun web driver:
npm install -g browserstack-webdriver
3. Set up Protractor configuration
Create a protractor.conf.js file (see documentation for BrowserStack's supported capabilities):
exports.config = {
capabilities: {
'browserstack.user' : 'my_user_name',
'browserstack.key' : 'my_secret_key',
// Needed for testing localhost
'browserstack.local' : 'true',
// Settings for the browser you want to test
// (check docs for difference between `browser` and `browserName`
'browser' : 'Chrome',
'browser_version' : '36.0',
'os' : 'OS X',
'os_version' : 'Mavericks',
'resolution' : '1024x768'
},
// Browserstack's selenium server address
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
// Pattern for finding test spec files
specs: ['test/**/*.spec.js']
}
Change your user name and secret key to the ones given on the Browserstack Automate page. If you're logged in to Browserstack, the instructions for setting up node.js will have you user and key substituted in the examples, and you can just copy-paste the javascript from there.
The same page also has a tool for generating the code for different test browser settings.
4. Download and run BrowserStackLocal
Download the BrowserStackLocal binary from the node.js instructions page.
Make the following changes to the command below, and run the binary to open the Browserstack tunnel required for testing.
Change your secret key in the command. Again, your_secret_key will be automatically substituted in the node.js guide, if you're logged in to Browserstack.
Change the port number to match the port you're hosting your AngularJS files at on localhost. The example uses port 3000.
./BrowserStackLocal your_secret_key localhost,3000,0
5. Run the tests
With everything ready for testing, run your tests:
protractor protractor.conf.js
You can watch the test run on Browserstack Automate and even see an updating live screenshot of the test browser.
Protractor from version 3.0.0 onwards has added inbuilt support for BrowserStack.
You simply need to add the following two parameters in your conf.js to launch the test on BrowserStack:
browserstackUser: '<username>'
browserstackKey: '<automate-key>'
Your username and automate key can be found here, after you have logged in to your account.
Hence, lets say you wish to run your test on Chrome 50 / OS X Yosemite, your conf.js should look something like this:
exports.config = {
specs: ['spec.js'],
browserstackUser: '<username>',
browserstackKey: '<automate-key>',
capabilities: {
browserName: 'Chrome',
browser_version: '50.0',
os: 'OS X',
os_version: 'Yosemite'
},
};
If you wish to run tests in parallel on different browser and OS combinations, you can use the multiCapabilities as given below:
exports.config = {
specs: ['spec.js'],
browserstackUser: '<username>',
browserstackKey: '<automate-key>',
multiCapabilities: [
{
browserName: 'Safari',
browser_version: '8.0',
os: 'OS X',
os_version: 'Yosemite'
},
{
browserName: 'Firefox',
browser_version: '30.0',
os: 'Windows',
os_version: '7'
},
{
browserName: 'iPhone',
platform: 'MAC',
device: 'iPhone 5S'
}
]
};
Some helpful links:
Code Generator - Helps you configure the capabilities to test on different various browser and OS combinations especially mobile devices.
Sample Github project for Protractor-BrowserStack - This should help you get started.
Hello! To only run the test against Browserstack, you may need to skip Step 4 from Niko Nyman answer, and in your conf.js you should have something like here's the one that I've used (+ report), then run Step 5:
var HtmlReporter = require('protractor-html-screenshot-reporter');
var reporter=new HtmlReporter({
baseDirectory: './protractor-result', // a location to store screen shots.
docTitle: 'Report Test Summary',
docName: 'protractor-tests-report.html'
});
// An example configuration file.
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome',
'version': '22.0',
'browserstack.user' : 'user_name',
'browserstack.key' : 'user_key',
'browserstack.debug' : 'true'
},
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['./specs/home_page_spec.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
},
onPrepare: function() {
jasmine.getEnv().addReporter(reporter);
}
};
We just wrote a blog post and open-source modules for this.
http://www.blog.wishtack.com/#!Cross-browser-testing-test-automation-with-Protractor-and-Browserstack/cuhk/554b3b5f0cf27313351f1163
wt-protractor-boilerplate
wt-protractor-runner
wt-protractor-utils
I'm building an AngularJS app on Windows. I want to create end-to-end tests with Jasmine. From my understanding, I need protractor to run these kinds of tests. For that reason, I've added the following to my package.json:
"devDependencies": {
...
"grunt-protractor-runner": "0.2.4",
"selenium-webdriver":"2.41.0",
...
}
In my gruntfile.js, I've configured Protractor as such:
grunt.initConfig({
protractor: {
options: {
configFile: "node_modules/protractor/referenceConf.js", // Default config file
keepAlive: true, // If false, the grunt process stops when the test fails.
noColor: false, // If true, protractor will not use colors in its output.
args: {
// Arguments passed to the command
}
},
tests: {
options: {
configFile: "tests/config/e2e.conf.js",
args: {} // Target-specific arguments
}
},
}
});
I'm then running the protractor:tests target. The contents of e2e.conf.js look like the following:
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Spec patterns are relative to the location of the spec file. They may
// include glob patterns.
specs: ['../../tests/e2e/user-tests.e2e.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
}
};
Now, when I run grunt from the command-line, I get an error that says:
Using the selenium server at http://localhost:4444/wd/hub
C:\Projects\MyProject\node_modules\grunt-protractor-runner\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1702
throw error;
^
Error: ECONNREFUSED connect ECONNREFUSED
...
I don't understand why I'm getting this error. I see in the Protractor Getting Started Guide that it expects a selenium standalone server to be running. However, I thought that was the purpose of the Grunt task runner: start the selenium server. I see webdriver-manager in node_modules\grunt-protractor-runner\node_modules\protractor\bin, however, if I change to that directory and run webdriver-manager update from the command-line, I get an error that says:
'webdriver-manager' is not recognized as an internal or external command,
operable program or batch file.
How do I get the selenium piece going so that I can run end-to-end tests with protractor?
Thank you!
Have you tried with "node" command ?
It worked for me :
node .\node_modules\grunt-protractor-runner\node_modules\protractor\bin\webdriver-manager update
This should download for you chromedriver and selenium server. If not, ou can also manually download/extract :
http://selenium-release.storage.googleapis.com/2.40/selenium-server-standalone-2.40.0.jar
https://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
in :
src/test/config/selenium/selenium-server-standalone.jar
src/test/config/selenium/chromedriver.exe
An other thing is that you need to be sure that Chrome is isntalled in :
<WINDOWS_USERS_FOLDER>\<USERNAME>\AppData\Local\Google\Chrome\Application\chrome.exe
First up:
There are many components in there that I don't understand :)
Feel free to add comments to help me improve my answer.
I do know some Selenium so here goes.
seleniumAddress: 'http://localhost:4444/wd/hub',
To me, this line indicates that you are trying to run the Selenium Grid Server.
Here's how to start it: (yes, it needs to be started manually as far as I know)
From a separate console, run the following:
java -jar selenium-server-standalone-2.41.0.jar -role hub
#now wait a few seconds for the hub to start
java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://localhost:4444/grid/register
What did we just do?
Started a hub. This hub is like a test distributor - it receives the requests.
Started a node. (You can start any number of nodes). This node is what will actually conduct the test.
You can verify that this server was started successfully. Just visit the local host link on your browser.
Gotchas:
Check that your firewall is not giving you problems. I've had crippling issues getting started on Windows 7 and finally moved over to Ubuntu (but that's probably just my situation).
Open ports 4444 (for hub), 5555(for node) for both incoming and outgoing connections on Windows Firewall.
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