Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to blur input element in puppeteer?

I'm testing form with puppeteer. The form has validation that fires when blur event happens on input element. But there are no API to blur element in puppeteer.

I'm trying focusing/clicking body or div element, but that can't fire my onBlur validations. page.click("body"), page.focus("body")

Now, I'm using fake click image for fire blur event. But it's not good way.

export class LoginPage {
  constructor(private page: Page) {}

  async setup(): Promise<void> {
    await this.page.goto(MY_LOGIN_FORM);
  }

  async typeEmail(email: string): Promise<void> {
    await this.page.type("input[name=email]", email);
    await this.blur();
  }

  async typePassword(password: string): Promise<void> {
    await this.page.type("input[name=password]", password);
    await this.blur();
  }

  async clickLoginButton(): Promise<void> {
    await this.page.click("button");
  }

  private async blur(): Promise<void> {
    // HACK
    await this.page.click("img");
  }
}

There are any way to blur input element without hacks?

like image 556
Ryoji Ishii Avatar asked Apr 04 '18 06:04

Ryoji Ishii


1 Answers

I think using $eval might even be a little bit cleaner:

await page.$eval('input[name=email]', e => e.blur());
like image 185
Johannes Kronmüller Avatar answered Nov 03 '22 16:11

Johannes Kronmüller