Start Selenium server automatically for e2e testing - angularjs

I followed this SO post
to set up my Gruntfile. If I manually downloaded Selenium standalone and specified its location in the file, my test runs successfully. Since I would like to automate this process, I tried the following configuration:
protractor_webdriver: {
start: {
options: {
path: 'node_modules/grunt-protractor-runner/node_modules/protractor/bin/',
command: 'webdriver-manager start'
}
}
};
grunt.loadNpmTasks('grunt-protractor-webdriver');
grunt.registerTask('test', ['protractor_webdriver:start','protractor:run'])
Is there a way to avoid downloading manually? I tried the above but when I ran it, I got the warning:
Running "protractor_webdriver:start" (protractor_webdriver) task
Verifying property protractor_webdriver.start exists in config...OK
File: [no files]
Options: path="node_modules/grunt-protractor-runner/node_modules/protractor/bin/", command="webdriver-manager start", keepAlive=false
Starting Selenium server
>> Selenium Standalone is not present.
Install with webdriver-manager update --standalone
So I still need to download the selenium standalone server manually?
Or maybe I missed some configuration here?

Protractor is a wrapper around WebDriverJS.
It's a nodejs program that interacts with Selenium Server and specific Browser drivers (e.g. ChromeDriver, IEDriver).
So, without using selenium server (at least for IE), you cannot run tests written with protractor. Test scripts send commands to the Selenium Server, which in turn then communicates with the browser driver. See this for a description of the architecture.
In a nutshell, without having started a Selenium server instance beforehand, nothing will happen.

You can run Protractor without Selenium by specifying
directConnect: true
in your respective Protractor configuration file (e.g. protractor.conf.js).

Related

Invalid or corrupt jar file when trying to start selenium server from intelliJ

I am trying to start my selenium server from intelliJ and it is saying the file is corrupt.
I use intelliJ to run my protractor tests, and when I starts the selenium server from the intelliJ terminal before I run them. For whatever reason it is saying my jar file for selenium-server-standalone is coming back as corrupt. Is there a way to fix this and, if not, is there a way to delete and then reinstall selenium server?
C:\Users\pheonix.jones\IdeaProjects\attensity-q-protractor>webdriver-manager
start [11:39:09] I/start - java
-Dwebdriver.chrome.driver=C:\Users\pheonix.jones\AppData\Roaming\npm\node_modules\protractor\node_modules\webdriver-manager\selenium\chromedriver_2.46.exe
-Dwebdriver.gecko.driver=C:\Users\pheonix.jones\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\webdriver-manager\selenium\geckodriver-v0.24.0.exe
-jar C:\Users\pheonix.jones\AppData\Roaming\npm\node_modules\protractor\node_modules\webdriver-manager\selenium\selenium-server-standalone-4.0.0-alpha-1.zip.jar
-port 4444 [11:39:09] I/start - seleniumProcess.pid: 2748 Error: Invalid or corrupt jarfile
C:\Users\pheonix.jones\AppData\Roaming\npm\node_modules\protractor\node_modules\webdriver-manager\selenium\selenium-server-standalone-4.0.0-alpha-1.zip.jar
[11:39:09] I/start - Selenium Standalone has exited with code 1
As you can see, it is just exiting after it comes back as corrupt. I haven't had issues with this before. I did try to update the webdriver.
I found a workaround, which may or may not be acceptable. You can force webdriver-manager to use an older version of the selenium-standalone package. This avoids whatever the problem is with 4.0.0-alpha-1.
$ webdriver-manager clean
$ webdriver-manager update --standalone --versions.standalone=3.8.0
$ webdriver-manager start --versions.standalone=3.8.0
…
[14:19:55] I/start - seleniumProcess.pid: 63863
14:19:55.379 INFO - Selenium build info: version: '3.8.0', revision: '924c4067df'
14:19:55.380 INFO - Launching a standalone Selenium Server
2019-04-24 14:19:55.483:INFO::main: Logging initialized #390ms to org.seleniumhq.jetty9.util.log.StdErrLog
There is an open defect in the webdriver-manager project for this issue: https://github.com/angular/webdriver-manager/issues/370
As a temporary workaround you can use the directConnect attribute in your protractor config.
There is issue with 4.0.0-alpha-1 , after deleting files you can install some older versions that will work. If you will just update wevdriver manager by using webdriver-manager update then it will not work for you but you need to use something like " webdriver-manager update --standalone --versions.standalone="some older version"
Since you are using Protractor, you can try a webdriver-manager update
You should be starting the server like this - webdriver-manager start
I believe Protractor's webdriver-manager already contains the selenium standalone server.
Or you can just add this line in your conf file, and you will no longer need to manually start anything before running a Protractor spec file directConnect: true

Use npm protractor webdriver-manager to start selenium server at a different port

I am trying to run E2E tests for an angular application using protractor. I use the command ./node_modules/protractor/bin/webdriver-manager to start my selenium server. However the default selenium server location is localhost:4444/wd/hub, but localhost:4444 is already used on my machine and this is difficult to change. How do I start a selenium server at a port other than 4444?
Use below command :
webdriver-manager start --seleniumPort XXXX

Getting Selenium Going for Use with Protractor for my AngularJS app

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.

AnglularJS: Can Protractor run on Windows?

The angular doc recommends Protractor for e2etesting.
http://docs.angularjs.org/guide/dev_guide.e2e-testing
I didn't find any doc about running it on Windows.
Can Protractor run on Windows?
**
Update:
I have some experience on Selenium using C# code to call functions in some selenium dlls to control IE brower.
But what are the relationship between Protractor, Selenium and NodeJS?
Is Protractor an tool(exe) or a lib(dll) or a JS lib(like Jasmine)?
Yes, you can run it on windows.
I hope this guide will help you:
http://www.ng-newsletter.com/posts/practical-protractor.html
You should Start Selenium local server and use this basic configuration for run protractor (Depending routes):
chromeDriver: './node_modules/protractor/selenium/chromedriver',
specs: ['test/e2e/**/*_spec.js'],
chromeOnly: true,
chromeDriver: './node_modules/protractor/selenium/chromedriver',
seleniumAddress: 'http://0.0.0.0:4444/wd/hub'
Yes at least it should. It runs on node and node runs on windows. However sometimes projects have windows issues, the most common cause IMO is hardcoding of / etc.. If the library uses node path and similar os independent apis then all is fine.
I have ran protractor on windows without any issues. But not extensively.
Prerequisites & Installation:
Prerequisites
Node.js (Latest Version)
Download NodeJS
Install NodeJs
To test Node.Js installation properly open command prompt and execute the command.
Java Development Kit(JDK)
Download the JDK
Install JDK from
Installation
Install Protractor Globally
Open the command prompt and type following command to install protractor globally.
npm install -g protractor
This will install two command line tools, protractor and webdriver-manager. Try running below command to check protractor version.
protractor –version
The webdriver-manager is a helper tool to easily get an instance of a Selenium Server running. Use it to download the necessary binaries with:
webdriver-manager update
Now start up a server with:
webdriver-manager start

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

Resources