GithubHelp home page GithubHelp logo

isabella232 / node-sauce-template Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 18f/node-sauce-template

0.0 0.0 0.0 180 KB

A Node.js template for cross-browser functional tests with Sauce Labs

License: Other

JavaScript 100.00%

node-sauce-template's Introduction

node-sauce-template

This is a Node template for cross-browser functional tests with Selenium on Sauce Labs. The stack is all Node, but the site to test can be statically or dynamically served with any language.

Before you start: You'll need to create a Sauce Labs account if you don't have one already.

Components

There are a couple of moving parts in this repository that you should know about before proceding:

  1. The test server in server.js is an Express app that renders an HTML page with the title, "Hello, world!".
  2. The test suite in test/selenium.js is a Mocha test script that uses the Selenium WebDriver API to test the HTML rendered by our test server in a remote browser.
  3. The sauceconnect-runner dependency and .env.example template simplify running Selenium tests on remote Sauce Labs virtual machines against your local test server via Sauce Connect.
  4. The multitest.js script reads a list of Selenium-compatible browser configurations from browsers.json and runs the test suite against each of them in parallel. See Multi-Browser Tests for more info.

Setup

Clone this repo and install the dependencies via npm:

git clone https://github.com/shawnbot/node-sauce-template.git
cd node-sauce-template
npm install

Verify

Next up, you can verify that the test server works by calling npm start. This fires up an Express server that serves up a page with an HTML title, "Hello, world!". You can confirm that it's running by visiting http://localhost:9000 in a web browser, or:

curl -s http://localhost:9000

Note: In your project environment, the server (i.e., the "start" script in package.json) would either start up your web application or start a local HTTP server to serve static files from your project directory.

Configure Sauce Labs

Before you can run the Selenium tests, you'll need to set some environment variables so that the scripts can pass your credentials along to Sauce. The easiest way to do this is to copy the provided .env.example file to .env:

cp .env.example .env

Then edit it to ensure that the SAUCE_USERNAME and SAUCE_ACCESS_KEY values match those of your Sauce Labs account. You can also do it in your shell manually like so:

export SAUCE_USERNAME="your-sauce-username"
export SAUCE_ACCESS_KEY="your-sauce-acess-key"

I suggest copying the file to .env and using autoenv to have it sourced whenever you cd into this directory.

Test Sauce Connect

Sauce Connect allows you to run virtual machine tests on Sauce Labs against your local web server. Once your environment variables area set, you can run:

npm run sauce-connect

You should see output similar to below and ending with a line of green text that reads, "Sauce Connect is running". If you don't, check your Sauce credentials and try again.

image

Run the Tests!

Okay, now you're ready to start testing. You're going to need two shells for this step, so get the other one ready (by opening a new window or tab in your terminal app, for instance) and cd into this directory. In the first shell, start the server:

npm start

Then, in the next shell, run the tests:

npm test

If all goes well, you should see some text like this:

image

If you look at package.json, you can see that this is using sauceconnect-runner's sc-run utility to create the Sauce Connect tunnel "around" the Mocha tests. If you're having trouble with sc-run, you can try decoupling it from the tests by running three shells in parallel:

  • npm start to start the server
  • npm run sauce-connect (or ./node_modules/.bin/sc-run) to create the Sauce Connect tunnel
  • npm test to run the test

One-Off Browser Tests

As you can see in test/selenium.js, you can use the BROWSER environment variable to override the default remote browser configuration. (This is how the multi-browser tests are run.) For instance, if you want to run your tests against Internet Explorer 9 on Windows, you could run:

BROWSER='{"browserName":"internet explorer","version":"9.0"}' npm test

Multi-Browser Tests

By default, the Selenium test suite is only run against Chrome on Sauce Labs' default platform (Linux as of this writing). The included multitest.js allows you to run the tests against all of the browser configurations specified in browsers.json in parallel, and exits with non-zero status if any of the tests fail.

To run the multi-browser tests, just substitute node multitest.js (or npm run test-multi) for npm test (i.e. mocha) in the procedure above. You can modify browsers.json and specify as many browser/platform configurations as your project needs. Just be warned that remote Selenium tests take much longer to run than local tests, and that your workflow may be impeded by tests that take too long to run. You can use the --serial flag to run the tests in serial, in which case the first failing test will abort the process:

node multitest.js --serial

node-sauce-template's People

Contributors

shawnbot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.