{"id":22556263,"url":"https://github.com/substrate-system/dom","last_synced_at":"2025-10-08T17:35:26.419Z","repository":{"id":213466165,"uuid":"734104754","full_name":"substrate-system/dom","owner":"substrate-system","description":"Helpers for working with the DOM in tests","archived":false,"fork":false,"pushed_at":"2025-03-26T05:26:14.000Z","size":145,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-30T11:34:37.688Z","etag":null,"topics":["dom","test"],"latest_commit_sha":null,"homepage":"https://substrate-system.github.io/dom/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/substrate-system.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":"2023-12-20T22:04:00.000Z","updated_at":"2025-03-26T05:26:11.000Z","dependencies_parsed_at":"2023-12-21T07:07:40.515Z","dependency_job_id":"e89368be-1efc-47ce-b38e-7b48be2128d2","html_url":"https://github.com/substrate-system/dom","commit_stats":null,"previous_names":["nichoth/dom","nichoth/test-dom","substrate-system/dom","bicycle-codes/dom"],"tags_count":41,"template":false,"template_full_name":"nichoth/template-ts-browser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/substrate-system%2Fdom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/substrate-system%2Fdom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/substrate-system%2Fdom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/substrate-system%2Fdom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/substrate-system","download_url":"https://codeload.github.com/substrate-system/dom/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247584329,"owners_count":20962110,"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":["dom","test"],"created_at":"2024-12-07T19:11:45.059Z","updated_at":"2025-10-08T17:35:21.383Z","avatar_url":"https://github.com/substrate-system.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dom\n![tests](https://github.com/substrate-system/dom/actions/workflows/nodejs.yml/badge.svg)\n[![types](https://img.shields.io/npm/types/msgpackr?style=flat-square)](README.md)\n[![module](https://img.shields.io/badge/module-ESM%2FCJS-blue?style=flat-square)](README.md)\n[![dependencies](https://img.shields.io/badge/dependencies-zero-brightgreen.svg?style=flat-square)](package.json)\n[![semantic versioning](https://img.shields.io/badge/semver-2.0.0-blue?logo=semver\u0026style=flat-square)](https://semver.org/)\n[![install size](https://flat.badgen.net/packagephobia/install/@substrate-system/dom)](https://packagephobia.com/result?p=@substrate-system/dom)\n[![Common Changelog](https://nichoth.github.io/badge/common-changelog.svg)](https://common-changelog.org)\n[![license](https://img.shields.io/badge/license-Polyform_Non_Commercial-26bc71?style=flat-square)](LICENSE)\n\n\nHelpers for working with the DOM; useful for tests.\n\n[Read the docs](https://substrate-system.github.io/dom/)\n\n## install\n```sh\nnpm i -D @substrate-system/dom\n```\n\n## use\n\n### import\n```js\nimport { dom } from '@substrate-system/dom'\n\n// or import individual functions\nimport { waitFor } from '@substrate-system/dom'\n```\n\n### require\n```js\nconst dom = require('@substrate-system/dom').dom\n```\n\n## API\n\n### convenient shortcuts\n\n__`dom.qs`__ points to `document.querySelector`\n\n__`dom.qsa`__ is equal to `document.querySelectorAll`\n\n__`dom.byId`__ is equal to `document.getElementById`\n\n\n-------\n\n\n### `waitFor`\nLook for a DOM element by slector. Default timeout is 5 seconds. Throws if the element is not found.\n\n```ts\nfunction waitFor (selector?:string|null, args?:{\n    visible?:boolean,\n    timeout?:number\n}|null, lambda?):Promise\u003cElement|null\u003e\n```\n\n#### `waitFor` example\n```js\nimport { waitFor } from '@substrate-system/dom'\n\n// or pass in a query selector string\nconst el = await waitFor('#my-element')\n\n// example of using a lambda function only\nconst el2 = dom.waitFor(null, null, () =\u003e {\n    return document.querySelector('p')\n})\n```\n\n### `waitForText`\nLook for an element containing the given text, or that matches a given regex. Return the element if found. Default timeout is 5 seconds. Throws if the element is not found.\n\nTakes either an option object or a string of text.\n\n```ts\nfunction waitForText (args:Partial\u003c{\n    text:string,\n    timeout:number,\n    multipleTags:boolean,\n    regex:RegExp\n}\u003e|string, parentElement:Element = document.body):Promise\u003cElement|null\u003e\n```\n\n#### `waitForText` example\n\n```js\nimport { waitForText } from '@substrate-system/dom'\n\n// by default will search the document.body\nconst el = await waitForText({\n    regex: /bar/\n})\n```\n\n##### Pass in a string selector\nCan pass in a string to search for. Will search the `document.body` by default.\n\n```js\nimport { waitForText } from '@substrate-system/dom'\n\nconst el = await dom.waitForText('bar')\n```\n\n##### Pass in a parent element and timeout.\n```js\nconst found = await waitForText({\n    element: dom.qs('#test-two'),\n    multipleTags: true,\n    text: 'bbb',\n    timeout: 10000  // 10 seconds\n})\n```\n\n### `click`\nDispatch a click event from the given element.\n\n```ts\nasync function click (selector:Element|string):Promise\u003cvoid\u003e\n```\n\n#### `click` example\n\n```js\nimport { dom } from '@substrate-system/dom'\n// or import { click } from '@substrate-system/dom'\n\ndom.click(dom.qs('#my-element'))\n\n// or pass a selector\ndom.click('#my-element')\n```\n\n### `event`\nDispatch an event from an element. Will dispatch from `window` if no element is passed in.\n\n```ts\nfunction event (\n    event:CustomEvent|Event|string,\n    element?:Element|Window|null\n):void\n```\n\n#### `event` example\n```js\nimport { dom } from '@substrate-system/dom'\n\n// pass in an event name. Will create a custom event.\ndom.event('hello', dom.qs('#test'))\n\n// create an event, then dispatch it\ndom.event(\n    new CustomEvent('test-event', {\n        bubbles: true,\n        detail: 'test'\n    }),\n    dom.qs('#test-two')\n)\n```\n\n### `sleep`\nWait for the given milliseconds.\n\n```ts\nasync function sleep (ms:number):Promise\u003cvoid\u003e\n```\n\n#### `sleep` example\n```js\nimport { sleep } from '@substrate-system/dom'\n\nawait sleep(3000)  // wait 3 seconds\n```\n\n### `type`\nEnter text into an input. This will simulate typing by dispatching `input` events.\n\n```ts\nasync function type (\n    selector:string|HTMLElement|Element,\n    value:string,\n):Promise\u003cvoid\u003e\n```\n\n#### `type` example\n\n```js\nimport { type } from '@substrate-system/dom'\n\n// this will dispatch 5 `input` events,\n// one for each character\nawait type('#test', 'hello')\n```\n\n## credits\n\nThanks [@raynos](https://github.com/raynos/) for writing this originally.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubstrate-system%2Fdom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsubstrate-system%2Fdom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubstrate-system%2Fdom/lists"}