Skip to content

custom locator strategy#4603

Merged
christian-bromann merged 18 commits intowebdriverio:masterfrom
baruchvlz:feat/locator-strategy
Nov 1, 2019
Merged

custom locator strategy#4603
christian-bromann merged 18 commits intowebdriverio:masterfrom
baruchvlz:feat/locator-strategy

Conversation

@baruchvlz
Copy link
Member

Proposed changes

Related issue: #4578

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@mgrybyk
Copy link
Member

mgrybyk commented Oct 10, 2019

How should it work in the end, can you please provide with examples?

@baruchvlz
Copy link
Member Author

baruchvlz commented Oct 10, 2019

@mgrybyk

@christian-bromann and I had a conversation about how to approach this. Essentially this is what we came up with.

The user will have to register a locator somewhere in their tests like so

browser.addLocatorStrategy(name: string, callback: (args: any[]) => HTMLElement))

When they want to use this strategy, they can call it as such:

browser.custom$(name: string, strategyArgs: any[]) => WDIOElement | WDIOElement[]

Any suggestions / concerns let me know.

@codecov
Copy link

codecov bot commented Oct 11, 2019

Codecov Report

Merging #4603 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4603      +/-   ##
==========================================
+ Coverage   99.39%   99.39%   +<.01%     
==========================================
  Files         203      207       +4     
  Lines        5251     5301      +50     
  Branches     1128     1145      +17     
==========================================
+ Hits         5219     5269      +50     
  Misses         29       29              
  Partials        3        3
Impacted Files Coverage Δ
packages/webdriverio/src/utils/getElementObject.js 97.82% <ø> (ø) ⬆️
packages/webdriverio/src/commands/element.js 100% <100%> (ø) ⬆️
...ckages/webdriverio/src/commands/element/custom$.js 100% <100%> (ø)
packages/webdriverio/src/commands/browser.js 100% <100%> (ø) ⬆️
...ckages/webdriverio/src/commands/browser/custom$.js 100% <100%> (ø)
...kages/webdriverio/src/commands/element/custom$$.js 100% <100%> (ø)
packages/wdio-cli/src/interface.js 100% <100%> (ø) ⬆️
packages/webdriverio/src/index.js 100% <100%> (ø) ⬆️
packages/webdriverio/src/utils.js 100% <100%> (ø) ⬆️
...kages/webdriverio/src/commands/browser/custom$$.js 100% <100%> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a7e6a99...f56c46c. Read the comment docs.

@CrispusDH
Copy link
Contributor

@baruchvlz , I think might be better to create two methods:

browser.custom$(name: string, strategyArgs: any[]) => WDIOElement
browser.custom$$(name: string, strategyArgs: any[]) => WDIOElement[]

in such case it would be convenient to work with types and be predictable what you expect to get.
What do you think?

@baruchvlz
Copy link
Member Author

baruchvlz commented Oct 11, 2019

@CrispusDH Yea that makes sense, I'll add the commands.

@baruchvlz
Copy link
Member Author

baruchvlz commented Oct 11, 2019

@CrispusDH
Sorry, on second thought, having both custom$ and custom$$ might be confusing since you have to add the strategies before you use them. It would be better to consider renaming the command to something more explicit likeuseLocatorStrategy, this way its easier to understand while writing tests, and we always return one element since it depends on the script passed by the user.

Alternatively, we could add an extra flag to the command for selecting multiple elements.

useLocatorStrategy<T = any>(name: string, strategyArgument: T, multiple: boolean) => WDIOElement | WDIOElement[]

Adding a dynamic type will allow the user to pass multiple arguments to their script if they require extra logic.

I think useLocatorStrategy is a good name and its intuitive since the user will attach strategies via browser.addLocatorStrategy.

@christian-bromann
Copy link
Member

on second thought, having both custom$ and custom$$ might be confusing since you have to add the strategies before you use them.

In what way is this confusing?

It would be better to consider renaming the command to something more explicit like useLocatorStrategy

My concern is that it is not aligned with other command names such as react$/$$ or shadow$/$$

and we always return one element since it depends on the script passed by the user.

This is not ensured by the user script. If for whatever reason the script fetches multiple elements, multiple elements will be returned.

@baruchvlz
Copy link
Member Author

There is no real difference between custom$/$$ and useLocatorStrategy and you have a good point on keeping the methods consistent. I'll add the methods to both browser and element objects.

@baruchvlz baruchvlz force-pushed the feat/locator-strategy branch 2 times, most recently from 52aafb7 to b21d235 Compare October 21, 2019 10:09
Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of comments

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor code formatting comments, after that we should be good to go.

@baruchvlz baruchvlz closed this Oct 31, 2019
@baruchvlz baruchvlz reopened this Oct 31, 2019
@baruchvlz baruchvlz marked this pull request as ready for review October 31, 2019 14:25
@christian-bromann
Copy link
Member

@baruchvlz can you rebase?

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@baruchvlz baruchvlz force-pushed the feat/locator-strategy branch from 7daf503 to f56c46c Compare November 1, 2019 13:23
Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing, even though pinning scripts in not yet part of WebDriver but with this we are far ahead of already allowing it now to some extend.

@christian-bromann christian-bromann added the PR: New Feature 🚀 PRs that contain new features label Nov 1, 2019
@christian-bromann christian-bromann merged commit 8f4b513 into webdriverio:master Nov 1, 2019
@christian-bromann christian-bromann deleted the feat/locator-strategy branch November 1, 2019 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: New Feature 🚀 PRs that contain new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants