Gathering detailed insights and metrics for @4c/selenium-sandbox
Gathering detailed insights and metrics for @4c/selenium-sandbox
npm install @4c/selenium-sandbox
Typescript
Module System
Node Version
NPM Version
61.9
Supply Chain
90.5
Quality
78.2
Maintenance
50
Vulnerability
98.9
License
TypeScript (98.64%)
JavaScript (1.36%)
Total Downloads
52,150
Last Day
4
Last Week
20
Last Month
57
Last Year
513
2 Stars
111 Commits
1 Forks
7 Watching
12 Branches
11 Contributors
Minified
Minified + Gzipped
Latest Version
0.2.3
Package Id
@4c/selenium-sandbox@0.2.3
Unpacked Size
16.69 kB
Size
6.11 kB
File Count
12
NPM Version
6.14.9
Node Version
14.15.0
Cumulative downloads
Total Downloads
Last day
100%
4
Compared to previous day
Last week
566.7%
20
Compared to previous week
Last month
35.7%
57
Compared to previous month
Last year
-77.7%
513
Compared to previous year
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
)
1import sandbox from '@4c/selenium-sandbox/browser'; 2 3const store = { 4 widgets: [], 5}; 6 7// additional properties and methods that can be accessed from selenium. for example here we are passing a store object 8const context = { store }; 9 10sandbox.setupTestContext(context); 11 12sandbox.fetchMock.mock('path:/api/v1/widgets', () => store.widgets);
Note: documentation for
fetchMock
can be found here
Add utilities to an already existing selenium driver (v4):
1import { augmentDriver } from '@4c/selenium-sandbox/webdriver';
2
3const driver = augmentDriver(
4 myBaseSeleniumDriver,
5 'http://sandboxed-app-to-test',
6);
... Alternatively you can use buildDriver to instantiate a reasonably opinionated driver:
1import { buildDriver } from '@4c/selenium-sandbox/webdriver';
2
3const driver = buildDriver(
4 baseUrl: 'http://sandboxed-app-to-test';
5 seleniumAddress: 'localhost:5000/wd/hub';
6 browserName: 'chrome';
7 screenSize: [1024, 768];
8 // optional
9 mobileEmulation: {
10 deviceName: 'iPhone 6/7/8',
11 };
12);
To mock:
1await driver.get('my/page');
2await driver.executeInBrowser(context => {
3 context.store.widgets = [{label: '1', label: '2'}]
4}
5
6const button = await driver.find('//button[text()="Get Widgets"]');
7await 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
orexecuteAtStartup
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:
1await driver.executeInBrowser(context => {
2 context.fetchMock.mock('path:/api/v1/users', () => ['user1', 'user2'])
3}
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:
1const request = await driver.getLastRequest(); 2invariant(request.headers.Authorization == 'Bearer XXX'); 3 4const requests = await driver.getRequests(); 5invariant(requests.every((req) => req.headers.Authorization == 'Bearer XXX'));
the driver has also the following utilities methods:
1// more resilient than element.click, will retry several times to avoid flakiness 2await driver.click(element); 3 4// accepts an xpath query and returns the first match. It will wait that all 5// images are loaded and that the element is indeed visible 6await driver.find('//button[text()="Get Widgets"]'); 7 8// unlike the base webdriver, allows to pass a path which is concatenated 9// to `baseUrl` 10await driver.get('my/page'); 11 12// the name says it all. Useful to make sure the page has finished rendering 13await driver.waitForAllImages();
There is also support for easy integration with Jest.
add the following line to your jest config:
1{ 2 // ... 3 testEnvironment: '@4c/selenium-sandbox/jest/environment.js', 4 setupFilesAfterEnv: ['@4c/selenium-sandbox/jest/setup.js'], 5 testEnvironmentOptions: { 6 baseUrl: 'http://sandboxed-app-to-test', 7 seleniumAddress: 'localhost:5000/wd/hub', 8 browserName: 'chrome', 9 screenSize: [1024, 768], 10 // optional 11 mobileEmulation: { 12 deviceName: 'iPhone 6/7/8', 13 }, 14 // optional, default 10000 15 waitTimeout: 50000, 16 }, 17}
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:
1import { AugmentedDriver } from '@4c/selenium-sandbox/webdriver'; 2 3// define a context type that reflects the context passed in `setupTestContext` 4type Context = { 5 store: { 6 widgets: {}[]; 7 }; 8}; 9 10declare const browser: AugmentedDriver<Context>;
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/22 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
44 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More