Last active
February 2, 2024 11:03
-
-
Save kaineer/2396676b0743f2bf8c61796669c2f0d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = async (page, scenario, viewport) => { | |
// Элементы настройки | |
// | |
const containerSelector = "header"; | |
const childCount = 3; | |
const additionalCount = 2; | |
const contents = ["Первый", "Второй", "Третий"]; | |
console.log('SCENARIO > ' + scenario.label); | |
// await require('./clickAndHoverHelper')(page, scenario); | |
// add more ready handlers here... | |
await page.waitForFunction(() => { | |
return document.fonts.ready.then(() => { | |
console.log('Fonts loaded'); | |
return true; | |
}); | |
}); | |
// Поехали | |
// 1. Получаем контейнер и все дочерние элементы | |
// | |
const containerEl = await page.$(containerSelector); | |
const childElements = await containerEl.$$("*"); | |
const elNames = {}; | |
// Разделяем по именам тегов | |
// | |
for (const el of childElements) { | |
const [tagName, text] = await page.evaluateHandle( | |
e => [e.tagName.toLowerCase(), e.textContent], el | |
); | |
if (typeof elNames[tagName] === "undefined") { | |
elNames[tagName] = []; | |
} | |
elNames[tagName].push({ el, text }); | |
} | |
// Выбираем имена тегов, для | |
// которых элементов нужное количество | |
// | |
const keysToCheck = Object.keys(elNames) | |
.filter((name) => { | |
if (elNames[name].length === childCount) { | |
const et = elNames[name].map(({ text }) => text); | |
// Проверяем содержимое | |
// | |
for (let i = 0; i < childCount; i++) { | |
if (et[i] !== contents[i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
return false; | |
}); | |
// Оставляем только самый первый | |
const key = keysToCheck[0]; | |
// Выбираем элементы | |
const ehs = elNames[key].map(({ el }) => el); | |
await page.evaluateHandle(e => { | |
const parent = e.parentElement; | |
// Добавляем клоны первого элемента | |
// в его родительский элемент | |
for (let i = 0; i < 0; i < additionalCount) { | |
parent.addChild(e.cloneNode(true)); | |
} | |
}, ehs[0]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment