How to set retryAssertionTimeout based on environment in Nightwatch - nightwatch.js

I'm trying to set the retryAssertionTimeout based on the environment. I have a node variable (NODE_ENV) that will be set to either QA or Prod and based on the value of that variable, I want to set a corresponding value for the retryAssertionTimeout. Here's my globals.js file ...
module.exports = {
//retryAssertionTimeout: 30000,
before: function (done) {
console.log("test env: " + process.env.NODE_ENV)
if (process.env.NODE_ENV == 'qa') {
console.log("Running in the QA environment")
retryAssertionTimeout: 30000
}
done()
}
}
Unfortunately, this doesn't work.

I would do it as below.
module.exports = {
retryAssertionTimeout: process.env.NODE_ENV === 'qa' ? '4000' : '5000',
before: function (done) {
console.log("Environment Selected: " + process.env.NODE_ENV)
done()
}
}

I put the suggested code into my globals.js file (I specified 10000 for the qa timeout value). I then created and ran a simple test like so ...
module.exports = {
'Simple Test': function (browser) {
console.log("*** retryAssertionTimeout: " + browser.globals.retryAssertionTimeout)
browser.waitForElementVisible('.someFakeSelector')
}
}
The output is ....
C:\git\portal-ui-tests>node nightwatch tests/simpleTest.js
Environment Selected: qa
[Simple Test] Test Suite
i Connected to localhost on port 4444 (10061ms).
Using: firefox (77.0.1) on windows 10.0 platform.
Running: Simple Test
*** retryAssertionTimeout: 10000
× Timed out while waiting for element <.someFakeSelector> to be present for 5000 milliseconds. - expected "visible" but got: "not found" (5085ms)
So it looks like retryAssertionTimeout is being set but it's not being used when I call waitForElementVisible.

Related

How can I see `cy.log` output when using Cypress headlessly?

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);
}
});

How to detect Development mode within Svelte code? [duplicate]

The dev mode using npm run dev, the release mode using npm build
How could i know that it's currently built on dev mode or not in the code, for example:
<script>
import {onMount} from 'svelte';
onMount(function(){
if(DEVMODE) { // --> what's the correct one?
console.log('this is x.svelte');
}
})
</script>
If you are using sveltekit:
import { dev } from '$app/environment';
if (dev) {
//do in dev mode
}
Not sure about the correct method. I share what I did on my project.
in rollup.config.js
import replace from "#rollup/plugin-replace";
const production = !process.env.ROLLUP_WATCH;
inside plugins:[ ] block add this
replace({
isProduction: production,
}),
rollup.config.js will look like this.
},
plugins: [
replace({
isProduction: production,
}),
svelte({
Then use isProduction inside components .
if (!isProduction){ console.log('Developement Mode'); }
If you are using Svelte with Vite, you may use:
import.meta.env.DEV - true in development environment.
import.meta.env.PROD - true in production environment.
import.meta.env.MODE - name of the mode, if you need more control.
See Vite docs on Env variables
I solved this problem by checking the hostname the application is running on.
You can also use other forms like, port or even msm a localStore browser variable.
Note that I check if the application is running on the client side before using the 'window'
const isProduction = (): boolean => {
// Check if is client side
if (typeof window !== 'undefined' && window.document !== undefined) {
// check production hostname
if (window?.location.hostname !== undefined &&
window.location.hostname === 'YOUR_PRODUCTION_HOSTNAME') {
return true
} else {
return false
}
} else {
return false
}
}
When using Svelte (not svelte-kit), this worked for me inside svelte components:
<script>
let isProduction = import.meta.env.MODE === 'production';
if (!isProduction) {
console.log("Developement Mode");
} else {
console.log("Production Mode");
}
</script>
Thanks timdeschryver for the reference

How to check next testcase then browser.verify.ok fails

module.exports = {
'test' : function (browser) {
browser
.url("someurl");
browser.getTitle((title) => {
browser.verify.ok(title.trim().length !== 0, "Title is not empty");
});
browser.expect.element('h1').text.to.not.equal('');
}
};
If browser.verify.ok fails testcase stops browser.expect.element('h1').text.to.not.equal(''); is not running.
How can I fix this?
In order to continue running tests after an assertion fails, set abortOnAssertionFailure: false in globals.js file. Reference: https://github.com/nightwatchjs/nightwatch/blob/master/lib/settings/defaults.js#L19

Protractor passing parameters in script run command

I need to pass the credentials in command running a script.
For now, I am using in protractor file following part:
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
}
}));
if (browser.params.Url == 'http://devel/') {
browser.params.webmaster='abc';
browser.params.webmaspass='foo';
}
//(other environments)
else {
console.log('-------------error during log in');
}*/
}
and it was working fine, but I need to change it - I can't pass credentials in this way. I thought about changing it to:
if (browser.params.Url == 'http://devel/') {
browser.params.webmaster='';
browser.params.webmaspass='';
}
and run the script using
npm run dev-script --browser.params.Url='http://devel/' --browser.params.webmaster='abc' --browser.params.webmaspass='foo'
where package.json I have:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev-script": "protractor --params.Url=http://devel/ --browser.params.webmaster='' --browser.params.webmaspass=''"
},
(or any variation) But it fails - I can't update params during running script, I need to write down the credentials in the code (which I find a little unsafe)
I found issues like Protractor needs password for login => insecure? but it about Google Auth problems
Any idea?
You need to remove the variable assignment in the onPrepare. You are overwriting what you are passing in from the command line by setting it to an empty string.
When you pass them in from the command line they will be availble on the params object. There is no need to set them again in your onPrepare. Add a console.log() in your onPrepare and you will see.
Run it from the command line like this: protractor conf.js --params.webmaster=abc --params.webmaspass=foo --params.url=http://devel/
Again, if you log them in your onPrepare you will see that it is working. The way you currently have it you are just overwriting the values you are passing in through the command line.
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
}
}));
if (browser.params.Url == 'http://devel/') {
consoel.log(browser.params.webmaster) //should be abc
console.log(browser.params.webmaspass) //should be foo
}
//(other environments)
else {
console.log('-------------error during log in');
}*/
}
Another way you can do this is to set some environment variables before your test run and then you can access them in your scripts by using process.env.envVariableName or ${envVariableName}. Both ways will work.
set DEVEL_WEBMASTER=abc
set DEVEL_WEBMASPASS=foo
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
}
}));
if (browser.params.Url == 'http://devel/') {
browser.params.webmaster=process.env.DEVEL_WEBMASTER;
browser.params.webmaspass=process.env.DEVEL_WEBMASPASS;
}
//(other environments)
else {
console.log('-------------error during log in');
}*/
}
Just remember that if you use this method you would have to set the variables for each session. If you are planning to automate these tests using a CI environment you can just add them there as secret variables (if you have that option) and they will always be there ready and waiting. There will be no need to set them manually during each build.
What I did it here was create the scripts in my package.json:
scripts: {
"automation-test": "concurrently --raw --kill-others \"./node_modules/.bin/webdriver-manager start\" \"sleep 5 && ./node_modules/.bin/protractor configuration/protractor.config.js\"",
"automation:pending": "TAGS=#pending npm run automation-test"
}
And in my protractor.conf.js I just assign the value to a variable so I can use in my config. Like this:
let tags = process.env.TAGS;
Then the command that I run is just this:
npm run automation:pending
but I could pass the TAGS like this as well:
npm run automation-test TAGS=#pending
I have not seen the configuration file on the parameters of the command line. you must specify the configuration file:
example: protractor config.js --params ......
Do this in your script file: i have added a config file after the command protractor
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev-script": "protractor config.js --params.Url=http://devel/ --browser.params.webmaster='' --browser.params.webmaspass=''"
},

JSCover with PhantomJS - TypeError: 'null' is not an object

When I try to run JSCover with PhantomJS, I see below ERROR:
Steps followed:
1) Run the JSCover Server:
java -jar ~/JSCover/target/dist/JSCover-all.jar -ws --report-dir=report
2) Run the PhantomJS runner with JSCover:
*phantomjs --debug=true ~/JSCover/src/test/javascript/lib/PhantomJS/run-jscover-jasmine.js
localhost8080/<app>/module/framework/test/SpecRunner.html
TypeError: 'null' is not an object(evaluating''document.body.querySelector('.description').innerText')`
phantomjs://webpage.evaluate():3
phantomjs://webpage.evaluate():22
phantomjs://webpage.evaluate():22
2013-09-19T16:36:07 [DEBUG] WebPage - evaluateJavaScript result QVariant(, )
2013-09-19T16:36:07 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function () {
jscoverage_report('phantom');
})(); })()"
2013-09-19T16:36:07 [DEBUG] WebPage - evaluateJavaScript result QVariant(, )
2013-09-19T16:36:07 [DEBUG] Network - Resource request error: 5 ( "Operation canceled" ) URL: localhost8080/<app_home>/lib/backbone/1.0.0/backbone.js?cb=0.5381254460662603
This was an issue that I ran into yesterday. It turns out that the example script does not work for newer versions, so I built a new Phantom Script that works for Jasmine 2.X which fixes it. You can locate the working script here in my repository:
https://github.com/tkaplan/PhantomJS-Jasmine
I faced with the same issue when I try running Jasmine with PhantomJS.
I realized that the latest version of Jasmine-html.js (jasmine-2.0.0-rc2)
does not go along with PhantomJS's run-jasmine.js (phantomjs-1.9.2-windows).
In the jasmine-2.0.0-rc2 version of Jasmine-html.js,
The '.description' class is not available if all tests passed.
This 'description' class is created only if any test failed.
Thus, when I run the phantomjs with all tests passed, I get the above error message.
I modified run-jasmine.js to adapt to Jasmine-html.js (jasmine-2.0.0-rc2) to
resolve this issue.
Are you loading your tests asynchronously? I use requirejs for modular javascript. It is also used to load the test specs:
<script data-main='SpecRunner' src='/test/scripts/libs/require.js'></script>
When using JSCover, the run-jscover-jasmine.js script does not account for this async behaviour, so the DOM nodes referenced in the query do not exist (yet). I modified the script to delay the waitFor call by 1 second:
page.open(system.args[1], function(status){
if (status !== "success") {
console.log("Unable to access network");
phantom.exit();
} else {
// Added 1s delay here
window.setTimeout(function() {
waitFor(function(){
return page.evaluate(function(){
return document.body.querySelector('.symbolSummary .pending') === null
});
}, function(){
var exitCode = page.evaluate(function(){
console.log('');
console.log(document.body.querySelector('.description').innerText);
var list = document.body.querySelectorAll('.results > #details > .specDetail.failed');
if (list && list.length > 0) {
console.log('');
console.log(list.length + ' test(s) FAILED:');
for (i = 0; i < list.length; ++i) {
var el = list[i],
desc = el.querySelector('.description'),
msg = el.querySelector('.resultMessage.fail');
console.log('');
console.log(desc.innerText);
console.log(msg.innerText);
console.log('');
}
return 1;
} else {
console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText);
return 0;
}
});
page.evaluate(function(){
jscoverage_report('phantom');
});
phantom.exit(exitCode);
});
}, 1000);
}
});
Depending on the amount of code loaded, you may have to increase the delay.

Resources