npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

casper-test-runner

v0.0.81

Published

This is a test runner built on casperjs.

Downloads

8

Readme

casper-test-runner

Prerequisites

This module runs on es6 and expects that your tests are also written in es6 and transpiled using babel before test run. To do this you can download babel-cli and babel-preset-es2015 and have an npm script that transpiles and the runs the casper tests

npm install babel-cli babel-preset-es2015 --save-dev

And in your package.json file

{
  "scripts": {
    "e2e-tests": "./node_modules/babel-cli/bin/babel.js tests/e2e/src --presets babel-preset-es2015 --out-dir tests/e2e/dist && ./node_modules/casperjs/bin/casperjs test tests/e2e/dist/index.js"
  }
}

Then to run

npm run e2e-tests

How To Use

Simply clone the repo

git clone https://github.com/spiffy2006/casper-test-runner.git

or npm install the module

npm install casper-test-runner --save-dev

Once it is installed create a your main test file

import CasperTestRunner from 'casper-test-runner';

let config = [ // these are the defaults
  'tests/e2e/dist/', // the location of your transpiled tests (path from project root)
  {width: 1024, height: 768, userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:36.0) Gecko/20100101 Firefox/36.0'},
  'https://www.google.com/' // the url to open on startup
};
let ctr = new CasperTestRunner(casper, phantom, ...config);

ctr.setCasperOptions({put: 'your', casper: 'options', here: 'please'});

ctr.run();

Writing Tests

Your tests will be written as an es6 class that takes one parameter in it's constructor. The parameter is a casper wrapper class that simplifies casper DOM interactions without all of the overhead of waiting for selectors and calling casper.then. Everything in the casper wrapper class is also promise based. So, for example, if you call evaluate you just chain a then to it, and get your data from the evaluate back.

The tests in your class need to be annotated with an @test in a DocBlock manner in order for the runner to recognize it as a test method.

Each test should run 1 assert. If you are using more than 1 assert per @test, then you need to break it up a little more. This ensures test granularity, and that the test runner can accurately determine the number of tests you are running.

Here is an example of a basic test class

export const description = "This is your class description. What are you testing?";
export default class MyFirstTestClass {
  constructor(cm) {
    this.cm = cm; // casper wrapper
    this.button = '.some-selector';
  }
  
  setUpBefore() {
    // This is where you will run anything that has a one time set up before the test(s) start running.
  }
  
  setUp() {
    // This method gets called before each test in your class
  }
  
  /**
  * This is a description of the test
  * @test <-- This is what tells the test runner that this is a test method
  * @param test
  */
  doTheThings(test) {
    // test is the casper test object that contains all of your assert functions that casper gives you
    this.cm.click(this.button); // click something
    test.assert(true, "true is true");
  }
  
  tearDown() {
    // This method is called after each test in your class
  }
  
  tearDownAfter() {
    // This method is called after all of your tests are completed
  }
}

Testing Tests

To test a test class or multiple test classes:

npm run e2e-tests -- --testFiles=path/to/your/test.js,path/to/different/test.js

testFiles is a comma separated string with the relative path from the test directory specified in your run.js file.

So if your tests are in tests/e2e/dist and your test class that you want to run is in tests/e2e/dist/google/testSearch.test.js you would run the command npm run e2e-tests -- --testFiles=google/testSearch.test.js to test that class.

To test specific tests in a file:

npm run e2e-tests -- --testFiles=path/to/your/test.js --tests=test1,test2,test3

tests is a comma separated string with the tests that you want to run in the testFiles variable.

Writing Library Classes

When writing classes that interact with the DOM the Page Object Model (POM) design pattern should be used. See this article as a reference POM

Example google.com homepage POM class

import GoogleSRP from './GoogleSRP';

export default class GoogleHomePage {
  constructor(cm) {
    this.cm = cm; // CasperManager instance
    this.casper = cm.getCasper(); // raw casper object
    this.phantom = cm.getPhantom(); // raw phantom object
    
    // page elements
    this.searchBox = '#lst-ib';
    this.searchBtn = '[name="btnK"]';
    this.feelingLuckyBtn = '#gbqfbb';
  }
  
  enterSearch(searchStr) {
    this.cm.sendKeys(this.searchBox, searchStr);
  }
  
  search(searchStr) {
    this.enterSearch(searchStr);
    this.cm.click(this.searchBtn);
    return new GoogleSRP(this.cm); // returns instance of GoogleSRP class for easy method chaining
  }
  
  feelingLucky(searchStr) {
    this.enterSearch(searchStr);
    this.cm.click(this.feelingLuckyBtn);
    return new GoogleSRP(this.cm); // returns instance of GoogleSRP class for easy method chaining
  }
  
  getSearchText() {
    return this.cm.getText(this.searchBox); // returns promise
  }
}