{"id":19282442,"url":"https://github.com/4catalyzer/selenium-sandbox","last_synced_at":"2025-04-22T01:31:42.198Z","repository":{"id":46025165,"uuid":"168610288","full_name":"4Catalyzer/selenium-sandbox","owner":"4Catalyzer","description":"Set of utilities to mock requests using selenium","archived":false,"fork":false,"pushed_at":"2024-11-05T23:10:38.000Z","size":1253,"stargazers_count":2,"open_issues_count":11,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-06T00:19:21.942Z","etag":null,"topics":["jest","mocking","selenium"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4Catalyzer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-31T23:04:21.000Z","updated_at":"2020-12-07T18:39:51.000Z","dependencies_parsed_at":"2023-12-24T08:23:59.420Z","dependency_job_id":"92ee1d13-6732-47f3-bf6e-dcd1b9cbdc91","html_url":"https://github.com/4Catalyzer/selenium-sandbox","commit_stats":{"total_commits":109,"total_committers":6,"mean_commits":"18.166666666666668","dds":0.5412844036697247,"last_synced_commit":"79dedaf3d420d74b867d302ba405113c0eb8791b"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fselenium-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fselenium-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fselenium-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4Catalyzer%2Fselenium-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4Catalyzer","download_url":"https://codeload.github.com/4Catalyzer/selenium-sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223886186,"owners_count":17219690,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jest","mocking","selenium"],"created_at":"2024-11-09T21:26:55.643Z","updated_at":"2024-11-09T21:26:56.222Z","avatar_url":"https://github.com/4Catalyzer.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# selenium-sandbox\n\neasily mock a web application accessed through selenium. Contains also an environment for integrating with jest.\n\n## Getting started\n\n### 0. Install\n\n    yarn add -D @4c/selenium-sandbox\n\n### 1. Add the sandbox to your test client\n\nFirst, we need to instrument our application to use the sandbox environment. Create a module (let's call it `injectSandbox.js`)\n\n```js\nimport sandbox from '@4c/selenium-sandbox/browser';\n\nconst store = {\n  widgets: [],\n};\n\n// additional properties and methods that can be accessed from selenium. for example here we are passing a store object\nconst context = { store };\n\nsandbox.setupTestContext(context);\n\nsandbox.fetchMock.mock('path:/api/v1/widgets', () =\u003e store.widgets);\n```\n\n\u003e **Note**: documentation for `fetchMock` can be found [here](https://github.com/wheresrhys/fetch-mock)\n\n### 2. Instrument Selenium\n\nAdd utilities to an already existing selenium driver (v4):\n\n```js\nimport { augmentDriver } from '@4c/selenium-sandbox/webdriver';\n\nconst driver = augmentDriver(\n  myBaseSeleniumDriver,\n  'http://sandboxed-app-to-test',\n);\n```\n\n... Alternatively you can use buildDriver to instantiate a reasonably opinionated driver:\n\n```js\nimport { buildDriver } from '@4c/selenium-sandbox/webdriver';\n\nconst driver = buildDriver(\n  baseUrl: 'http://sandboxed-app-to-test';\n  seleniumAddress: 'localhost:5000/wd/hub';\n  browserName: 'chrome';\n  screenSize: [1024, 768];\n  // optional\n  mobileEmulation: {\n      deviceName: 'iPhone 6/7/8',\n  };\n);\n```\n\n### 3. Usage\n\nTo mock:\n\n```js\nawait driver.get('my/page');\nawait driver.executeInBrowser(context =\u003e {\n  context.store.widgets = [{label: '1', label: '2'}]\n}\n\nconst button = await driver.find('//button[text()=\"Get Widgets\"]');\nawait driver.click(button);\n```\n\nIf 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\n\n\u003e **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.\n\nYou can also access fetchMock from the context to make additional mocks:\n\n```js\nawait driver.executeInBrowser(context =\u003e {\n  context.fetchMock.mock('path:/api/v1/users', () =\u003e ['user1', 'user2'])\n}\n```\n\nSince `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:\n\n```js\nconst request = await driver.getLastRequest();\ninvariant(request.headers.Authorization == 'Bearer XXX');\n\nconst requests = await driver.getRequests();\ninvariant(requests.every((req) =\u003e req.headers.Authorization == 'Bearer XXX'));\n```\n\nthe driver has also the following utilities methods:\n\n```js\n// more resilient than element.click, will retry several times to avoid flakiness\nawait driver.click(element);\n\n// accepts an xpath query and returns the first match. It will wait that all\n// images are loaded and that the element is indeed visible\nawait driver.find('//button[text()=\"Get Widgets\"]');\n\n// unlike the base webdriver, allows to pass a path which is concatenated\n// to `baseUrl`\nawait driver.get('my/page');\n\n// the name says it all. Useful to make sure the page has finished rendering\nawait driver.waitForAllImages();\n```\n\n## Jest Support\n\nThere is also support for easy integration with Jest.\n\n### Environment\n\nadd the following line to your jest config:\n\n```js\n{\n  // ...\n  testEnvironment: '@4c/selenium-sandbox/jest/environment.js',\n  setupFilesAfterEnv: ['@4c/selenium-sandbox/jest/setup.js'],\n  testEnvironmentOptions: {\n    baseUrl: 'http://sandboxed-app-to-test',\n    seleniumAddress: 'localhost:5000/wd/hub',\n    browserName: 'chrome',\n    screenSize: [1024, 768],\n    // optional\n    mobileEmulation: {\n        deviceName: 'iPhone 6/7/8',\n    },\n    // optional, default 10000\n    waitTimeout: 50000,\n  },\n}\n```\n\nthis will:\n\n- declare a global `browser` variable as described above\n- set a 10s timeout which is a better fit for selenium tests\n- add a snapshot serializer specific to fetchMock requests\n\n## TypeScript Support\n\nType definitions come out of the box. For full jest support, you should add the following declaration to be used in your test file:\n\n```ts\nimport { AugmentedDriver } from '@4c/selenium-sandbox/webdriver';\n\n// define a context type that reflects the context passed in `setupTestContext`\ntype Context = {\n  store: {\n    widgets: {}[];\n  };\n};\n\ndeclare const browser: AugmentedDriver\u003cContext\u003e;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4catalyzer%2Fselenium-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4catalyzer%2Fselenium-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4catalyzer%2Fselenium-sandbox/lists"}