Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ps-george opened this issue Apr 15, 2020 · 4 comments
Closed

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

ps-george opened this issue Apr 15, 2020 · 4 comments

Comments

@ps-george
Copy link

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)

@ps-george
Copy link
Author

ps-george commented Apr 15, 2020

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

@zyhou
Copy link
Contributor

zyhou commented Apr 15, 2020

Thanks for the report.

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

@HALLERPierre
Copy link
Contributor

Done in #160.
Thanks

@zyhou
Copy link
Contributor

zyhou commented Apr 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants