Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hookable Accessors #463

Open
Donavan opened this issue May 21, 2018 · 0 comments
Open

Hookable Accessors #463

Donavan opened this issue May 21, 2018 · 0 comments

Comments

@Donavan
Copy link
Contributor

Donavan commented May 21, 2018

I have a gem called Captain Hook that adds before/after hooks to arbitrary objects. I built it for the purpose of DRYing up my automation code. I have a gist up of at PageObject extension that defines a new set of "hookable" accessors. It works be replacing the "_element" method PageObject adds to return a Watir element wrapped by a Captain Hook "Hookable" wrapper (A small SimpleDelegator wrapper).

The gist is way bigger than the changes it makes. It's as big as it is because it needs to override the shortcut methods that bypass the Watir element if we've hooked the method being short circuited.

I've been testing it in a large project for the past few weeks ensuring it works as intended and is actually useful before going public. Originally I had some caching built in to avoid building the Hookable object each time we instantiated an element, but that turned out to be a terrible idea.

With it I can declare a set of hooks like so:

WFA_HOOKS ||= CptHook.define_hooks do
  after(:click).call(:wait_for_ajax)
  after(:set).call(:wait_for_ajax)
  after(:value=).call(:wait_for_ajax)
  after(:check).call(:wait_for_ajax)
  after(:uncheck).call(:wait_for_ajax)
end

Then use them when I declare the elements:

button_hooked(:cancel_dialog, data_bind: /cancelDialog/, hooks: WFO_HOOKS)

Hooks can be combined for composition:

  SEARCH_HOOKS ||= define_hooks do
    before(:value=).call(:ensure_search_element_visible)
  end
  text_field_hooked(:search, id: 'cmboJobReference', hooks: SEARCH_HOOKS.merge(WFA_HOOKS))

The DSL allows for more complicated hooks where parameters are passed as well as using procs/lambdas. It also has the notion of a "context" for the method call, allowing the hook to make calls on other objects.

  DASHBOARD_MENU_HOOKS ||= define_hooks do
    before(:click).call(:ensure_visible).with(:dashboard).using(:page)
  end

I'm opening this ticket to see if there's interest in merging this into PageObject itself, perhaps as the default accessors, or if I should continue with my original plan of making it part of a wider extension Gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants