playwright-ruby-client icon indicating copy to clipboard operation
playwright-ruby-client copied to clipboard

How do I blur an element nicely?

Open mhenrixon opened this issue 2 years ago • 1 comments

Usecase / What to achieve

I have some calculations that run on blur (lost focus). I want a concise way to blur that element by filling it in.

Question

I found this old code on the internet:

def fill_in_then_blur(locator, with:)
  fill_in(locator, with:)
  find_field(locator).native.send_keys :tab # Tab away from the element to trigger a blur event.
end

I also tried page.keyboad.press("Tab") but it seems that didn't work either. Love your work, appreciate your assistance. Cheers.

mhenrixon avatar Mar 28 '24 09:03 mhenrixon

@mhenrixon Hi, Locator#blur would be the best way for it.

YusukeIwaki avatar Mar 28 '24 17:03 YusukeIwaki

Feel free to reopen the issue if needed.

YusukeIwaki avatar Apr 05 '24 15:04 YusukeIwaki

@YusukeIwaki not sure what you are refering to but:

The following works:

def fill_in_then_blur(locator, with:)
  find_by_id(locator).set(with).send_keys :tab
end

The following does not:

def fill_in_then_blur(locator, with:)
  find_by_id(locator).set(with).blur
end

I thought the locator would be the result of the find. What am I missing?

  1) Cars can add and sell cars
     Failure/Error: find_by_id(locator).blur

     NoMethodError:
       undefined method `blur' for an instance of Capybara::Node::Element

     [Screenshot Image]: tmp/capybara/screenshots/failures_r_spec_example_groups_cars_can_add_and_sell_cars_553.png

mhenrixon avatar Apr 05 '24 16:04 mhenrixon