Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Nightwatch.js provides a built-in JUnit reporter, which we'll use.The test


Info
titlePlease note

The tests below don't use the Page Objects pattern

...

. However, it would be a good recommendation to do so as mentioned by Nightwatch.js.

You may find other test examples, using the Page Objects pattern, in nightwatch.js repository here.



Test 1: Verify the content of a page on GitHub

Code Block
languagejs
titlepages/home.pagetest/e2e/github.js
collapsetrue
/**
 * Created by titusfortner on 11/23/16.
 */

var Pageconf = require('./page./../nightwatch.conf.js');

var HomePagemodule.exports = Object.create(Page, {

  'Demo test graphicsTabGitHub': function (browser) {
    browser
    get: function .url('http://www.github.com/dwyl') {
  // visit the   url
     return browser.elementwaitForElementVisible(`~Graphics`'body');
 // wait for the body to be }rendered
    },

  // check click:if {
we are seeing the Mobile Version of GitHub
      browser.element('css  value:selector', '.switch-to-desktop', function (tabNameresult) {
            if (tabName === "Graphics"result.status != -1) {
 //Element exists, do something
            this.graphicsTab.click();
browser.click('.switch-to-desktop')
          .waitForElementVisible('body'); // wait }for elsethe {
body to be rendered
        }
     throwError("Not implemented" });
    // part two:
      }browser
        }
    }

});


module.exports = HomePage;.assert.containsText('body', 'dwyl.com') // assert body contains text
      .saveScreenshot(conf.imgpath(browser) + 'dwyl.png')
      .end();
    }
  };


Test 2: Verify the title of some page
Test: Verify the existence of a meny entry

Code Block
languagejs
titletests/menu-testtest/e2e/guineaPig.js
collapsetrue
var expectconfig = require('chai').expect;

var HomePage = require('../../pages/home.pagenightwatch.conf.js'),;

module.exports = { // MenuPageaddapted = require('../pages/menu.page');

describe('Mocha Spec Sync example', function(from: https://git.io/vodU0
  '@tags': ['guineaPig'],
  'Guinea Pig Assert Title': function(browser) {
    browser
 it("verify Arcs entry in menu", function() {
  .url('https://saucelabs.com/test/guinea-pig')
      HomePage.click("Graphics");
  waitForElementVisible('body')
      .assert.title('I am a page title - Sauce Labs')
      expect.saveScreenshot(MenuPageconfig.arcsEntry.isVisibleimgpath()).to.equal(true);
browser) + 'a-screenshot-description.png')
      });
});

Running tests locally

Test(s) then can be run using NPM "test" task.

No Format
npm test

After successfully running the tests and generating the JUnit XML reports (e.g. androidemulator.android.5_1.apidemos-debug_apk?raw=true.xmlsamsunggalaxys4emulator.android.4_4.apidemos-debug_apk?raw=true.xml), it can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).

We'll use some shell-script sugar to do that for us and at the same time populate the Test Environment field on the Test Execution issues that will be created.

No Format
PROJECT=CALC
JIRASERVER=https://yourjiraserver
USERNAME=user
PASSWORD=pass
FIXVERSION=v3.0

for FILE in `ls *.xml`; do
 TESTENV=$(echo $FILE | cut -d "." -f 1-3)
 curl -H "Content-Type: multipart/form-data" -u $USERNAME:$PASSWORD -F "file=@$FILE"  "$JIRASERVER/rest/raven/1.0/import/execution/junit?projectKey=$PROJECT&testEnvironments=$TESTENV&fixVersion=$FIXVERSION"
done

In our case, two Test Executions will be created: one per each mobile device. Each one contains the same Test case.

Image Removed   Image Removed

Image Removed   Image Removed

.clearValue('#i_am_a_textbox')
      .setValue('#i_am_a_textbox', 'nightwatch roolz!')
      .saveScreenshot(config.imgpath(browser) + 'nightwatch-roolz.png')
      .end();
  }
};



Info
titlePlease note

Beware that some Javascripts, even though in the "tests" folder, may not be actual test cases (e.g. the file test/e2e/upload_screenshots_to_s3.js provided in the upstream project).


Test 3: upload screenshots to S3

Code Block
languagejs
titletest/e2e/upload_screenshots_to_s3.js
collapsetrue
require('env2')('.env'); // optionally store youre Evironment Variables in .env
var conf = require('../../nightwatch.conf.js')
var fs = require('fs'); // read the screenshot files
var path = require('path');
var AWS = require('aws-sdk');
var mime = require('mime-types');
AWS.config.region = process.env.AWS_REGION;
var s3bucket = new AWS.S3({params: {Bucket: process.env.AWS_S3_BUCKET}});

function s3_create () {
  if(!process.env.AWS_ACCESS_KEY_ID) {
    console.log(`If you want to upload Screenshots to S3
      please set your AWS Environment Variables (see readme).`);
  }
  else {
    var SP = conf.SCREENSHOT_PATH;
    var version = SP.split('/')[ SP.split('/').length - 2 ];
    fs.writeFileSync(conf.SCREENSHOT_PATH + 'index.html', // don't overwrite index
      fs.readFileSync(path.join(__dirname + '/index.html')), 'utf8');
    // fs.createReadStream(path.join(__dirname + '/index.html'))
    //   .pipe(fs.createWriteStream(conf.SCREENSHOT_PATH + 'index.html'));
    // fist read the list of screenshots
    var images = fs.readdirSync(SP).filter(file => {
      return fs.statSync(SP + file).isFile()
        && file.indexOf('.png') > -1; // only screenshot images
    })
    // create meta.json with list of screenshots
    var meta = {images: images}
    fs.writeFileSync(path.join(SP, 'meta.json'), JSON.stringify(meta, null, 2));

    // get list of files to upload to S3 (including meta.json & index.html)
    fs.readdirSync(SP).forEach(function (file) {
      var filepath = path.join(SP, file);
      var mimetype = mime.lookup(filepath);
      if (mimetype) {
        var s3path = version + '/uat' +
          filepath.split('node_modules/nightwatch/screenshots/' + version)[1];
        var s3obj = new AWS.S3({ params: {
          Bucket: process.env.AWS_S3_BUCKET,
          ACL: 'public-read',
          Key: s3path,
          ContentType: mimetype,
        }});
        // upload (stream) the files to S3 in parallel
        s3obj.upload({Body: fs.createReadStream(filepath)}).send(function(e, data) {
          if (e) {
            console.log(' >>> ERROR:', e);
          }
          if (filepath.indexOf('index.html') > -1) {
            console.log('Uploaded', images.length, 'screenshots >> ', data.Location);
          }
        });
      }
    });
  }
}
s3_create();

Running tests locally


Test(s) then can be run using NPM "test" task.

No Format
npm test


After successfully running the tests, JUnit XML files will be created, under the node_modules/nightwatch/reports/ folder (e.g. CHROME_67.0.3396.99_Mac OS X_github.xmlCHROME_67.0.3396.99_Mac OS X_guineaPig.xml).

Reports can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).


Image Added

 

Tests Mocha tests are mapped to Generic Tests in Jira, and the Generic Test Definition field contains the namespace, the name of the class, and the method name that implements the Test casefile along with the title of the function implementing the automate test.

The execution screen details will provide information on the overall test run result.


Image RemovedImage Added

Running in the cloud using SauceLabs

...

Multiple JUnit XML files will be created, one per capability (device+browser), under the node_modules/nightwatch/reports/ folder./reports/ folder.


Info
titlePlease note

Since the previous command generates multiple JUnit XML files, we may need to merge them into a single XML file so it can be submitted into a Test Execution more easily. That can be achieved by using the junit-merge utility. In this case,

Info
titlePlease note

By using the junit-merge utility we can merge multiple JUnit XML reports and upload the merged report to Xray, although we'll loose the visibility of results on a per Test Environment basis though.


junit-merge  -o results.xml -d node_modules/nightwatch/reports/

If we want to have visibility of the results per Test Environment, then we need to separately upload them and identify the Test Environment on each REST API request.

...