Is there a way to let configuration variables in 'cypress.json' point to another variable?
A little example:
{
"baseUrl": "https://example.org"
"env": {
"apiUrl": "${baseUrl}/api/v1"
}
}
I didn't found something about this in the documentation, but it would be very usefull to me.
There is no way to make interpolation inside cypress.json because it is simple JSON file. But, you can achieve it during runtime, like this (put this code inside your cypress/plugins/index.js):
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
config.baseUrl = `${config.baseUrl}${config.env.apiUrl}`
console.log(config.baseUrl) // https://example.org/api/v1
return config;
}
And your cypress.json:
{
"baseUrl": "https://example.org"
"env": {
"apiUrl": "/api/v1"
}
}
Related
When running Cypress headlessly, I can see console.log output from the frontend code under test by using the DEBUG environment variable, like:
DEBUG='cypress:launcher' npx cypress run --browser chrome
However, I haven't found any similar way to see the output of cy.log from the Cypress test code when running headlessly. Even with DEBUG='cypress:*' I don't see them - they only seem to be visible in the interactive interface. It feels like there must be some way to see the cy.log output headlessly - can someone help with that?
The first step is to add a new task in your Cypress config file so that you can run console.log from Node:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
on("task", {
log(args) {
console.log(...args);
return null;
}
});
},
},
});
Then, you can override cy.log so that it calls this task whenever you run the command in headless mode, and console.log when you're running in headed mode. You can do this by adding the following to your commands file:
Cypress.Commands.overwrite("log", function(log, ...args) {
if (Cypress.browser.isHeadless) {
return cy.task("log", args, { log: false }).then(() => {
return log(...args);
});
} else {
console.log(...args);
return log(...args);
}
});
I have five different cypress projects in the same repo.
The Cypress.json file of each project has reporterOptions :
{
"fixturesFolder": "./src/fixtures",
"integrationFolder": "./src/integration",
……..
"reporter": "../../node_modules/mocha-testrail-reporter",
"reporterOptions": {
"username": "my-user-name”,
"password": "my-password",
"host": "https://abc.testrail.io",
"domain": "abc.testrail.io",
"projectId": 1,
"suiteId": 3,
"includeAllInTestRun": true,
"runName": "test"
}
}
The Username, host, password and domain value are same for all five cypress projects. Thus, I want to put them in the .env file like this, and access these variables and use them in the Cypress.json files
USERNAME= my-user-name
PASSWORD= my-password
HOST= https://abc.testrail.io
DOMAIN= abc.testrail.io
How do I access these variables? Any help will be appreciated. Thank you,
Take a look at Extending the Cypress Config File
Cypress does not support extends syntax in its configuration file
But in plugins it can be done
module.exports = (on, config) => {
const reporterParams = require('.env') // not quite sure of the format
// may need to fiddle it
const reportOptions = {
...config.reporterOptions, // spread existing options
"username": reporterParams.username,
"password": reporterParams.password,
"host": reporterParams.host,
"domain": reporterParams.domain,
}
const merged = {
...config,
reportOptions
}
return merged
}
This might be repeated question for you guys but really I didn't get answer yet.
Here is my multi-capabilities definition in protractor config file.
I want to access the deviceName parameter value. How can I do it?
exports.config = {
directConnect:true,
multiCapabilities: [
{
browserName: 'chrome',
'chromeOptions': {
'mobileEmulation': {
'deviceName': 'iPad'
}
}
}
],
Tried under onPrepare but not giving multi-capabilities values
browser.getCapabilities().then(function(c) {
console.log(c.get('deviceName'));
});
Not sure about solving with getCapabilities(), but you should be able to solve this with getProcessedConfig().
getProcessedConfig will return a promise of your entire configuration settings (and a few protractor defaults). So taking your example:
browser.getProcessedConfig().then((c) => {
console.log(c.capabilities.chromeOptions.mobileEmulation.deviceName);
});
You could make console.log(process.env) in the onPrepare block and find what you want.
Try getProcessedConfig()
http://www.protractortest.org/#/api?view=ProtractorBrowser.prototype.getProcessedConfig
Or just plain old stupid:
let device_name = 'iPad'
exports.config = {
directConnect: true,
multiCapabilities: [{
browserName: 'chrome',
chromeOptions: {
mobileEmulation: {
deviceName: device_name
}
}
}],
onPrepare: function () {
console.log('Device name will be', device_name);
}
Fetching device name worked as advised by Gunderson but now I am running into different issue I am unable to access the variable value outside the code block while in onPrepare.
onPrepare: function () {
browser.getProcessedConfig().then(function (c) {
return global.deviceName
c.capabilities.chromeOptions.mobileEmulation.deviceName;
}).then(function () {
console.log("Device Name is:" + global.deviceName);
customDevice = global.deviceName;
}
);
};
customDevice not printing any value.....which is define as global variable on top of the configuration file.
I know might be doing silly mistake in accessing it...:)
I am trying to record all browser request and responses. This can be done via browsermob-proxy api's.
For this, I have to change desired capabilities and change httpProxy for browser.
In beforeEach at global or file level, I am trying to change this. Though it reflects in browser object, actual browser is not initiated with those settings.
Simple example:
globalhook file
module.exports = {
before : function (done) {
},
beforeEach: function(browser, done){
browser.options.desiredCapabilities = {
"browserName": "chrome",
"proxy": {
"proxyType": "manual",
"httpProxy": "127.0.0.1:" + someport,
"sslProxy": "127.0.0.1:" + someport
},
"javascriptEnabled": true,
"acceptSslCerts": true,
}
done()
},
afterEach: function(browser, done){
//some code
}
after : function (done) {
//some code
},
}
If i change desired capabilities in before hook, chrome browser is taking those changes. Problem is with beforeEach [global, file level].
Further debugging, I've found setCapabilities function is run just before beforeEach Hook.
Could anyone please have a look or suggest if I am doing something wrong.
Thanks for moving the issue here.
The issue is that the desiredCapabilities are applied to a browser session.
beforeEach and afterEach run on the same browser session therefore any change in the desiredCapabilities won't be applied unless the session is restarted.
If you need to dynamically change the desiredCapabilities you have to structure your tests in the different way, e.g. split your tests in separate test classes
First of all, you probably want to use before() hook instead of beforeEach(), since beforeEach() will run before each test case and you won't be able to modify already started browser session. before() will run before browser session is initiated.
To solve your issue you need to define desiredCapabilities property on your test run module and in case you need to set some dynamic values, use before() hook to modify that object:
module.exports = {
desiredCapabilities: {
"browserName": "chrome",
"proxy": {
"proxyType": "manual",
"httpProxy": "127.0.0.1",
"sslProxy": "127.0.0.1"
},
"javascriptEnabled": true,
"acceptSslCerts": true,
},
before: function(client, done) {
this.desiredCapabilities.proxy.sslProxy = '127.0.0.1' + someport;
done();
}
}
I've searched official docs on this feature but couldn't find anything, though we used this in our tests and it worked well.
Update:
Since you only need to change proxy on desiredCapabilities object, there is a little hack:
// global hooks file:
module.exports = {
beforeEach: function(client, done) {
client.options.desiredCapabilities.proxy = {
"proxyType": "manual",
"httpProxy": "127.0.0.1:" + someport,
"sslProxy": "127.0.0.1:" + someport
};
done(client);
}
};
Changing desiredCapabilities object won't work, since reference to original object is already stored somewhere under the hood of Nightwatch. But nothing stops you from overriding proxy property on that object.
After checking sources and tests in Nightwatch I believe this is the only solution for your case.
I'm looking for a way to disable chunkhash in neutrino.js when building, but didn't find any documentation about it, anyone could help?
Updated:
As in webpack, I can customize the output.filename, in neutrino.js, it seems the string "[name].[hash].bundle.js" is baked in, and there's no way to remove [hash] as far as I can see.
In your .neutrinorc.js file, you can add an additional override function to change the output filename to not include the chunk hash (using neutrino-preset-react as an example:
module.exports = {
use: [
'neutrino-preset-react',
(neutrino) => {
// the original value of filename is "[name].[chunkhash].js"
neutrino.config.output.filename('[name].js');
}
]
};
If you want to change build targets based on an environment variable:
module.exports = {
use: ['neutrino-preset-react'],
env: {
NEUTRINO_TARGET: {
desktop: {
use: [
(neutrino) => neutrino.config.output.filename('[name].js');
]
},
mobile: {
use: [
(neutrino) => neutrino.config.entry('mobile').add('index.mobile.js');
]
}
}
}
};
Then you can run Neutrino twice with differing environments:
NEUTRINO_TARGET=desktop neutrino build
NEUTRINO_TARGET=mobile neutrino build