How to pass in custom parameter for running tests on particular env - testing

I am running my tests using - .testcaferc.json file and command that I am using to run is : node node_modules/testcafe/bin/testcafe
I want to pass an additional parameter for running my different tests on different environments. When I tried to add that parameter in command: node node_modules/testcafe/bin/testcafe production then I am not able to it as it consider them as tests.
Please let me know how can I handle this.

You can use the environment variables for this case.
Set the environment variable
// Enviroment variable set is platform specific
// See https://devexpress.github.io/testcafe/documentation/recipes/configuration/access-environment-variables-in-tests.html#set-environment-variables
export production=true
testcafe chrome test.js
and use it in a test
fixture ('Fixture');
test('test', async t => {
console.log(process.env.production);
});

Related

Setting up a general afterEach test for all test specs

Is there a way to set up a general afterEach test that will run after each of the tests are running?
(instead of configuring it repeatedly for each test file)
Found it - you need to test the afterTest property to the desired function in the webio configuration file.
https://webdriver.io/docs/configurationfile/

using Webdriverio service, when running as script (no test)

I wrote a webdriver.io script for scrapping a website.
The way to start such script is by running it through node command i.e.:
node my-webdriverio.js
I want to install some Webdriverio services, specifically:
https://webdriver.io/docs/devtools-service/#cdp-command for using the CDP protocol in my script.
The problem is that the only way to register a service is by setting it into wdio.conf.js file like this:
export.config = {
// ...
services: ['devtools'],
// ...
};
the problem is that for using this method you must run it as a test (i.e. npx wdio wdio.conf.js)
but because I am using the "run in a script" option, I can't find any way to register a service
https://webdriver.io/docs/gettingstarted#run-in-a-script
and web I am trying to check if I have the cdp capability I am getting undefined:
console.log(typeof browser.cdp); // undefined
So my question is, how can I register a service, or even better how can I use the 'devtool' service when running Webdriverio as a script?

How can I set up TestCafe to run on an environment specified in the CLI?

How can I set up TestCafe to run on an environment specified in the CLI?
For example
testcafe chrome test.js --env=development
I want to set up a list of different environments - such as development, integration, etc - in which I can call in the CLI when running the tests.
Is there a default set-up of this in the config?
Does anyone have an example?
TEST_ENV=development npm run test:firefox
Above is the command to run test.
Use this environment variable via process.env.Test_ENV, wherever you need to pass 'development' or any other environment as value. The script,
"scripts": {
"test:firefox": "testcafe firefox ./tests"}

Running protractor test parallel on different environments

I would like to run my protractor test on different environments such as
testing it on local environment,
testing it on test environment,
testing it on production
environment and so on at the same time and using the same browser example chrome.
So in this case my base URL would change for every environment: When I run the test I would like to run it parallel on all the different environments.
baseUrl:'localhost:8080'
baseUrl:'tst.company.com'
baseUrl:'prod.company.com'
etc
and browser remains the same
multiCapabilities:[
{ 'browsername':'chrome',
'chromeOptions':{
'binary': 'drive:pathToChrome',
'args':'[]'
'extensions':[]
}
}]
Any one knows how to, on this cases.
Thanks
I would approach this with a task manager: grunt and grunt-parallel.
Create 3 separate grunt task configurations with different baseUrl settings (you would need grunt-protractor-runner package installed).

Running a set of actions before every test-file in mocha

I've started recently working with mocha to test my expressjs server.
My tests are separated to multiple files and most of them contain some duplicated segments (Mostly before statements that load all the fixtures to the DB, etc) and that's really annoying.
I guess I could export them all to a single file and import them on each and every test, but I wonder if there are some more elegant solutions - such as running a certain file that has all the setup commands , and another file that contains all the tear-down commands.
If anyone knows the answer that would be awesome :)
There are three basic levels of factoring out common functionality for mocha tests. If you want to load in some fixtures once for a bunch of tests (and you've written each test to be independent), use the before function to load the fixtures at the top of the file. You can also use beforeEach function if you need the fixtures re-initialized each time.
The second option (which is related), is to pull out common functionality into a separate file or set of files and require that file.
Finally, note that mocha has a root level hook:
You may also pick any file and add "root"-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe() block, called the "root suite").
We use that to start an Express server once (and we use an environment variable so that it runs on a different port than our development server):
before(function () {
process.env.NODE_ENV = 'test';
require('../../app.js');
});
(We don't need a done() here because require is synchronous.) This was, the server is started exactly once, no matter how many different test files include this root-level before function.
The advantage of splitting things up in this way is that we can run npm test which runs all tests, or run mocha on any specific file or any specific folder, or any specific test or set of tests (using it.only and describe.only) and all of the prerequisites for the selected tests will run.
Why not mocha -r <module> or even using mocha.opts?
Have your common file in, say, init.js and then run
mocha -r `./init`
Which will cause mocha to run and load ./init.js before loading any of the mocha test files.
You could also put it in mocha.opts inside your tests directory, and have its contents be
--require ./init