
Security News
PyPI Expands Trusted Publishing to GitLab Self-Managed as Adoption Passes 25 Percent
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads
@4c/selenium-sandbox
Advanced tools
easily mock a web application accessed through selenium. Contains also an environment for integrating with jest.
easily mock a web application accessed through selenium. Contains also an environment for integrating with jest.
yarn add -D @4c/selenium-sandbox
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
fetchMockcan be found here
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',
};
);
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
executeInBrowserorexecuteAtStartupneeds 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();
There is also support for easy integration with Jest.
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:
browser variable as described aboveType 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>;
FAQs
easily mock a web application accessed through selenium. Contains also an environment for integrating with jest.
We found that @4c/selenium-sandbox demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads

Research
/Security News
A malicious Chrome extension posing as an Ethereum wallet steals seed phrases by encoding them into Sui transactions, enabling full wallet takeover.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.