I am trying to use Cypress 12 to run compnent tests in a Vue.js 2 app. Below is my cypress.config.ts file:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
baseUrl: "http://localhost:9090/.......",
defaultCommandTimeout: 60000,
},
component: {
devServer(cypressConfig: CypressConfiguration) {
// return devServer instance or a promise that resolves to
// a dev server here
return {
port: 9090,
close: () => {},
};
},
},
});
I setup a custom devServer in vue.config.js (otherwise Cypress starts uses its own localhost):
module.exports = {
devServer: {
port: 9090,
proxy: 'http://localhost:8080'
}
}
However, the tests wont load
When I run e2e tests, all is fine: tests appears, calls localhost:9090. However, if I want to run only component tests, it just gets stuck trying to load the tests.
It is not a DevTools problem as I have looked into that. All other configuration settings are standard.
When running protractor without sharding, it runs just fine, the tests are passing or failing, and in the end the webdriver / protractor execution stops.
When trying to run protractor with sharding, the execution works but when all the tests are done, the process hangs, the console doesn't display anything.
I stripped down the config file as much as possible to give more visibility.
I'm running protractor using
protractor conf --suite all
Conf file:
'use strict';
require('dotenv').config();
const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
let config = {
framework: 'jasmine2',
baseUrl: process.env.CLUSTER_URL,
rootElement: 'body',
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter());
},
suites: {
all: [
'FoH/customerManagement/UI/*.smoke.spec.js',
'Other/logo/logo.*.spec.js'
]
},
capabilities: {
browserName: 'chrome',
shardTestFiles: true,
maxInstances: 2
}
};
exports.config = config;
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);
});
});
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.
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).