async function getRandomID() {
const inputElement = await page.$("//input[@placeholder='ex: John']");
const id = await inputElement.getAttribute('id');
const idFinal: string = id.replace("FirstName", "");
console.log("idFinal: ", idFinal);
return idFinal;
}
async function remplirChamp(page: Page, HTMLid: string, selecteurName: string, valeur: string) {
const fieldName: string = HTMLid+selecteurName;
const inputField = await page.$(`#${fieldName}`);
if (inputField) {
await inputField.fill(valeur);
}
}
await remplirChamp(page, HTMLid, "FirstName", rdmPrenom);
await remplirChamp(page, HTMLid, "Name", rdmNom);
await remplirChamp(page, HTMLid, "Nationality", rdmNationalite);
await remplirChamp(page, HTMLid, "Country", rdmCountry);
await remplirChamp(page, HTMLid, "Birthdate", rdmBirthdate);
await remplirChamp(page, HTMLid, "Passport", rdmPasseport);
await remplirChamp(page, HTMLid, "PassportExpiracy", rdmPasseportExpDate);
await remplirChamp(page, HTMLid, "Email", rdmEmail);
await page.locator("#Email").fill(rdmEmail);
await remplirChamp(page, HTMLid, "CCRefundNumber", rdmCard);
I'm working on some tests for a website in my firm, basically, I scrap the id (that is generated randomly), and I fill the inputs, but here is the problem : the first name is not filled and it happens randomly with any input so my test is flaky
This thread is trying to answer question "Why are input fields not being consistently filled in the Playwright test?"
Yeah I guess im gonna ask to add a data-test-id, It'll be less headache, but I must admit I'd have liked to complete this test without an internal helper
Use locators instead of evaluating on the page: https://playwright.dev/docs/locators
You can try using pressSequentially method to fill the value. And use locator or GetBy methods to find element.
For more info you can go through My this video. https://youtu.be/FJuMMWq_Vug
Please let me know if it does not work.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].