Pill Factory - skinnyjames/screen-pill GitHub Wiki

Screen Pill comes with a Pill Factory helper that can mixin to your cucumber runner via a Custom World

The Pill Factory exposes the methods on, and visit: both take an optional callback with the screen-pill as the parameter

To use it, create a custom world function.


//env.js
const { setWorldConstructor } = require('cucumber')
const webdriver = require('selenium-webdriver')
const driver = new webdriver.Builder().forBrowser('chrome').build()

function CustomWorld() {
  // set the driver on world
  this.driver = driver
}
// Mixin the PillFactory into your CustomWorld function
let World = PillFactory(CustomWorld)
setWorldConstructor(World)

Then, in your stepdefs, you have access to the factory methods

//steps.js
const { Given, When, Then } = require('cucumber')
const chai = require('chai')
const expect = chai.expect
// require a screen pill
var GoogleSearch = require('./pills/google-search')
var GoogleResults = require('./pills/google-results')

Given('I go to google', async function() {
  // visit will instantiate the pill function with a driver
  // and go to the pill's directUrl
  return this.visit(GoogleSearch)
})

When(/I search (.*)/, async function(term) {
  return this.on(GoogleSearch, async function(pill) {
    // pill methods can be called in the callback
    await pill.query(term)
  })
})

Then('I get results', async function() {
 return this.on(GoogleResults, async function(pill) {
    await pill.stats.waitUntilPresent()
    let stats = await pill.stats.get()
    expect(stats).to.match(/About/)
  })
})