Protractor E2E Testing Error: Cannot find module 'selenium-webdriver' - angularjs

Error: Cannot find module 'selenium-webdriver'
I have installed protractor and selenium-webdriver globally using npm install -g protractor webdriver-manager update
var webdriver = require('selenium-webdriver');
describe('modes of failure', function() {
it('should fail to find a non-existent element', function() {
browser.get('index.html#/form');
// Run this statement before the line which fails. If protractor is run
// with the debugger (protractor debug debugging/conf.js), the test
// will pause after loading the webpage but before trying to find the
// element.
browser.debugger();
// This element doesn't exist, so this fails.
var nonExistant = element(by.binding('nopenopenope')).getText();
});
it('should fail to use protractor on a non-Angular site', function() {
browser.get('http://www.google.com');
}, 20000);
it('should fail an assertion', function() {
browser.get('index.html#/form');
var greeting = element(by.binding('{{greeting}}'));
expect(greeting.getText()).toEqual('This is not what it equals');
});
});

You need to install the node module:
npm i selenium-webdriver --save-dev

Did you try to remove the var webdriver = require('selenium-webdriver'); ?
You shouldn't need it, you can access it in your test via browser
browser this is the a wrapper around an instance of webdriver. Used for navigation and page-wide information.
(quoted from Getting started - Protractor docs)
If you've already installed the Selenium standalone server using webdriver-manager update, and started it with webdriver-manager start, all you have to do is to run your tests using
protractor path/to/your/protractor-conf.js

When you type:
webdriver-manager
Into the terminal, the these helpful options appear:
webdriver-manager
Usage: webdriver-manager <command>
Commands:
update: install or update selected binaries
start: start up the selenium server
status: list the current available drivers
Have you tried update, start, or status?

Try using the standalone option:
$ webdriver-manager start --standalone

Related

Difference between command run ->'protractor protractor.conf.js' and 'npm run e2e '

I want to know the exact difference between this two command when we run those using angular Cli project terminal-
protractor protractor.conf.js' --> Basically it launches baseurl provided in protractor.conf.js instead of using localhost
'npm run e2e ' --> Basically it does not launches baseurl provided in protractor.conf.js instead it launches localhost
I am confused while running e2e test -like which command to use and if dont want use #command 1 how it will launch basurl
thanks for your help
the e2e in npm run e2e comes from the e2e inscripts in your package.json.
So npm run e2e actually executed command ng e2e which comes from Angular CLI,
ng e2e generally will do two things:
Compile and start your app at local
execute protractor protractor.conf.js to the app started at Step 1,
It will ignore the baseUrl in protractor.conf.js, only respect the url
where app started at Step 1
If you want ng e2e respect the baseUrl in protractor conf file, you need to start app before execute ng e2e and append option --no-serve:
ng e2e --no-serve

Newbie to Selenium E2E with React

I created a boilerplate React project, packages.json has the usual suspects:
prestart
start
list
test
etc.
I am using Selenium for my E2E framework. I have the following test:
it('should launch a browser', () => {
const By = webDriver.By;
let driver = new webDriver.Builder()
.forBrowser('chrome')
.build();
// verify Continue button exist on page
driver.navigate().to('http://localhost:3000').then(() => driver.findElement(By.id('submitButton')).getAttribute('value'))
.then(buttonValue => expect(buttonValue).toEqual('Continue'));
});
If I do npm start, my site launches and my E2E launches an additional Chrome browser and navigate to my running site: localhost:3000. The test succeeds.
My question is, how do I run my E2E separately, without the need to my site side by side using npm start.
I am newbie to React and Selenium, in case I am missing a lot of information on this post, I apologize in advance.
Well, since you didn't find the time to update the question information with the NPM "scripts" object, then I'll try to give it a shot in the dark.
First of all, due to your wording, I can interpret your question two ways:
a.) you want to run your E2E tests separately, w/o your server running (which is started via npm start);
b.) you want to run your E2E tests via npm start, without triggering your server from starting;
a.) If you want to run your scripts separately, seeing as you are using Mocha, then you can trigger them via: ./node_modules/.bin/mocha <pathToTests>/<testFile>.
Now, since you stated in your question that you're using npm test script, then that should be the best switch to bind your E2E tests execution to:
package.json (Scripts object):
"scripts": {
"test": "mocha --reporter spec <pathToTests>/<testFile>",
"start": "node <yourServerName>.js"
},
Please note that mocha <pathToTests>/<testFile> is equivalent to ./node_modules/.bin/mocha <pathToTests>/<testFile>, because NPM looks for binaries inside node_modules/.bin and when Mocha was installed, it installed it into this directory.
Note: Many packages have a bin, or .bin section, declaring scripts that can be called from NPM similar to Mocha. If you want to find out what other binaries you can run that way, just issue a ls node_modules/.bin.
b.) In this care, I think your issue might be due to NPM defaulting some script values based on package contents. Specifically, if you have a server.js file in the root of your package, then npm will default the start command to server.js.
So if you're starting your E2E tests via npm start, having this ("start": "mocha <pathToTests>/<testFile>") in your package.json and there is a server.js file in the root of your package, then npm will default the start command to node server.js.
In which case, you could either move your server script to another place in the project, or change the switch you're using to trigger the E2E tests (see section b.)).
Hope this solves your problem and if not, looking forward for that package.json "scripts" object so we can really see what's up. :)
Cheers!

ng2-bootstrap. Errors during e2e tests

Trying to run e2e tests for ng2-bootstrap and have a couple of issues. Interested if this is jut me or not.
Here's what I'm doing:
Download the latest from
https://github.com/valor-software/ng2-bootstrap
run npm-install
run npm run build
run npm run link
and finally run nmp run e2e
On the first run I got an error: SpecReporter is not a constructor which I was able to fix by replacing const SpecReporter = require('jasmine-spec-reporter'); in protractor.conf.js to const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
But than there's another error: Cannot find namespace 'webdriver' which I can't figure out how to fix.
So if someone could just repeat these steps above an let me know if you're getting the same results I would really appreciate it.
I have created an issue on github about this: https://github.com/valor-software/ng2-bootstrap/issues/1665
try import * as webdriver from 'selenium-webdriver';

Protractor tests reports using jasmine-reporters

I'm trying to export protractor test results to xml files, for that I have installed jasmine-reporters using
npm install -g jasmine-reporters.
Protractor version is Version 2.1.0.
jasmine-reporters version 2.0.7
This is my protracotr config file:
exports.config = {
seleniumAddress: 'http://localhost:4455/wd/hub',
capabilities: {
'browserName': 'chrome'
},
specs: [
'student_spec.js'
],
onPrepare: function() {
require('jasmine-reporters');
jasmine.getEnv().addReporter(
new jasmineReporters.JUnitXmlReporter(null, true, true, '/test/e2e/JasmineReporter')
);
},
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 50000
}
};
When I run the protractor, I am getting this error
Error: Cannot find module 'jasmine-reporters'
Help me, where I'm doing wrong.
Make sure you have installed jasmine-reporters and the proper path of jasmine-reporters is provided. If it was installed properly then run the below command to see if you get the version of it -
npm list -g jasmine-reporters
If there was a problem installing it, use below command to install it which is compatible with Jasmine 2.x versions -
npm install --save-dev jasmine-reporters#^2.0.0
Update your conf.js file to include proper global scope variable jasmineReporters as mentioned in the package file -
framework: 'jasmine2',
onPrepare: function() {
var jasmineReporters = require('path_of_installed_jasmine-reporters-plugin');
//update proper path, in my case its ('/usr/local/lib/node_modules/jasmine-reporters')
jasmine.getEnv().addReporter(
new jasmineReporters.JUnitXmlReporter(null, true, true, '/test/e2e/JasmineReporter')
);};
Did you try:
var jasmineReporters = require('jasmine-reporters').jasmineReporters;
?
I think something like that should work.
Best regards
I also got the same issue as I installed jasmine locally.But when I installed jasmine as Globally it works fine for me.
npm install –g jasmine-reporters#^2.0.0
then,
npm list -g jasmine-reporters
After that run the conf.js again as mentioned above

How to run Protractor

I'm new to AngularJS. I'm trying to learn and do some end-to-end tests with Protractor. I've been going through the information provided here. I'm stuck when I get to the part that says "Run with protractor myConf.js".
Is protractor a command-line program? Or what? What I'm trying to identify is, under what environment do I need to run "protractor myConf.js" within. I do NOT want to install protractor globally. I would like to run the module in a local context. Is that an option?
Thank you,
You need to run it through node.
So from the base of your project;
node node_modules\protractor\bin\protractor test\myConf.js
You can install Protractor globally via:
$ npm install -g protractor
Afterwards it should be available on the command line (Windows/Linux)
$ protractor protractor.conf.js
To install just for the current project:
$ npm install protractor --save-dev
It can be run via the node_modules like this (Windows/Linux):
$ ./node_modules/.bin/protractor protractor.conf.js
You can add it to your package.json for easier running:
"scripts": {
"test": "./node_modules/.bin/protractor protractor.conf.js"
}
Then later:
$ npm test
These are the getting started docs:
https://github.com/angular/protractor/blob/master/docs/getting-started.md
You need to have node.js installed on your machine, as well as the npm node package. Once you have those two things installed you can follow the rest of the directions in the docs above.
It should only take about 5-10 mins of installation before you have Protractor up and running. Let me know if you're still stuck.
You should use npm-run-all (or concurrently, parallelshell), because it has more control over starting and killing commands.
Once npm-run-once, protractor, http-server installed locally, you can modify package.json like that:
scripts: {
"webdriver-start": "./node_modules/protractor/bin/webdriver-manager update && ./node_modules/protractor/bin/webdriver-manager start",
"protractor": "./node_modules/protractor/bin/protractor ./tests/protractor.conf.js",
"http-server": "./node_modules/http-server/bin/http-server -a localhost -p 8000",
"python-example": "python -m SimpleHTTPServer",
"test1": "npm-run-all -p -r webdriver-start http-server protractor",
"test2": "npm-run-all -p -r webdriver-start python-example protractor"
}
-p = Run commands in parallel.
-r = Kill all commands when one of them finishes with zero.
Running npm run test1 will start Selenium driver, start http server (to serve you files) and run protractor tests. Once all tests are finished, it will close the http server and the selenium driver.
Here is example using Typescript, but if it is not your case, you can simply remove all 'tsc' stuff. Configure your package.json scripts section to look like this:
"scripts": {
"postinstall": "node node_modules/protractor/bin/webdriver-manager update",
"pretest": "npm run tsc",
"test": "npm run eslint && npm run protractor",
"eslint": "node node_modules/eslint/bin/eslint.js '*.js' 'test/**/*.js' 'test/**/*.ts'",
"protractor": "node node_modules/protractor/bin/protractor",
"start": "node node_modules/protractor/bin/webdriver-manager start",
"tsc": "node node_modules/typescript/bin/tsc"
}
and run npm start in one terminal and npm test in another one.
I have a code generator that creates an empty protractor project. The instructions should be easy to follow:
https://npmjs.org/package/generator-protractor
I think the best way to run protractor is by installing it local to your project and then run it with npm scripts.
Backing up one step, npm itself uses a hierarchy based on the file system to find executable modules. If you type npm bin npm will tell you the first place it is going to look to find something executable (e.g. [project]/node_modules/.bin). If you include protractor in your package.json, when you do an npm install, protractor will add a symlink in your .bin directory for both protractor and webdriver-manager.
There are a number of ways you can use this information to execute protractor. The ~~correct~~ best way I think though is to use npm scripts. When you use npm scripts npm will automatically load protractor from the local .bin directory.
Here's an example
package.json
{
"name": "awesomeapp",
"version": "1.0.0",
"devDependencies": {
"protractor": "latest"
},
"scripts": {
"test-e2e": "protractor protractor.conf",
"selenium": "webdriver-manager start"
}
}
So now you can run your selenium server with npm run selenium then run your protractor tests with npm run test-e2e.
This is also cross platform so if you are using mac or Windows you'll be covered either way.
NOTE: You can do stuff in these scripts that are not cross platform (npm docs) so if being cross platform is important to you and you want to do anything fancy I would recommend using shelljs.
P.P.S. Didn't want to clutter the point above but npm also has pre and post hooks so you can update and run selenium with one command.
"scripts": {
"preselenium": "webdriver-manager update",
"selenium": "webdriver-manager start"
}
Theoretically Windows should support && so you could also do this but ymmv...
"scripts": {
"selenium": "webdriver-manager update && webdriver-manager start"
}
If you have any suite in config.js try this
suites: {
TESTCASES_RELATED_TO_SUITE1: ['TEST_CASES/Suite1/**/*spec.js'],
TESTCASES_RELATED_TO_SUITE2: ['TEST_CASES/Suite2/**/*spec.js']
},
The tests can be executed from command line as below:
<path>protractor config.js --suite TESTCASES_RELATED_TO_SUITE1
This will only execute one test suite.
Yes, it is possible to run protractor by using the following command :
npm install protractor
and you can then access it by :
./node_modules/.bin/protractor conf.js
For more info visit : Protractor - end-to-end testing for AngularJS . This a very good place to start.
I'm using IntelliJ for protractor tests. Also note that for this, IntelliJ Ultimate Edition is required along with node.js and protractor installations.
You can find the details here Protractor Setup on IntelliJ
First you need to install the node.js from https://nodejs.org/en/download/ and then install protractor using "npm install -g protractor". This will install the protractor globally. I faced issues when I installed protractor locally. Better try to install it globally.
Or you can provide all your dependencies in the package.json file like below:
{
"dependencies": {
"protractor": "4.0.3",//any latest versions of these.
"protractor-jasmine2-screenshot-reporter": "0.3.2",
"jasmine-terminal-reporter": "1.0.3"
},
"scripts": {
"postinstall": "node node_modules\\protractor\\bin\\webdriver-manager update"
}
}
There are dependencies for the reporting of the test results as well in the above package.json file. And you need to run webdriver-manager update. It is a helper tool to get an instance of a selenium server running.
You can put everything in the package.json file and run "npm install" to install all the dependencies. This will create a "node_modules" folder for you.
Now create a configuration file ex: conf.js. This can be something like this.
// An example configuration file. There are the reporters which are used to give the test results. There are many reporters. You can use which ever is convenient. The below reporters are for example to show how to configure them.
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var JasmineTerminalReporter = require('jasmine-terminal-reporter');
//To get the Current Date and Time. To make the test output more clear, you can give date.
var currentDate = new Date(),
currentHoursIn24Hour = currentDate.getHours(),
month = currentDate.getMonth() + 1,
totalDateString = currentDate.getDate() + '-' + month + '-' + currentDate.getFullYear() +
'-' + currentHoursIn24Hour + 'h-' + currentDate.getMinutes() + 'm';
var htmlReporter = new HtmlScreenshotReporter({
pathBuilder: function (currentSpec, suites, browserCapabilities) {
'use strict';
return currentSpec._suite.description + totalDateString + '/' + browserCapabilities.get('browserName') + '/' + currentSpec.description;
},
dest: 'TestOutput',
cleanDestination: false,
showSummary: true,
showQuickLinks: true
});
exports.config = {
directConnect: true,//If you make this flag true, it connects the browser directly.
capabilities: {
'browserName': 'chrome'
},
//this is to bring up the test dependencies. Some kind of setup. run once
beforeLaunch: function () {
'use strict';
return new Promise(function (resolve) {
htmlReporter.beforeLaunch(resolve);
});
},
//once per capabilities.
onPrepare: function () {
jasmine.getEnv().addReporter(htmlReporter);
jasmine.getEnv().addReporter(new JasmineTerminalReporter({
isVerbose: true,
showColors: true
}));
},
//A callback function called once all tests have finished running and
// the WebDriver instance has been shut down.
afterLaunch: function (exitCode){
return new Promise(function(resolve){
htmlReporter.afterLaunch(resolve.bind(this, exitCode));
});
},
getPageTimeout: 120000,
allScriptsTimeout: 120000,
specs: ['../TestScripts/*.js']//This contains the test files.
};
Once you are done with the setup, create a test file. The tests are written using jasmine framework which contains "describe" and "it". "describe" will hold "it " which has the tests in it. You can go through this: http://www.protractortest.org/#/
Now run the test with "protractor conf.js". This will run the tests and generate the reports as well in the TestOutput folder which we have set up in the configuration file.
Here we have a complete beginners tutorial: Protractor for beginners video
Getting Started
We looked upon Protractor with Chrome headless, multiple browsers integrating with Sauce Labs.
Lets look at the test automation execution reports, how we can integrate it.
awesome QA
Pre requisite
npm is distributed with Node.js- which means that when you download Node.js, you automatically get npm installed on your computer.
1. Install nodejs
First, install protractor globally on your system:
install protractor as a development dependency:
2. run npm install -g Protractor
3. run npm install protractor --save-dev
To install and start the standalone Selenium Server manually, use the webdriver-manager command line tool, which comes with Protractor.
This will install the server and ChromeDriver.
4. run npm install -g webdriver-manager
5. run updated webdriver-manager
This will start the server. You will see a lot of output logs, starting with INFO. The last line will be 'Info - Started org.openqa.jetty.jetty.Server'.
5. run start webdriver-manager
Leave the server running while you conduct your test sessions.
In your config file, set seleniumAddress to the address of the running server. This defaults to http://localhost:4444/wd/hub.
6. Finally run your script - Protractor<location of your config file>conf.js
Build and Test
Checkout github : https://github.com/shahing/Protractor-Web-Automation

Resources