Cucumber + Protractor - timed out error while executing the steps - selenium

I am using cucumber and protractor to write behavioural driven tests.My scenarios and all the steps will pass but at the end it will show timed out error. The homepage will get loaded for the first step and later it will not perform any steps described in the steps definition file. Once the page is loaded it should click on the tabs. I have mentioned these steps in step definition file.But these steps are not executed and it will show all the steps passed in the console. I followed this link for reference https://semaphoreci.com/community/tutorials/getting-started-with-protractor-and-cucumber
This is the error message
Please find the sample code below.
//sample.feature
Feature: The Dashboard has 2 tabs, Statistics and Results
Scenario: I want to have 2 tabs with Displayed text "Statistics" and "Results" on the homepage
Given I go to Dashboard homepage
And I click on the "#/results"
Then the Results page is displayed
And I click on the "#/Statistics" tab
Then the Statistics page is displayed
//menu.steps.js
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
module.exports = function() {
this.Given(/^I go to Dashboard homepage$/, function() {
browser.get('http://localhost:8100/#/');
browser.waitForAngular();
});
this.Then(/^I click on the "([^"]*)"$/,function(arg1){
element(by.css('[href="#/results"]')).click();
});
this.Then(/^the results page is displayed$/, () => {
browser.get('http://localhost:8100/#/results');
});
this.When(/^I click on the "([^"]*)" tab$/, function(arg1) {
element(by.css('[href="#/statistics"]')).click();
});
this.Then(/^the statistics page is displayed$/, () =>{
browser.get('http://localhost:8100/#/statistics');
});
//cucumber.conf.js
exports.config = {
framework: 'custom', // set to "custom" instead of cucumber.
frameworkPath: require.resolve('protractor-cucumber-framework'),
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['test/e2e/cucumber/*.feature'],
capabilities: {
'browserName': 'firefox',
},
baseUrl: 'http://localhost:8100/#/',
// cucumber command line options
cucumberOpts: {
require: ['test/e2e/cucumber/*.steps.js'], // require step definition files before executing features
tags: [], // <string[]> (expression) only execute the features or scenarios with tags matching the expression
strict: true, // <boolean> fail if there are any undefined or pending steps
format: ["pretty"], // <string[]> (type[:path]) specify the output format, optionally supply PATH to redirect formatter output (repeatable)
dryRun: false, // <boolean> invoke formatters without executing steps
compiler: [] // <string[]> ("extension:module") require files with the given EXTENSION after requiring MODULE (repeatable)
},
onPrepare: function () {
browser.manage().window().maximize(); // maximize the browser before executing the feature files
},
resultJsonOutputFile: './test/e2e/results.json'
}

If you are using CucumberJS, you can choose to use callbacks or promises. In your code, you didn't use one of them. Below, you will find an example of your steps how you can use promises, of callbacks.
Be aware of the fact that you if you implement one of the 2 solutions, your tests could still fail, but that has more to do with the test implementation instead of the solution for callbacks / promises
// With promises
module.exports = function() {
this.Given(/^I go to Dashboard homepage$/, function() {
browser.get('http://localhost:8100/#/');
return browser.waitForAngular();
});
this.Then(/^I click on the "([^"]*)"$/, function(arg1) {
return element(by.css('[href="#/results"]')).click();
});
this.Then(/^the results page is displayed$/, () => {
return browser.get('http://localhost:8100/#/results');
});
this.When(/^I click on the "([^"]*)" tab$/, function(arg1) {
return element(by.css('[href="#/statistics"]')).click();
});
this.Then(/^the statistics page is displayed$/, () => {
return browser.get('http://localhost:8100/#/statistics');
});
}
// With callbacks
module.exports = function() {
this.Given(/^I go to Dashboard homepage$/, function(done) {
browser.get('http://localhost:8100/#/');
browser.waitForAngular().then(done);
});
this.Then(/^I click on the "([^"]*)"$/, function(arg1, done) {
element(by.css('[href="#/results"]')).click().then(done);
});
this.Then(/^the results page is displayed$/, (done) => {
browser.get('http://localhost:8100/#/results').then(done);
});
this.When(/^I click on the "([^"]*)" tab$/, function(arg1, done) {
element(by.css('[href="#/statistics"]')).click().then(done);
});
this.Then(/^the statistics page is displayed$/, (done) => {
browser.get('http://localhost:8100/#/statistics').then(done);
});
}

Related

Protractor - How to use tags on Jasmine for e2e testning

I'm having some issues where I am stuck on what to do to be able to tag different tests that I want to run.
The tests that I want is only required at the end where I want to be able to tag which payment I would like to pay which is Mastercard, Visa or Amex. I do have a test that does the details page such as writing users information, choose shipment but then at the end I do have multiply options that I would like to test pending on what I want to test:
paymentPage.js
describe('Payment page', function () {
paymentPage = new PaymentPage();
// The details page is accessible by the specified URL
it(`Credit Card - Has a form that can receive user data`, async function () {
await paymentPage.creditCardPayment();
});
//Visa
it('Enets payment', async function () {
//TODO
});
//Amex
it('Enets payment', async function () {
//TODO
});
});
As you can see there is 3 options that I would like to test so whenever I do etc "protractor e2e run mastercard" so that means it should run the first test case, if I choose visa then do the second test case and skip the rest basically.
However I do have couple of tests that executes before coming to the payment page but they all need to do the same for each payment so meaning that all test cases before payment needs to do exactly the same thing everytime (So I guess we just need to do tags at the payment to let the script know which payment to choose)?
How can I do a sort of tagging or maybe someone has another solution that is better? That I can be able to choose what payment provider I want to run
edit:
exports.config = {
capabilities: {
browserName: 'chrome',
chromeOptions: {
args: [
'incognito', 'disable-extensions', 'start-maximized', 'disable-infobars', '--window-size=1920,1080'
]
},
loggingPrefs: { browser: 'ALL' },
platform: 'ANY',
version: ''
},
specs: [
'pagesDesktop/testPage.js',
'pagesDesktop/paymentPage.js'
],
jasmineNodeOpts: {
reporter: "mochawesome",
defaultTimeoutInterval: 60000
},
SELENIUM_PROMISE_MANAGER: false,
framework: 'jasmine',
params: {
cardType: {
}
}
};
describe('Payment page', function () {
paymentPage = new PaymentPage();
console.log(browser.params.cardType);
if (browser.params.cardType === "mastercard") {
// The details page is accessible by the specified URL
it(`Credit Card - Has a form that can receive user data`, async function () {
await paymentPage.creditCardPayment();
});
}
just add if/else logic
describe('Payment page', function () {
paymentPage = new PaymentPage();
// The details page is accessible by the specified URL
it(`Credit Card - Has a form that can receive user data`, async function () {
await paymentPage.creditCardPayment();
});
if (browser.params.cardType === 'visa') {
it('Enets payment', async function () {
//TODO
});
} else if (browser.params.cardType === 'amex') {
//Amex
it('Enets payment', async function () {
//TODO
});
}
});
You can read how to parameterize teste here How can I use command line arguments in Angularjs Protractor?, or here
https://medium.com/#nicklee1/making-your-protractor-tests-data-driven-e3c9e2a5e4e7

Overriding Cypress' before and after methods

We are using Cypress.io to build our automation suite. We have a requirement to seed our database before every test and to clear the data afterward. This could be done like below.
describe('Our test suite', function() {
before(function () {
//loadDbSeed is a custom command that will load the seed file based on the spec file
seed = cy.loadDbSeed()
cy.task('seed:db', seed)
})
it('should be true', function() {
//Some test with some action followed by an assertion
cy.visit('/some-page')
cy.get('[data-cy="identifier"]')
.click()
expect(true).to.equal(true)
})
after(function () {
// clearDb is a custom command that will clear out the DB.
// We are still debating if we must clear the DB after the tests.
// But we might still need to do some common actions for the entire suite after
cy.clearDb()
})
})
The problem we see is that the same before and after operations will be required for all our test suites. So we would like to override these methods so that our tests are something like this.
describe('Our test suite', function() {
before(function () {
// DB seeding is done automatically
// Write only custom before steps required for this test
})
it('should be true', function() {
//Some test with some action followed by an assertion
cy.visit('/some-page')
cy.get('[data-cy="identifier"]')
.click()
expect(true).to.equal(true)
})
after(function () {
// DB clearing is done automatically
// Write only custom after steps required for this test
})
})
How do we achieve this? I have been digging around in the Cypress code and haven't found anything obvious.
If you look at the Cypress docs, using after isn't recommended - Cypress Docs. I'd caution against setting data globally, will you really need it for every test? If you need to enter data on a per-test basis at some point, will that conflict with this global data? You could do something like this on a per test basis:
describe('Our test suite', function() {
beforeEach(function () {
cy.then(async () => {
await MyDatabaseService.resetdata()
await MyDatabaseService.createSomeData()
});
});
it('should be true', function() {
//Some test with some action followed by an assertion
})
})
I've also had to nest some tests as follows when specific tests needed specific data (sorry if some of the formatting here is a bit out, hopefully it'll make sense!):
describe('Our test suite', function() {
beforeEach(function () {
cy.then(async () => {
await MyDatabaseService.resetdata()
await MyDatabaseService.createSomeData()
});
});
it('should be true', function() {
//Some test with some action followed by an assertion
});
describe('These tests need more data, this is a nested describe', function () {
before(function () {
cy.then(async () => {
await MyDatabaseService.addSomeMoreData()
});
it('this test uses the extra data', function () {
// Do some funky tests here
});
});
});
})
For the second tests above, the test will run all three database actions.
Upshot is, if you clear data before you run your tests then it makes things much clearer.
I hope that helps. I'm new to Cypress myself and it can be hard to drop that (bad?) habits we've used for some time in Selenium!

Protractor "restartBrowserBetweenTests: true" restart browser, but don't run test after this

I need to restart browser after each "it" (test), for this reason I write "restartBrowserBetweenTests: true" in protractor.config file. Now after first "it" (test) finished, browser closed and opened again (open website that I write in "beforeEach", but next "it" (test) is not run.
What I do wrong? I will be happy for any advice.
I use "protractor": "2.5.1".
Thanks!
Added:
beforeEach(function () {
browserUtil.navigateTo(browserUtil.url.main);
loginPage.loginAsSample();
});
afterEach(function(){
browserUtil.deleteCookies();
});
it("'Delete' button is inactive if there are no projects in the list", function() {
projectPage.clickOnProjectButton();
expect(projectPage.isProjectPageFormVisibility(true)).toBe(true);
expect(projectPage.isDeleteBtnDisable()).toBe(true);
});
it("Create new project with template 'A'", function() {
projectPage.clickOnNewProjectBtn();
projectPage.clickOnAProject();
projectPage.clickOnOkBtnProject();
expect(projectPage.isOpenedProjectVisibility(true)).toBe(true);
});
I've never utilized restartBrowserBetweenTests: true, but this works for me:
in protractor.conf, remove restartBrowserBetweenTests: true
in test.spec.js, modify beforeEach and afterEach
beforeEach(function () {
browser.get(browserUtil.url.main);
});
afterEach(function () {
browser.manage().deleteAllCookies();
});
Being that you're utilizing a page object structure for your tests, you could/would want to encapsulate browser.get(browser.browserUtil.url.main); into you're page object.
Where ProjectPage is defined, I'd add :
this.refreshPage = function () {
browser.get(browserUtil.url.main);
};
If you go with this approach, you'd change the call on beforeEach to :
beforeEach(function () {
projectPage.refresh();
});

Functional testing with grunt-webdriver, mocha and chai-as-promised

I'm trying to build a functional testing system to verify our web site is behaving correctly for our users. I have cobbled together a bunch of Node.js modules and helpers in an attempt to get a framework that provides simple, concise tests without a heap of nested function callbacks and I believe promises can provide that, so my package.json file looks like this:
"dependencies": {
"chai-as-promised": "^4.3.0",
"grunt": "^0.4.5",
"grunt-webdriver": "^0.4.8"
}
My Gruntfile.js looks like this:
module.exports = function(grunt) {
grunt.initConfig({
webdriver: { // for use with webdriver.io
options: {
desiredCapabilities: {
browserName: 'phantomjs' // No Xvfb required
}
},
chrome: {
tests: ['chrome/*.js'],
options: {
desiredCapabilities: {
browserName: 'chrome'
}
}
},
},
});
grunt.loadNpmTasks('grunt-webdriver');
grunt.registerTask('default', ['webdriver']);
};
And finally my test case in chrome/login.js looks like this:
'use strict';
var chai = require('chai'),
chaiAsPromised = require('chai-as-promised'),
assert;
chaiAsPromised.transferPromiseness = browser.transferPromiseness;
chai.use(chaiAsPromised);
assert = chai.assert;
describe('login test', function () {
it('verifies user can log in', function(done) {
browser
.url('https://localhost/')
.setValue('#userauth_username','foo')
.setValue('#userauth_password',"password")
.submitForm('#form_users_login')
.then(function(){
browser.getText('#auth-user-id', function(err, value){
console.log(value);
});
assert.becomes(browser.getText('#auth-user-id'), 'foo');
})//.call(done);
});
});
When I run grunt webdriver:chrome on the command line, I see it start up Chrome and log into the website. The 'auth-user-id' span is correctly displaying the user's id after logging in but for some reason browser.getText() is not returning it and the test is therefore failing. I have tried adding a .pause(100) after the .submitForm() to give me time to interact with the page in Chrome so I know it is a problem in the test case.
What am I doing wrong?
This seems to be the best and most succinct way of doing what I want. I'm not sure I need chai-as-promised yet but maybe I'll move the login function to an included file and use chai-as-promised to assert that the promised login has occurred before entering the tests.
'use strict';
var chai = require('chai'),
chaiAsPromised = require('chai-as-promised'),
assert,
expect;
chaiAsPromised.transferPromiseness = browser.transferPromiseness;
chai.use(chaiAsPromised);
assert = chai.assert;
expect = chai.expect;
describe('login test', function () {
it('verifies user can log in', function(done) {
browser
.url('https://localhost/')
.setValue('#userauth_username','foo')
.setValue('#userauth_password',"password")
.submitForm('#form_users_login')
.waitForExist('#auth-user-id')
.getText('#auth-user-id')
.then(function(text){
//console.log('Username: ' + text);
assert.equal(text, 'foo');
})
.saveScreenshot('out.png')
.call(done)
});
it('should not display logincontrols after login', function(done){
browser
.isVisible('#logincontrols')
.then(function(bool){
expect(bool).to.be.false;
})
.call(done)
});
it('should display loggedin section after login', function(done){
browser
.isVisible('#loggedin')
.then(function(bool){
expect(bool).to.be.true;
})
.call(done)
});
});
and for completeness, this is what I see on the output:
# grunt webdriver:chrome
Running "webdriver:chrome" (webdriver) task
login test
✓ verifies user can log in (7691ms)
✓ should not display logincontrols after login (70ms)
✓ should display loggedin section after login (58ms)
3 passing (8s)
Done, without errors.

How do I take a screenshot when a test in internjs fails?

I am having issues figuring out how to take a screenshot ONLY when a test fails in InternJs. I have this simple test in my registerSuite;
'verify google homepage': function () {
var url = 'https://www.google.com/';
return this.remote
.get(url)
.getCurrentUrl()
.then(function (data) {
assert.strictEqual(data, url, 'Incorrect URL');
})
.findByName('q')
.click()
}
I can simply create a screenshot using the following code;
.takeScreenshot
.then(function (data) {
fs.writeFileSync('/path/to/some/file', data, 'base64');
)}
I want to only take a screenshot, if the above test fails the assertion or is unable to find the locator.
I looked into the afterEach method, but I can't figure out how to get the status of the last test to apply a conditional.
So my question is, has anyone setup their internjs test to only take screenshots on failures and how was it accomplished?
It is not currently possible to interact with the currently executing test from beforeEach or afterEach methods; this capability is coming in the next version of Intern.
Selenium server, by default, provides a screenshot on every Selenium command failure, which is a Buffer object on the error.detail.screen property. If a Selenium command fails, just use this property which already has the screenshot waiting for you.
For assertion failures, you can create a simple promise helper to take a screenshot for you:
function screenshotOnError(callback) {
return function () {
try {
return callback.apply(this, arguments);
}
catch (error) {
return this.remote.takeScreenshot().then(function (buffer) {
fs.writeFileSync('/path/to/some/file', buffer);
throw error;
});
}
};
}
// ...
'verify google homepage': function () {
return this.remote.get(url).getCurrentUrl().then(screenshotOnError(function (actualUrl) {
assert.strictEqual(actualUrl, url);
}));
}
If it’s too inconvenient to wrap all your callbacks manually like this, you can also create and use a custom interface for registering your tests that wraps the test functions automatically for you in a similar manner. I’ll leave that as an exercise for the reader.
You can use catch method at the end of your chain and use error.detail.screen as suggested by C Snover.
'verify google homepage': function () {
return this.remote
.get(require.toUrl('./fixture.html'))
.findById('operation')
.click()
.type('hello, world')
.end()
.findById('submit')
.click()
.end()
.catch(function(error){
fs.writeFileSync('/tmp/screenshot.png', error.detail.screen);
})
}
I've been playing with this today and have managed to get it for an entire suite rather than needing to add the code to every single test which seems quite needless.
var counter = -1,
suite = {
beforeEach: function () {
counter++;
},
afterEach: function () {
var currentTest = this.tests[counter];
if (!currentTest.error) {
return;
}
this.remote
.takeScreenshot().then(function (buffer) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
fs.writeFileSync('/tmp/' + currentTest.name + '.png', buffer);
});
}
};
The annoying thing you will need to do is do this for every test suite rather than "globally" but is much better than doing it for every test.
Building on the answer by Hugo Oshiro,
// tests/support/CleanScreenshots.js
define([
'intern/dojo/node!path',
'intern/dojo/node!del',
], function(path, del) {
return new Promise((resolve, reject) => {
let directory = 'tests/screenshots';
del(path.join(directory, '**/*'))
.then(resolve)
.catch(reject);
});
});
Then in your intern config:
/* global define */
define([
'tests/support/CleanScreenshots'
], function (CleanScreenshots) {
return {
...
setup: function () {
return CleanScreenshots();
},
...
};
});
According to this issue, starting with the Intern 3.0 you can do a custom reporter that take an Screenshots when test fail. So you can centralize it in a simple way, just referencing the custom reporter in your config.js. In my case, what can I just add a reporter array in the config.js with the path to my custom array:
reporters: [
{ id: 'tests/support/ScreenShot' }
],
than I made an custom reporter overriding testFail:
'use strict';
define([
'intern/dojo/node!fs',
], function(fs) {
function ScreenShot(config) {
config = config || {};
}
ScreenShot.prototype.testFail = function(test) {
test.remote.takeScreenshot().then(function(buffer) {
try {
fs.writeFileSync('./screenshots/' + test.parent.name.replace(/ /g, '') + '-' +
test.name.replace(/ /g, '') + '.png', buffer);
} catch (err) {
console.log('Failed to take a screenshot: ' + err);
}
});
};
return ScreenShot;
});
Pay attention to the relative paths both to reference the custom reporter and the place for screenshots. They all seems to be taken considering where you run intern-runner, not the place the source files are located.
For more info about custom reporters go to this page.