marmelab / gremlins.js

Monkey testing library for web apps and Node.js

Home Page:https://marmelab.com/blog/2020/06/02/gremlins-2.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: Uncaught RangeError: Chance: Min cannot be greater than Max.

ps-george opened this issue · comments

Getting Uncaught RangeError with Chance using gremlins.js in Cypress. Solution seems simple to implement.

  • gremlins.js version: 2.0.1
  • node version: 12.9.0
  • npm (or yarn) version: 6.10.2
  • browser version: electron 80 or Firefox

Steps To Reproduce

  1. Run gremlins.js with Cypress.

The current behavior

RangeError
Chance: Min cannot be greater than Max.
View stack trace
RangeError: Chance: Min cannot be greater than Max.
testRange@http://localhost:3000/__cypress/tests?p=cypress/integration/netherplane.js-019:2218:20
[5]</</</chance_1</</Chance.prototype.integer@http://localhost:3000/__cypress/tests?p=cypress/integration/netherplane.js-019:2401:19
[5]</</</chance_1</</Chance.prototype.natural@http://localhost:3000/__cypress/tests?p=cypress/integration/netherplane.js-019:2443:22
defaultPositionSelector@http://localhost:3000/__cypress/tests?p=cypress/integration/netherplane.js-019:9892:25
clicker/</<@http://localhost:3000/__cypress/tests?p=cypress/integration/netherplane.js-019:9955:32
executeInSeries/</<@http://localhost:3000/__cypress/tests?p=cypress/integration/netherplane.js-019:10735:61

The expected behavior

No error.

Suggested solution:

Probably quite similar to #27 and #47 .
Culprit is here:

const defaultPositionSelector = () => {
	        return [
	            randomizer.natural({
	                max: document.documentElement.clientWidth - 1,
	            }),
	            randomizer.natural({
	                max: document.documentElement.clientHeight - 1,
	            }),
	        ];
	    };

Solution: Use || or Math.max(document.documentElement.clientWidth - 1, 0)

Changes I made to fix this issue:

diff --git a/src/species/clicker.js b/src/species/clicker.js
index 1ff294b..cf82a16 100644
--- a/src/species/clicker.js
+++ b/src/species/clicker.js
@@ -20,10 +20,10 @@ const getDefaultConfig = (randomizer) => {
     const defaultPositionSelector = () => {
         return [
             randomizer.natural({
-                max: document.documentElement.clientWidth - 1,
+                max: Math.max(0, document.documentElement.clientWidth - 1),
             }),
             randomizer.natural({
-                max: document.documentElement.clientHeight - 1,
+                max: Math.max(0, document.documentElement.clientHeight - 1),
             }),
         ];
     };
diff --git a/src/species/scroller.js b/src/species/scroller.js
index 3bc0258..d6e4b80 100644
--- a/src/species/scroller.js
+++ b/src/species/scroller.js
@@ -21,10 +21,10 @@ const getDefaultConfig = (randomizer) => {
 
         return [
             randomizer.natural({
-                max: documentWidth - documentElement.clientWidth,
+                max: Math.max(0, documentWidth - documentElement.clientWidth),
             }),
             randomizer.natural({
-                max: documentHeight - documentElement.clientHeight,
+                max: Math.max(0, documentHeight - documentElement.clientHeight),
             }),
         ];
     };
diff --git a/src/species/toucher.js b/src/species/toucher.js
index 7f8d8b5..3a05530 100644
--- a/src/species/toucher.js
+++ b/src/species/toucher.js
@@ -17,10 +17,10 @@ const getDefaultConfig = (randomizer) => {
     const defaultPositionSelector = () => {
         return [
             randomizer.natural({
-                max: document.documentElement.clientWidth - 1,
+                max: Math.max(0, document.documentElement.clientWidth - 1),
             }),
             randomizer.natural({
-                max: document.documentElement.clientHeight - 1,
+                max: Math.max(0, document.documentElement.clientHeight - 1),
             }),
         ];
     };
diff --git a/src/species/typer.js b/src/species/typer.js
index 852b569..1b59cfb 100644
--- a/src/species/typer.js
+++ b/src/species/typer.js
@@ -61,10 +61,10 @@ export default (userConfig) => (logger, randomizer) => {
         const eventType = randomizer.pick(config.eventTypes);
         const key = config.keyGenerator();
         const posX = randomizer.natural({
-            max: documentElement.clientWidth - 1,
+            max: Math.max(0, documentElement.clientWidth - 1),
         });
         const posY = randomizer.natural({
-            max: documentElement.clientHeight - 1,
+            max: Math.max(0, documentElement.clientHeight - 1),
         });
         const targetElement = config.targetElement(posX, posY);

Thanks for the report.

I agree with you fix, can you open a pull request ?

Done in #160.
Thanks