Protractor: Error: ReferenceError: describle is not defined - automation

I tried to run protractor test but there is error shows up on the command prompt. I put the spec file and config file at the same directory. Below is the content of the file.
spec.js
describle("Freelance Website", function () {
it("Sign Up", function (){
browser.get("https://www.freelancer.com/");
});
});
config.js
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js']
}
I run the protractor test with following command protractor conf.js. What is the problem? How to solve it?

please correct the spelling of describe. Refer the below code
describe("A suite is just a function", function() {
var a;
it("and so is a spec", function() {
a = true;
expect(a).toBe(true);
});
});

Related

Nightwatch, How to call 'Setup' and 'Teardown' API globally

I am just getting started learning nightwatchjs to test my webapp. I need to call a 'setup' and a 'teardown' script before and after everything else, respectively. These scripts create the necessary conditions in the database for the tests to run (creating test licenses, users, etc), and remove them afterward. I have an API call in my app that triggers these.
In globals.js, you can set before and after methods that should execute before and after everything else, and also a beforeEach and afterEach that should execute before and after each test suite, if I'm not mistaken. It seems the beforeEach and afterEach methods accept as arguments a browser object and a done callback. The before and after methods, however, only get the done callback.
In a particular test suite, the same four methods can be added, but in this case, the before and after run before everything and after everything, respectively, and the beforeEach and afterEach run before and after each individual test in the suite.
Ideally, I would call my setup and teardown scripts in the global before and after methods, but those do not get an instance of nightwatch (browser), so I don't know how I'd do that.
I am able to fire off the 'setup' call just fine, in my globals beforeEach method. This isn't ideal for me, but it would still work, it would just be a but superfluous (calling it before each test suite rather than just once before everything).
However, I run into issues when I try to call the teardown script from my globals afterEach method. When I run my test, the output hangs at the end, until I hit CTRL+C.
Here is my nightwatch.conf.js:
const seleniumServer = require("selenium-server");
const chromedriver = require("chromedriver");
// we use a nightwatch.conf.js file so we can include comments and helper functions
module.exports = {
"src_folders": [
"tests",
],
"page_objects_path": './pages',
"globals_path": "./globals.js",
"output_folder": "./reports", // reports (test outcome) output by nightwatch
"custom_commands_path" : "./commands",
"selenium": {
"start_process": true, // tells nightwatch to start/stop the selenium process
"server_path": seleniumServer.path,
"host": "127.0.0.1",
"port": 4444, // standard selenium port
"cli_args": {
"webdriver.chrome.driver" : chromedriver.path
}
},
"test_settings": {
"default": {
"silent": true,
"launchUrl": 'http://local.mytestwebsite.com',
"screenshots": {
"enabled": true, // if you want to keep screenshots
"path": "./screenshots/" // save screenshots here
},
"globals": {
"waitForConditionTimeout": 5000 // sometimes internet is slow so wait.
},
"desiredCapabilities": { // use Chrome as the default browser for tests
"browserName": "chrome",
"chromeOptions": {
"args": [
"window-size=1366,768",
"--incognito"
]
}
}
}
}
}
Here is my globals.js:
var chromedriver = require('chromedriver');
module.exports = {
beforeEach: function(browser, done) {
console.log('Executing the global `beforeEach`');
browser.url('http://www.vulfpeck.com');
browser.expect.element('body').to.be.present;
browser.end();
// getting the session info
browser.status(function(result) {
console.log("Session Info: ", result.value);
done();
});
},
afterEach: function(browser, done){
console.log('Executing the global `afterEach`');
browser.url('http://www.vulfpeck.com');
browser.expect.element('body').to.be.present;
browser.end();
// getting the session info
browser.status(function(result) {
console.log("Session Info: ", result.value);
done();
});
},
before: function(done) {
console.log('Executing the global `before`');
console.log('starting the chromedriver');
chromedriver.start();
done();
},
after: function(done) {
console.log('Executing the global `after`');
console.log('stoppin the chromedriver');
chromedriver.stop();
done();
}
};
And here is my test suite:
module.exports = {
'Test 1': function(browser){
browser.url('http://www.google.com');
browser.expect.element('body').to.be.present;
browser.end();
},
'Test 2': function(browser){
browser.url('http://www.vulfpeck.com');
browser.expect.element('body').to.be.present;
browser.end();
},
before: function(browser, done){
console.log('test before');
done();
},
beforeEach: function(browser, done){
console.log('test beforeEach');
done();
},
after: function(browser, done){
console.log('test after');
done();
},
afterEach: function(browser, done){
console.log('test afterEach');
done();
}
};
Here is the output.
$ nightwatch
Executing the global `before`
starting the chromedriver
Starting selenium server... started - PID: 11772
[Login] Test Suite
======================
Executing the global `beforeEach`
√ Expected element <body> to be present - element was present in 31ms
Session Info: { ready: true,
message: 'Server is running',
build:
{ revision: '63f7b50',
time: '2018-02-07T22:42:28.403Z',
version: '3.9.1' },
os: { arch: 'amd64', name: 'Windows 10', version: '10.0' },
java: { version: '9' } }
test before
Running: Test 1
test beforeEach
√ Expected element <body> to be present - element was present in 30ms
test afterEach
OK. 1 assertions passed. (3.56s)
Running: Test 2
test beforeEach
√ Expected element <body> to be present - element was present in 20ms
test afterEach
OK. 1 assertions passed. (2.503s)
test after
Executing the global `afterEach`
Session Info: { ready: true,
message: 'Server is running',
build:
{ revision: '63f7b50',
time: '2018-02-07T22:42:28.403Z',
version: '3.9.1' },
os: { arch: 'amd64', name: 'Windows 10', version: '10.0' },
java: { version: '9' } }
× Expected element <body> to be present - element was not found - expected "present" but got: "not present"
at Object.afterEach (C:\sites\mytestwebsite.com\selenium\globals.js:29:21)
So, am I missing something? Why can I not hit a URL in the global afterEach method without it hanging? (I just figured out that if I remove the browser.end(); from my last test in the suite, my global afterEach will work just fine. Why is this?). Is there some other recommended way of hitting a URL before and after running all tests?
Thanks! Any help is appreciated!
While I haven't completely answered my own question, I have come up with an alternate way to call my setup and teardown scripts in the globals before and after hooks.
For this, I have installed the node libarary child_process (by typing npm install child_process), which allows me to execute commands on the command line.
In my globals.js, I am requiring that library at the top:
const { exec } = require('child_process');
And in my before and after hooks, I am using it like so:
exec('php ../run.php api selenium setup', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.log("node couldn't execute the command",err);
done();
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
done();
});
Hope this helps someone.

Gulp-protractor not starting webdriver-manager

I'm trying to run protractor automatically using gulp-protractor plugin. This whole process works fine when using protractor commands and explicitly running the web drivers individually.
The same works when running using gulp-protractor, provided ie webdriver is started manually in background before triggering the gulp task.
Below is the code snippet of my Gulp task
var protractor = require("gulp-protractor").protractor;
var webdriverupdate = require("gulp-protractor").webdriver_update;;
var webdriver_standalone = require("gulp-protractor").webdriver_standalone;
// This task is to update & run the webdriver
gulp.task('webdriver_standalone', webdriver_standalone);
gulp.task('webdriverUpdate', ['webdriver_standalone'], function () {
browsers: ['chrome', 'ie']
});
//for running protractor E2E test cases
gulp.task('protractor', function (callback) {
gulp
.src(['./e2e/sanity/shared/*.spec.ts',
'./e2e/sanity/app-header/*.spec.ts',
])
.pipe(protractor({
'configFile': 'Protractor.conf.js',
}))
.on('error', function (e) {
console.log(e);
})
.on('end', callback);
});
gulp.task('default',['webdriverUpdate','protractor']);
Below is the code snippet of my protractor.config.js
const { SpecReporter } = require('jasmine-spec-reporter');
exports.config = {
allScriptsTimeout: 1100,
suites: {
shared: ['./e2e/sanity/shared/*.ts'] ,
appheader: ['./e2e/sanity/app-header/*.spec.ts']
},
multiCapabilities:
[{
seleniumAddress: 'http://localhost:5555/',
'browserName': 'internet explorer',
'platform': 'windows 10',
'version': '11',
'ignoreProtectedModeSettings': true,
},
{
seleniumAddress: 'http://localhost:4444/wd/hub',
'browserName': 'chrome',
'ignoreProtectedModeSettings': true,
}],
};
How do I run webdriver standalone task ahead of protractor task via gulp??
I used gulp with gulp-angular-protractor like below, hope this helps. It will work for gulp-protractor plugin as well.
//Gulpfile
var gulp = require('gulp');
var gulpProtractor = require('gulp-angular-protractor');
var paths = require('../paths.js');
// Execute e2e Tests
gulp.task('e2e-test', function(callback) {
gulp.src(paths.tests)
.pipe((gulpProtractor({
configFile: 'protractor.conf.js'
})).on('error', function(e) {
console.log(e);
}).on('end', callback));
});
gulp.task('webdriver-update', gulpProtractor.webdriver_update);
gulp.task('webdriver-standalone', ['webdriver-update'], gulpProtractor.webdriver_standalone);
//paths.js:
module.exports = {
tests: 'test/e2e/**/*.spec.js'
};

Pass parameters for each browser using Protractor

Just got started with Protractor for E2E testing.
I want to pass parameters (login and password) for each instance of chrome selenium server.
I want test the same spec file with different user account in parallel.
This is my conf.js :
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--disable-web-security']
},
count: 10
},
You can use the onPrepare-method of Protractor for that. If multiple capabilities are being run, this will run once per capability. You can add data to the browser-object that you can use during your execution.
What you can do is something like this
// A JSON file or something
var login = {
"chrome": {
"user": "usernameChrome",
"pass": "passwordChrome"
},
"firefox": {
"user": "usernameFirefox",
"pass": "passwordFirefox"
}
};
// in your config
// An example configuration file.
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
multiCapabilities: [{
'browserName': 'chrome'
},
{
'browserName': 'firefox'
}
],
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['example_spec.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
},
onPrepare: function() {
return browser.getCapabilities()
.then((capabilities) => {
// Get the current browser you are using
browser.browserName = capabilities.get('browserName').toLowerCase();
// Add the user and pass to the browser-object
browser.user = login[browser.browserName].user;
browser.pass = login[browser.browserName] pass;
});
}
};
// In your spec
describe('logon', function() {
it('should logon', function() {
browser.get('http://www.example.com');
element(by.model('user')).sendKeys(browser.user);
element(by.model('pass')).sendKeys(browser.pass);
element(by.tagName('button')).click();
});
});
You can handle this with Protractor's params on the command line. For example, you can start each test with a different username/password like this:
protractor conf.js --params.username user1 --params.password password1
Then, in your test, you would use them something like this:
logIntoMyApp(browser.params.username, browser.params.password);
You can also set defaults in your config file (see the docs for details).

How to override the preconfigured casper instance in a test environment

i have installed the phantomjs 1.9.7 & casperjs 1.1.0 and now i see that all my scripts need to be updated and to remove the first line since it makes the below fatal error:
Fatal: you can't override the preconfigured casper instance in a test environment.
Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options
Reading also in another question i understand that i have to remove now this line from my scripts:
var casper = require('casper').create();
Which is a lot of work since i have meanwhile more then 200 script that needs to be updated!!!
So my question is how can i overcome this issue without updating all the scripts (mentioned in Can't run the testing framework in CasperJS)
var casper = require('casper').create();
if (casper.cli.has("ip") === false) {
casper.echo("Usage: casper.js test get_info.js --ip=<x.x.x.x>").exit();
}
casper.test.begin('get info', 1, function suite(test) {
casper.start("http://" + casper.cli.get("ip") + ":xxxx/man/", function() {
test.assertTitle("TEST GUI", "GUI has the correct title");
test.assertExists('form[name="loginForm"]', "login form is found");
this.fill('form[name="loginForm"]', {
'userId': 'xxxxx',
'password': 'yyyyy'
}, true);
});
casper.then(function() {
test.assertTextExists("Welcome", "Login into TEST GUI");
this.click('a#test_menu.menu1itemUnSel[tabindex="4"]');
});
casper.then(function() {
casper.wait(5000, function() {
this.echo('should appear after 5s');
});
});
casper.then(function() {
test.assertTextExists("TEST Data", "Login into Data");
this.click('a#test_menu_data.menu2itemUnSel[tabindex="4"]');
});
casper.then(function() {
casper.wait(5000, function() {
this.echo('should appear after 5s');
});
});
casper.then(function() {
test.assertTextExists("Logout", "Loggin out from TEST GUI");
this.click('a.minorLinkshighlight');
});
casper.run(function() {
test.done();
this.exit();
});
});
The above script was working before i installed the phantomjs 1.9.7 & casperjs 1.1.0, the only thing that i can't recall is which version i had before my server had to be reinstalled!
Thanks in adv.
Yes this is my way of avoiding updating old casperjs scripts due to the overriding of the preconfigured casper instance in a test environment!
go to the casperjs path and edit in path modules:
/../casperjs/modules/casper.js
and remark out the below lines:
exports.create = function create(options) {
"use strict";
// This is a bit of a hack to check if one is trying to override the preconfigured
// casper instance from within a test environment.
// if (phantom.casperTest && window.casper) {
// console.error("Fatal: you can't override the preconfigured casper instance in a test environment.");
// console.error("Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options");
// phantom.exit(1);
// }
return new Casper(options);
};
This worked for me. You should use it only on your own risk!

Debugging protractor test in intellij idea

I have project and I use protractor test. But I would like use debugging but I don't know how to create config for him.
It is my protractor.conf.js
'use strict';
var paths = require('./.yo-rc.json')['generator-gulp-angular'].props.paths;
exports.config = {
capabilities: {
'browserName': 'chrome'
},
specs: [paths.e2e + '/**/*.js'],
mochaOpts: {
timeout: 5000
},
framework: 'mocha'
};
And e2e-tests.js gulp file:
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
module.exports = function(options) {
gulp.task('webdriver-update', $.protractor.webdriver_update);
gulp.task('webdriver-standalone', $.protractor.webdriver_standalone);
function runProtractor (done) {
gulp.src(options.e2e + '/**/**.js')
.pipe($.protractor.protractor({
configFile: 'protractor.conf.js'
}))
.on('error', function (err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
})
.on('end', function () {
// Close browser sync server
browserSync.exit();
done();
});
}
gulp.task('protractor', ['protractor:src']);
gulp.task('protractor:src', ['serve:e2e', 'webdriver-update'], runProtractor);
gulp.task('protractor:dist', ['serve:e2e-dist', 'webdriver-update'], runProtractor);
};
Help me please fix this issue because write code without quick watch and other good components not very well.