Change directory in Cypress using cy.exec() - javascript

Working with cypress to run a different folder's python script. but I guess the directory is not changing perfectly.
gotoPythonProject() {
cy.exec("cd /Users/**/Desktop/project/");
cy.exec("ls"); // which does not reflect the files of project that's been changed
cy.reload();
}

As with many CI tools it is normal behavior that every command starts in a fresh scope. This way a command is unaffected by the state of a previous one.
There are two practical solutions to this case:
Combine your commands using &&. For example:
cy.exec("cd .. && pwd");
Call a bash script. For example: cy.exec("test_me.sh");, your bash script would than look something like:
#!/bin/bash
cd ..
pwd

Related

How do I pass in an entire JSON as the cypress.env through the command line in Cypress?

I am familiar with adding individual environment variables through the CLI by entering:
./node_modules/.bin/cypress run -- --env itemToOverride="val"
But let's say I have an entire JSON that can be a valid cypress.env, how do I pass that in via CLI without having to add each item from the JSON individually?
For example, let's say I have env1.json and env2.json which both have valid structure for my test, but I want to be able to run all my tests with env1 then env2? Similar to:
./node_modules/.bin/cypress run -- --env env1.json
For others that stumble here and want a working answer: The easiest solution to this would be to create a config folder in your project directory containing all the environment JSONs you want. I wrote a script to take an environment file's name as an argument when running it. I decided on a bash script, but you could also use an npm script to achieve this. Basically, to run the tests in "env1" you would type on your command line:
./cypress --env=env1
The way I achieved this was by copying the selected JSON from the config folder to the main directory, then renaming it 'cypress.env.json'. This is a setup function in my bash script to get you started:
function doSetup {
echo "Selecting environment variables from $ENV.env.json"
cp ./cypress/config/"$ENV.env.json" ./
mv "$ENV.env.json" "cypress.env.json"
}
It's an obscure problem that does not have very many answers online, but I hope this helps!

how to change the test file run order in cypress in run time

I have 3 different test files
1- testfile-1.js
2- testfile-2.js
3- testfiles-3.js
i can change the test file running order using testfiles feature in cypress.config file but i want to change the order runtime not hard coded.
looking for the solution
Regards
Change the file name to something like this in case you don't want to use testFiles: [] in your cypress.config.
01-testfile.spec.js
02-testfile.spec.js
03-testfile.spec.js
You can comma-separate the file names on the command line,
yarn cypress run --spec "testfile-1.js,testfile-2.js,testfiles-3.js"
Not sure if that guarantees the order though.
If no luck with that, it would be possible to guarantee the order by running with a special script (but it's more work).
i found a way
{
"chromeWebSecurity": false,
}
paste this in cypress.json and it will allow the external iframes to work in cypress

Pass command line arguments to NodeJS script from NPM script

I have a main script (publish-all.js) from which I want to invoke the npm publish script of an Angular project, which also has a sub-script (publish.js that does general stuff (creating folders, copying files, moving folders...) after ng build.
I need to pass some environment variables to that second script.
I am using shelljs to run unix-like commands.
I tried using:
npm run publish -- VERSION=${productVersion} DESTDIR=${productDestinationPath}
From publish-all.js where productVersion and productDestinationPath are constants declared above that line, and which invokes the following script from the package.json:
"publish": "ng build --prod && node ./scripts/publish.js"
But the actual command line I get is
ng build --prod && node ./scripts/publish.js "VERSION=value" "DESTDIR=value"
Finally, in my publish.js script I tried getting those variables the following way:
let version = process.env.VERSION;
let destinationPath = process.env.DESTDIR;
But I get undefined values.
What am I doing wrong? Is the a better way of doing all this?
Should I maybe use process.argv instead??
I am using this strategy because it is what I were told to do, but I would like to know if there is a less confusing way.รง
EDIT 2021-07-13
I tried using export (with shelljs, since I am in Windows and using powershell) but I am getting an exception.
I have the following code in publish-all.js now:
shelljs.exec(`export VERSION=${productVersion}`);
shelljs.exec(`export DESTDIR=${productDestinationPath}`);
shelljs.exec('npm run publish');
And in the publish.js script from the ANGULAR project:
version = process.env.VERSION;
destinationPath = process.env.DESTDIR;
Though it does not get to publish.js. It gets stuck in the shelljs.exec('npm run publish'), with the following exception:
I had to hide the project folder because of privacy policies, but it is a subfolder inside the folder where I am executing publish-all.js.
Environmental variables go BEFORE the command. So, instead of passing them after you can add them BEFORE:
VERSION=${productVersion} DESTDIR=${productDestinationPath} npm run publish
Or,
You can export the variables first then run the script:
export VERSION=${productVersion}
export DESTDIR=${productDestinationPath}
npm run publish

Get os environment in typescript with node js

I have operating system environment called KEY and when i called it with
echo $KEY in Ubuntu and it will print out the value
How can I get this value in typescript?
I already looked out for this and every one say i must call it with process.env.KEY in my typescript code, but if i print the process.env.KEY it would print undefined
If your executing your TypeScript code in the node environment process.env.KEY should work; at least it works on my Windows 10 machine.
However, you can alternatively use cross-env while executing your code and set custom value to any custom code.
Let's say the typescript code looks as follows:
console.log(process.env.MYKey);
While executing the code you can use cross-env to set the value for MYKey, as follows.
tsc myfile; cross-env MYKey=MyVal node myfile
And certainly you can concisely put this invocation script in your npm run script (in package.json), and use that directly.
Example:
"scripts": {
"prerun-myfile":"tsc myfile",
"run-myfile":"cross-env MYKey=MyVal node myfile"
}
And execute your code using npm run run-myfile.
Hope this helps.

How to run npm {bin: script.js} with parameters

I am planing to make something similar as lodash custom builds. So in general I want to let user write command like:
lodash category=collection,function
Which create custom module just with category i specified
I read few tutorials how to run scripts with npm bin. Just in case I understand something wrong I write it what i think.
So if I have package.json with this part:
"main": "bin/index.js",
"bin": {
"snippet": "bin/index.js"
},
and I npm install -g console should listen for command snippet and when i write it it run the script index.js in folder bin.
This part looks it works correctly for me. When i have something simple in my index.js i.e. console.log('It Works').
In standard situation you want to let user pass parameters to script. So i found out that all parameters should be in variabile process.argv.
The process.argv property returns an array containing the command line
arguments passed when the Node.js process was launched. The first
element will be process.execPath. The second element will be the path
to the JavaScript file being executed. The remaining elements will be
any additional command line arguments.
So i simply console.log it and run script.
If I run script via command snippet -f -a
Output is : [ 'node', 'path/to/file' ]
If i run script via node bin/index.js -f -a
Output is: [ 'node', 'path/to/file', '-f', '-a' ]
I dont understand that, its same script but different output. However I try it looks like when i call script via bin command it never pass parameters.
Is here someone who have experience with this? And advise me what i am doing wrong?
Or alternativly is there some other way how to make this?
Thanks for any advise.
It take a time however I have a solution now so hope it help someone later.
Where was a problem:
I noticed that my windows has default program to run .js file set to NODE.js and because it's default setting of course all .js files are opening without parameter.
So in my case every .js file open with NODE no matter what, I try to changed it to open with something like PSPAD or similar but this basicly open editor instead of execute file.
How did I fix it:
Instead of using executing .js directly with something I make my ./bin/index.js binary file (basicly removed .js suffix)
Added #!/usr/bin/env node on top of index file
Goes to package.json and changed all dependency on ./bin/index.js to ./bin/index
Woala! it works :)
p.s. As I mentioned at start I believe there is an option to run this with .js as well but I wasn't able to find it. So please if anyone will find it let me know.
Thanks

Categories