Repository: Codeception/CodeceptJS (js)

Issue:
windowSize has no effect [Nightmare]
#### What are you trying to achieve?

I want the window to enlarge the window on boot

#### What do you get instead?

The window always shows at its default size



### Details

* CodeceptJS version: 0.4.9
* NodeJS Version: 7.1.0
* Operating System: OS X Sierra
* Nightmare: 2.8.1
* Configuration file:

```js
...
    "Nightmare": {
      "show": true,
      "waitForAction": 5000,
      "windowSize": "1000x1000",
      "typeInterval": 20,      
      "webPreferences": {
        "partition": "codecept-acceptance"
      }
    }
...
```


Pull request description:
Fixed resizing in nightmare and added a test for it
Fixes #301 

Patch:
diff --git a/.travis.yml b/.travis.yml
index df11f313..626e4ebc 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -17,14 +17,17 @@ addons:
     packages:
       - php5-cli
 
+services:
+  - docker
+
 before_install:
-  - wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
+  - docker pull selenium/standalone-chrome:2.53.0
 
 before_script:
-  - chmod -R 777 test/data
+  - docker run -d --net=host selenium/standalone-chrome:2.53.0
   - export DISPLAY=:99.0
   - sh -e /etc/init.d/xvfb start
-  - "java -jar selenium-server-standalone-2.53.0.jar -port 4444 >/dev/null 2>&1 &"
+  - chmod -R 777 test/data
   - "php -S 127.0.0.1:8000 -t test/data/app >/dev/null 2>&1 &"
   - sleep 10
 
diff --git a/lib/actor.js b/lib/actor.js
index e963f852..03eb8ce7 100644
--- a/lib/actor.js
+++ b/lib/actor.js
@@ -4,6 +4,7 @@ let container = require('./container');
 let methodsOfObject = require('./utils').methodsOfObject;
 let recorder = require('./recorder');
 let event = require('./event');
+let output = require('./output');
 
 /**
  * Fetches all methods from all enabled helpers,
@@ -14,10 +15,10 @@ module.exports = function (obj) {
   obj = obj || {};
 
   let helpers = container.helpers();
+
+  // add methods from enabled helpers
   Object.keys(helpers)
-    .map(function (key) {
-      return helpers[key];
-    })
+    .map((key) => helpers[key])
     .forEach((helper) => {
       methodsOfObject(helper, 'Helper')
       .filter((method) => {
@@ -32,11 +33,15 @@ module.exports = function (obj) {
             step.name = actionAlias;
             step.actor = container.translation().I;
           }
+          // add methods to promise chain
           return recordStep(step, Array.prototype.slice.call(arguments));
         };
       });
     });
 
+  // add print comment method
+  obj.say = (msg) => recorder.add(`say ${msg}`, () => output.say(msg));
+
   return obj;
 };
 
diff --git a/lib/helper/Nightmare.js b/lib/helper/Nightmare.js
index 454bdcc3..b05eb15b 100644
--- a/lib/helper/Nightmare.js
+++ b/lib/helper/Nightmare.js
@@ -159,13 +159,13 @@ class Nightmare extends Helper {
 
   _beforeSuite() {
     if (!this.options.restart) {
-      this._startBrowser();
+      return this._startBrowser();
     }
   }
 
   _before() {
     if (this.options.restart) {
-      this._startBrowser();
+      return this._startBrowser();
     }
     return this.browser;
   }
@@ -192,10 +192,8 @@ class Nightmare extends Helper {
     });
 
     if (this.options.windowSize) {
-      if (Array.isArray(this.options.windowSize)) {
-        let size = this.options.windowSize.split('x');
-        this.browser.viewport(size[0], size[1]);
-      }
+      let size = this.options.windowSize.split('x');
+      return this.browser.viewport(parseInt(size[0]), parseInt(size[1]));
     }
   }
 
@@ -735,6 +733,9 @@ class Nightmare extends Helper {
    * {{> ../webapi/clearCookie}}
    */
   clearCookie(cookie) {
+    if (!cookie) {
+      return this.browser.cookies.clearAll();
+    }
     return this.browser.cookies.clear(cookie);
   }
 
diff --git a/lib/helper/Protractor.js b/lib/helper/Protractor.js
index 8f15f9f5..822f59b2 100644
--- a/lib/helper/Protractor.js
+++ b/lib/helper/Protractor.js
@@ -42,6 +42,7 @@ let withinStore = {};
  * * `restart` - restart browser between tests (default: true), if set to false cookies will be cleaned but browser window will be kept.
  * * `seleniumAddress` - Selenium address to connect (default: http://localhost:4444/wd/hub)
  * * `rootElement` - Root element of AngularJS application (default: body)
+ * * `windowSize`: (optional) default window size. Set to `maximize` or a dimension in the format `640x480`.
  * * `manualStart` (optional, default: false) - do not start browser before a test, start it manually inside a helper with `this.helpers["WebDriverIO"]._startBrowser()`
  * * `capabilities`: {} - list of [Desired Capabilities](https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities)
  * * `proxy`: set proxy settings
@@ -66,6 +67,7 @@ class Protractor extends SeleniumWebdriver {
       seleniumAddress: 'http://localhost:4444/wd/hub',
       rootElement: 'body',
       scriptsTimeout: 10000,
+      windowSize: null,
       driver: 'hosted',
       capabilities: {}
     };
@@ -130,6 +132,16 @@ class Protractor extends SeleniumWebdriver {
     global.element = browser.element;
     this.browser = protractorWrapper(browser, this.options.url, this.options.rootElement);
     this.browser.ready = this.browser.manage().timeouts().setScriptTimeout(this.options.scriptsTimeout);
+
+    if (this.options.windowSize == 'maximize') {
+      this.resizeWindow(this.options.windowSize);
+    }
+
+    if (this.options.windowSize) {
+      var size = this.options.windowSize.split('x');
+      this.resizeWindow(parseInt(size[0]), parseInt(size[1]));
+    }
+
     return this.browser;
   }
 
diff --git a/lib/helper/SeleniumWebdriver.js b/lib/helper/SeleniumWebdriver.js
index c31e2004..f942ed62 100644
--- a/lib/helper/SeleniumWebdriver.js
+++ b/lib/helper/SeleniumWebdriver.js
@@ -45,6 +45,7 @@ let withinStore = {};
  * * `seleniumAddress` - Selenium address to connect (default: http://localhost:4444/wd/hub)
  * * `waitForTimeout`: (optional) sets default wait time in _ms_ for all `wait*` functions. 1000 by default;
  * * `scriptTimeout`: (optional) sets default timeout for scripts in `executeAsync`. 1000 by default.
+ * * `windowSize`: (optional) default window size. Set to `maximize` or a dimension in the format `640x480`.
  * * `manualStart` (optional, default: false) - do not start browser before a test, start it manually inside a helper with `this.helpers["WebDriverIO"]._startBrowser()`
  * * `capabilities`: {} - list of [Desired Capabilities](https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities)
  *
@@ -67,6 +68,7 @@ class SeleniumWebdriver extends Helper {
       url: 'http://localhost',
       seleniumAddress: 'http://localhost:4444/wd/hub',
       restart: true,
+      windowSize: null,
       waitforTimeout: 1000, // ms
       scriptTimeout: 1000, // ms
       manualStart: false,
@@ -108,6 +110,16 @@ class SeleniumWebdriver extends Helper {
 
   _startBrowser() {
     this.browser = this.browserBuilder.build();
+
+    if (this.options.windowSize == 'maximize') {
+      this.resizeWindow(this.options.windowSize);
+    }
+
+    if (this.options.windowSize) {
+      var size = this.options.windowSize.split('x');
+      this.resizeWindow(parseInt(size[0]), parseInt(size[1]));
+    }
+
     return this.browser;
   }
 
@@ -119,7 +131,9 @@ class SeleniumWebdriver extends Helper {
   }
 
   _before() {
-    if (this.options.restart && !this.options.manualStart) return this._startBrowser();
+    if (this.options.restart && !this.options.manualStart) {
+      return this._startBrowser();
+    }
   }
 
   _after() {
diff --git a/lib/helper/WebDriverIO.js b/lib/helper/WebDriverIO.js
index 6a9d6dd1..da438663 100644
--- a/lib/helper/WebDriverIO.js
+++ b/lib/helper/WebDriverIO.js
@@ -54,7 +54,7 @@ let withinStore = {};
  *      "WebDriverIO" : {
  *        "browser": "chrome",
  *        "restart": false,
- *        "windowSize": "maximize,
+ *        "windowSize": "maximize",
  *        "timeouts": {
  *          "script": 60000,
  *          "page load": 10000,


Test patch:
diff --git a/examples/github_test.js b/examples/github_test.js
index 209437cd..060ca3bf 100644
--- a/examples/github_test.js
+++ b/examples/github_test.js
@@ -14,6 +14,7 @@ Scenario('search', (I) => {
 
 Scenario('signin', (I) => {
   I.click('Sign in');
+  I.say('it should not enter');
   I.see('Sign in to GitHub');
   I.fillField('Username or email address', 'something@totest.com');
   I.fillField('Password', '123456');
diff --git a/test/data/app/db b/test/data/app/db
index 23fcf75d..be6e9466 100644
--- a/test/data/app/db
+++ b/test/data/app/db
@@ -1,1 +1,1 @@
-{"form":{"LoginForm":{"username":"","password":"","rememberMe":"0"},"yt0":""},"files":[]}
\ No newline at end of file
+{"params":[]}
\ No newline at end of file
diff --git a/test/data/app/view/form/resize.php b/test/data/app/view/form/resize.php
new file mode 100644
index 00000000..fb7d7795
--- /dev/null
+++ b/test/data/app/view/form/resize.php
@@ -0,0 +1,22 @@
+<html>
+<head>
+<script type="text/javascript">
+function printWindowSize() {
+  document.getElementById('height').innerText = 'Height '+ window.outerHeight;
+  document.getElementById('width').innerText = 'Width '+ window.outerWidth;
+}
+</script>
+</head>
+<body>
+
+<div>
+    <button onclick="printWindowSize()">Window Size</button>
+    <div id="height"></div>
+    <div id="width"></div>
+</div>
+
+
+
+
+</body>
+</html>
diff --git a/test/helper/Nightmare_test.js b/test/helper/Nightmare_test.js
index 5bd769fc..fc2f8969 100644
--- a/test/helper/Nightmare_test.js
+++ b/test/helper/Nightmare_test.js
@@ -25,6 +25,7 @@ describe('Nightmare', function () {
 
     I = new Nightmare({
       url: site_url,
+      windowSize: '500x400',
       show: false
     });
     I._init();
diff --git a/test/helper/Protractor_test.js b/test/helper/Protractor_test.js
index ed870c14..43db0278 100644
--- a/test/helper/Protractor_test.js
+++ b/test/helper/Protractor_test.js
@@ -30,7 +30,7 @@ describe('Protractor', function() {
 
   before(() => {
     global.codecept_dir = path.join(__dirname, '../data');
-    I = new Protractor({url: site_url});
+    I = new Protractor({url: site_url, browser: 'chrome'});
     I._init();
     I._beforeSuite();
   });
diff --git a/test/helper/SeleniumWebdriver_test.js b/test/helper/SeleniumWebdriver_test.js
index f5a3f46c..ef0bc6c4 100644
--- a/test/helper/SeleniumWebdriver_test.js
+++ b/test/helper/SeleniumWebdriver_test.js
@@ -24,7 +24,8 @@ describe('SeleniumWebdriver', function () {
 
     I = new SeleniumWebdriver({
       url: site_url,
-      browser: 'firefox'
+      browser: 'chrome',
+      windowSize: '500x400'
     });
     I._init();
     browser = I._before();
diff --git a/test/helper/WebDriverIO_test.js b/test/helper/WebDriverIO_test.js
index 993f9a9b..8960f742 100644
--- a/test/helper/WebDriverIO_test.js
+++ b/test/helper/WebDriverIO_test.js
@@ -25,7 +25,8 @@ describe('WebDriverIO', function () {
 
     wd = new WebDriverIO({
       url: site_url,
-      browser: 'firefox'
+      browser: 'chrome',
+      windowSize: '500x400'
     });
   });
 
@@ -164,18 +165,6 @@ describe('WebDriverIO', function () {
     });
   });
 
-  describe('window size : #resizeWindow', () => {
-    it('should change the active window size', () => {
-      return wd.amOnPage('/')
-        .then(() => wd.resizeWindow(640, 480))
-        .then(function () { return this.windowHandleSize((err, size) => {
-          assert.equal(size.value.width, 640);
-          return assert.equal(size.value.height, 480);
-        });
-      });
-    });
-  });
-
   describe('popup : #acceptPopup, #seeInPopup, #cancelPopup', () => {
     it('should accept popup window', () => {
       return wd.amOnPage('/form/popup')
@@ -297,5 +286,4 @@ describe('WebDriverIO', function () {
         .then((res) => res.length.should.be.equal(0))
     });
   });
-
 });
diff --git a/test/helper/webapi.js b/test/helper/webapi.js
index d681d81d..9734c3cc 100644
--- a/test/helper/webapi.js
+++ b/test/helper/webapi.js
@@ -479,6 +479,13 @@ module.exports.tests = function() {
         .then(() => I.clearCookie('auth'))
         .then(() => I.dontSeeCookie('auth'));
     });
+
+    it('should clear all cookies', () => {
+      return I.amOnPage('/')
+        .then(() => I.setCookie({name: 'auth', value: '123456'}))
+        .then(() => I.clearCookie())
+        .then(() => I.dontSeeCookie('auth'));
+    });
   });
 
   describe('#waitForText', () => {
@@ -497,6 +504,23 @@ module.exports.tests = function() {
     });
   });
 
+  describe('window size #resizeWindow', () => {
+    it('should set initial window size', () => {
+      return I.amOnPage('/form/resize')
+        .then(() => I.click('Window Size'))
+        .then(() => I.see('Height 400', '#height'))
+        .then(() => I.see('Width 500', '#width'))
+    });
+
+    it('should resize window to specific dimensions', () => {
+      return I.amOnPage('/form/resize')
+        .then(() => I.resizeWindow(800, 600))
+        .then(() => I.click('Window Size'))
+        .then(() => I.see('Height 600', '#height'))
+        .then(() => I.see('Width 800', '#width'))
+    });
+  });
+
   describe('#waitForElement', () => {
     it('should wait for visibile element', () => {
       return I.amOnPage('/form/wait_visible')
diff --git a/test/unit/actor_test.js b/test/unit/actor_test.js
index d21bdf84..0b233847 100644
--- a/test/unit/actor_test.js
+++ b/test/unit/actor_test.js
@@ -23,8 +23,8 @@ describe('Actor', () => {
     I = actor();
   });
 
-  it('should take all methods of helpers', () => {
-    I.should.have.keys(['hello', 'bye', 'greeting']);
+  it('should take all methods from helpers and built in', () => {
+    I.should.have.keys(['hello', 'bye', 'greeting', 'say']);
   });
 
   it('should return promise', () => {