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

Allow RelativeBy to start with any locator, not just tag name #9273

Merged

Conversation

JustasMonkev
Copy link
Contributor

… tests for these methods

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

added new methods for RelativeBy(withXpath,withCssSelector)

Description

Added new Methods for RelativeLocator to improve UX.

Motivation and Context

I believe it improves UX. now it's easier to use RelativeLocator.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [ x] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • [x ] I have read the contributing document.
  • [ x] My change requires a change to the documentation.
  • [ -] I have updated the documentation accordingly.
  • [ x] I have added tests to cover my changes.
  • [ x] All new and existing tests passed.

@CLAassistant
Copy link

CLAassistant commented Mar 12, 2021

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@shs96c shs96c left a comment

Choose a reason for hiding this comment

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

Hi! Thank you for the PR! I love the idea behind it, and it's going to be really helpful, but I think we should consider just passing in a general locator (a By subclass) to be even more useful. What do you think? That'd mean replacing withTagName(String) with with(By)


WebElement lowest = driver.findElement(By.id("below"));

List<WebElement> elements = driver.findElements(withXpath("//p").above(lowest));
Copy link
Member

Choose a reason for hiding this comment

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

The xpath locators rooted with // will (because of how xpath works) scan the entire document. My expectation with this is that we'll end up returning the first p tag in the entire doc. I'm surprised this works as expected....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should I use this xpath //p[position()=1]?

public void shouldBeAbleToCombineFiltersWithXpath() {
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(withXpath("//td").above(By.id("center")).toRightOf(By.id("second")));
Copy link
Member

Choose a reason for hiding this comment

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

Again, this should be returning the very first td in the document.

@@ -97,6 +99,16 @@ public static RelativeBy withTagName(String tagName) {
return new RelativeBy(By.tagName(tagName));
}

public static RelativeBy withXpath(String xpathExpression) {
Copy link
Member

Choose a reason for hiding this comment

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

It may be better to just allow someone to pass in an arbitrary locator at this point. That is, replace withTagName(String) with with(By) so folks can write with(tagName("td")) or whatever takes their fancy.

@JustasMonkev
Copy link
Contributor Author

please take a look @shs96c

@sonarqubecloud
Copy link

Copy link
Member

@shs96c shs96c left a comment

Choose a reason for hiding this comment

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

Hi! This looks great. Thank you for working on it :) There's a couple of things that we can clean up, but it won't take long to do that, and I'll make the changes once this is landed.

@shs96c
Copy link
Member

shs96c commented Mar 23, 2021

LGTM, and I think this will make using the relative locators a lot nicer. Thank you!

@shs96c shs96c merged commit 6a3f867 into SeleniumHQ:trunk Mar 23, 2021
@JustasMonkev JustasMonkev changed the title added new methods for RelativeBy(withXpath,withCssSelector) and added… Allow RelativeBy to start with any locator, not just tag name May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants