Skip to content

Instantly share code, notes, and snippets.

@dominikkaegi
Last active November 12, 2019 16:33
Show Gist options
  • Select an option

  • Save dominikkaegi/e7eb5e56d90558cb836df66f09f0f945 to your computer and use it in GitHub Desktop.

Select an option

Save dominikkaegi/e7eb5e56d90558cb836df66f09f0f945 to your computer and use it in GitHub Desktop.
Logs all data test ids with console.group. Within a group the reference to the element is available.
function logIds() {
function getDataTestIdElements() {
return document.querySelectorAll('[data-test-id]')
}
function toArray(elementList) {
return Array.from(elementList)
}
function toTagTestIdPair(item) {
return {
ref: item,
id: item.getAttribute('data-test-id')
}
}
function map(array, f) {
return array.map(f)
}
function logElements(items) {
for (const item of items) {
const tagTitle = rightSpacing(item.ref.tagName, 6)
console.groupCollapsed(`${tagTitle}: ${item.id}`)
console.log(item.ref)
console.groupEnd()
}
}
/**
* Adds spaces to the right of a text if it the text
* does not exceed the amount of letters in the text
* than the provided length
* @param text
* @param length
*/
function rightSpacing(text, length) {
const diff = length - text.length;
let result = text;
for (let i = 0; i < diff; i++) {
result = result + ' ';
}
return result
}
logElements(
map(
toArray(getDataTestIdElements()),
toTagTestIdPair
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment