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

@4c/selenium-sandbox

v0.2.3

Published

easily mock a web application accessed through selenium. Contains also an environment for integrating with jest.

Downloads

23

Readme

selenium-sandbox

easily mock a web application accessed through selenium. Contains also an environment for integrating with jest.

Getting started

0. Install

yarn add -D @4c/selenium-sandbox

1. Add the sandbox to your test client

First, we need to instrument our application to use the sandbox environment. Create a module (let's call it injectSandbox.js)

import sandbox from '@4c/selenium-sandbox/browser';

const store = {
  widgets: [],
};

// additional properties and methods that can be accessed from selenium. for example here we are passing a store object
const context = { store };

sandbox.setupTestContext(context);

sandbox.fetchMock.mock('path:/api/v1/widgets', () => store.widgets);

Note: documentation for fetchMock can be found here

2. Instrument Selenium

Add utilities to an already existing selenium driver (v4):

import { augmentDriver } from '@4c/selenium-sandbox/webdriver';

const driver = augmentDriver(
  myBaseSeleniumDriver,
  'http://sandboxed-app-to-test',
);

... Alternatively you can use buildDriver to instantiate a reasonably opinionated driver:

import { buildDriver } from '@4c/selenium-sandbox/webdriver';

const driver = buildDriver(
  baseUrl: 'http://sandboxed-app-to-test';
  seleniumAddress: 'localhost:5000/wd/hub';
  browserName: 'chrome';
  screenSize: [1024, 768];
  // optional
  mobileEmulation: {
      deviceName: 'iPhone 6/7/8',
  };
);

3. Usage

To mock:

await driver.get('my/page');
await driver.executeInBrowser(context => {
  context.store.widgets = [{label: '1', label: '2'}]
}

const button = await driver.find('//button[text()="Get Widgets"]');
await driver.click(button);

If a request needs to be mocked before page loading, you can use executeAtStartup instead of executeInBrowser. the code will be stored in storage and executed when the assets are loaded

Note: Since the function passed to executeInBrowser or executeAtStartup needs to be stringified in order to be executed in the remote browser, it cannot access variables defined outside of its body.

You can also access fetchMock from the context to make additional mocks:

await driver.executeInBrowser(context => {
  context.fetchMock.mock('path:/api/v1/users', () => ['user1', 'user2'])
}

Since fetchMock is part of the context, it can be used to make assertions on the requests. There are two helper methods on the driver to facilitate that:

const request = await driver.getLastRequest();
invariant(request.headers.Authorization == 'Bearer XXX');

const requests = await driver.getRequests();
invariant(requests.every((req) => req.headers.Authorization == 'Bearer XXX'));

the driver has also the following utilities methods:

// more resilient than element.click, will retry several times to avoid flakiness
await driver.click(element);

// accepts an xpath query and returns the first match. It will wait that all
// images are loaded and that the element is indeed visible
await driver.find('//button[text()="Get Widgets"]');

// unlike the base webdriver, allows to pass a path which is concatenated
// to `baseUrl`
await driver.get('my/page');

// the name says it all. Useful to make sure the page has finished rendering
await driver.waitForAllImages();

Jest Support

There is also support for easy integration with Jest.

Environment

add the following line to your jest config:

{
  // ...
  testEnvironment: '@4c/selenium-sandbox/jest/environment.js',
  setupFilesAfterEnv: ['@4c/selenium-sandbox/jest/setup.js'],
  testEnvironmentOptions: {
    baseUrl: 'http://sandboxed-app-to-test',
    seleniumAddress: 'localhost:5000/wd/hub',
    browserName: 'chrome',
    screenSize: [1024, 768],
    // optional
    mobileEmulation: {
        deviceName: 'iPhone 6/7/8',
    },
    // optional, default 10000
    waitTimeout: 50000,
  },
}

this will:

  • declare a global browser variable as described above
  • set a 10s timeout which is a better fit for selenium tests
  • add a snapshot serializer specific to fetchMock requests

TypeScript Support

Type definitions come out of the box. For full jest support, you should add the following declaration to be used in your test file:

import { AugmentedDriver } from '@4c/selenium-sandbox/webdriver';

// define a context type that reflects the context passed in `setupTestContext`
type Context = {
  store: {
    widgets: {}[];
  };
};

declare const browser: AugmentedDriver<Context>;