'How to run cypress test using tags from saucectl
I am able to run cypress test on saucelab using saucectl. Now the ask is to run tests with required tags like smoke or regression. Is there any configuration that I can do in confg.yml file or pass value from CLI to run tag specific test.
Solution 1:[1]
saucectl leaves much of the test organisation up to your test runners which is unfortunate, because Cypress itself doesn't have in-built support for running tests via tag.
There's a partial workaround that might suit, based on file location. In your config.yml, you can specify individual suites (see here) which let you provide individual config options to Cypress. You're still limited to running individual files (by passing them to the testFiles option; see the docs).
This isn't a problem if you only want to run entire files; Just include the file in each Suite you want it to be part of.
Glob-based "tag" Selection
By making clever use of globbing in the testFiles option, you can avoid having to update your saucectl config file every time you add a new test file.
This removes the risk of forgetting to add files to their respective groups, and also helps keep information about each spec file (eg, what kind of spec it is) closer to the file itself.
Example
Suite Files
Say your tests look like this. Each file contains tests that should only run on a specific browser (or combination of browsers).
specs/
ui/
login_chromeOnly_spec.js
login_firefoxOnly_spec.js
accounting/
jsMathBugFix_ieOnly_spec.js
chromiumCantCount_edgeOnly_chromeOnly_spec.js
config.yml
Here's the suites component of config.yml. Each suite has a unique glob for testFiles which finds all tests in your specs folder which ends in the respective _browserOnly_spec.js value.
suites:
- name: "Where There's Smoke There's"
browser: "firefox"
platformName: "Windows 10"
config:
testFiles: ["specs/**/*firefoxOnly*_spec.js"]
- name: "Cream on"
browser: "chrome"
platformName: "Windows 10"
config:
testFiles: ["specs/**/*chromeOnly*_spec.js"]
- name: "Livin' on the"
browser: "edge"
platformName: "Windows 10"
config:
testFiles: ["specs/**/*edgeOnly*_spec.js", "specs/**/*ieOnly*_spec.js"]
How it works
The first glob, ["specs/**/*firefoxOnly*_spec.js"], will match
specs/ui/login_firefoxOnly_spec.js.
The second glob, ["specs/**/*chromeOnly*_spec.js"], matches specs/ui/login_chromeOnly_spec.js AND specs/accounting/chromiumCantCount_edgeOnly_chromeOnly_spec.js
The third suite has two entries in the glob array, ["specs/**/*edgeOnly*_spec.js", "specs/**/*ieOnly*_spec.js"]. It will include all files which match any of those globs; In this case it matches any file tagged with either edgeOnly or ieOnly, which is specs/accounting/jsMathBugFix_ieOnly_spec.js and chromiumCantCount_edgeOnly_chromeOnly_spec.js.
Individual spec alternatives
If you want to have individual specs within a file tagged... You're out of luck at the moment. I can think of a couple of ways of doing this:
- Use a plugin to add tags to Cypress, then pass the tag selection option via
config.yml - Send a PR to Cypress to add tags natively
- Write a transpiler plugin to allow you to add tags to your tests, then have them transpiled into individual files (then use the glob approach)
Solution 2:[2]
I'd suggest using a dedicated library for this, such as decimal.js.
Decimal objects have a useful method isInteger(), that can be invoked after the division is complete.
You can set the precision required, though I believe the default should suffice (20, see precision)
function isQuotientInteger(dividend, divisor) {
return new Decimal(dividend).dividedBy(new Decimal(divisor)).isInteger();
}
let testInputs = [ { dividend: 1.15, divisor: 0.01 }, { dividend: 9.96, divisor: 0.01 }, { dividend: 15.121, divisor: 0.01 } , { dividend: 3.14159, divisor: 0.00001 }];
formatRow('Dividend', 'Divisor', 'Quotient is integer')
for(let testInput of testInputs) {
formatRow(testInput.dividend, testInput.divisor, isQuotientInteger(testInput.dividend, testInput.divisor))
}
function formatRow(...row) {
console.log(...row.map(f => (f + '').padEnd(10)))
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/10.3.1/decimal.min.js" integrity="sha512-Ou4M+sSU8oa+mE3juYqR3JmW633MUpMhe1cd+IusOtfjkMo8I3zXs4fRmjmCFqpRg5RK/geqoXBY8XRwFY2Rsg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Solution 3:[3]
Borrowing from this thread:
Rounding to the nearest hundredth of a decimal in JavaScript
let ex1=1.15 / 0.01
let ex2=9.96 / 0.01
let ex3=15.121 / 0.01
const desired_place=3
function round(num, places) {
//round # to however many places you need
let multiplier = Math.pow(10, places);
let rounded= Math.round(num * multiplier) / multiplier;
return Number.isInteger(rounded)? true : false
}
console.log(round(ex1,desired_place)) //true
console.log(round(ex2,desired_place)) //true
console.log(round(ex3,desired_place)) /false
Solution 4:[4]
In Javascript all numbers are internally represented as floating point numbers, which can in many cases lead to visible or invisible rounding issues, when mathematical operations are executed. It is unfortunate that Number.isInteger() produces an unexpected result in the cases you list, where we humans can clearly "see" that the result of the operation must be an integer value. Here is a practical solution to find out if a quotient is an integer with a given precision:
First you should use Number.toFixed() to get a number rounded to your required precision, and then apply Number.toInteger().
function isInteger(arg) {
const precision = 12; // for example
return Number.isInteger(Number(arg.toFixed(precision)));
}
console.log(isInteger(1.15 / 0.01));
console.log(isInteger(9.96 / 0.01));
console.log(isInteger(15.121 / 0.01));
Solution 5:[5]
Try checking for the remainder.
console.log(1.15 % 0.01 === 0) // false
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Skeloquent Kokkaku |
| Solution 4 | |
| Solution 5 | Vladislav Dobrinskii |
