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

Issue 9549 #9557

Merged
merged 3 commits into from Jun 10, 2021
Merged

Issue 9549 #9557

merged 3 commits into from Jun 10, 2021

Conversation

Jiwari
Copy link
Contributor

@Jiwari Jiwari commented Jun 9, 2021

Feature implementation of Issue#9549

Description

Update PageFactory methods signature from WebDriver to SearchContext.

Currently:

  public static void initElements(WebDriver driver, Object page) {
    initElements(new DefaultElementLocatorFactory(driver), page);
  }

  public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy) {
    T page = instantiatePage(driver, pageClassToProxy);
    initElements(driver, page);
    return page;
  }

  private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy) {
    try {
      try {
        Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);
        return constructor.newInstance(driver);
      } catch (NoSuchMethodException e) {
        return pageClassToProxy.getDeclaredConstructor().newInstance();
      }
    } catch (ReflectiveOperationException e) {
      throw new RuntimeException(e);
    }
  }

With changes:

  public static void initElements(SearchContext searchContext, Object page) {
    initElements(new DefaultElementLocatorFactory(searchContext), page);
  }

  public static <T> T initElements(SearchContextsearchContext, Class<T> pageClassToProxy) {
    T page = instantiatePage(searchContext, pageClassToProxy);
    initElements(searchContext, page);
    return page;
  }

  private static <T> T instantiatePage(SearchContext searchContext, Class<T> pageClassToProxy) {
    try {
      try {
        Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);
        return constructor.newInstance(searchContext);
      } catch (NoSuchMethodException e) {
        return pageClassToProxy.getDeclaredConstructor().newInstance();
      }
    } catch (ReflectiveOperationException e) {
      throw new RuntimeException(e);
    }
  }

No further changes would be needed as upstream classes already use SearchContext as type instead of WebDriver

My current concern is about instantiatePage change. Currently Selenium tries to instantiate the page with 2 different constructors (WebDriver arg and parameterless), I am wondering if there should be a third for SearchContext between the two existing ones, and if it would be useful.

Motivation and Context

Allow elements to be passed as an argument to PageFactory#initElements methods and be used as the SearchContext.

My objective here is to solve issues related to shadow doms and elements lookup.

I currently have this changed implemented on my project which has a lot of shadow DOMs on the page, and being able to provide the shadow element to initElements allows me to use PageFactory with the shadow element as the search context, providing WebDriver would never work as the elements are "hidden" inside the shadow DOM.

Types of changes

  • Bug fix (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 change)

Checklist

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

@Jiwari
Copy link
Contributor Author

Jiwari commented Jun 9, 2021

Regarding the documentation related items of checklist, I updated the javadocs accordingly, is there any additional documentation changes that need to be done?

@sonarcloud
Copy link

sonarcloud bot commented Jun 10, 2021

Copy link
Member

@diemol diemol left a comment

Choose a reason for hiding this comment

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

Thank you, @Jiwari!
Congrats on your first contribution! 🎉

@anushareddyqa
Copy link

When we call initelements function, passsing driver object is not throwing any error.
How to pass searchcontext as an argument-can you give sample example for POM

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.

None yet

3 participants