Extending @FindBy

842 views
Skip to first unread message

Paul Hammant

unread,
Mar 4, 2017, 5:25:57 AM3/4/17
to Selenium Developers
I'm maintaining locators in Java for Angular in the ngWebDriver project (yes yes, it's just a wrapper for Protractor from the Angular team).

I'm wanting to make new page-object style annotations for it.  NgWebDriver like Protractor is going to the client via the JavascriptExecutor and interoperating with the inner workings of Angular in order to do things. That manifests itself as another series of things like By.id(..). Those would be ByAngular.model(..), ByAngular.repeater(..) and more.

The annotation @FindBy is fixed. Sure, I'd want a new @AngularFindBy. So, looking at Annotations.buildBy(..) I don't think it is open for alternates.

I'm pretty sure there is a backwards-compatible way of changing WebDriver to allow for new @FindBy inspired annotations via parent annotations:

    public @interface FindBy extends PageObjectAnnotation {...}

The logic in Annotations.buildBy(..) would change to looking PageObjectAnnotation and calling methods within that.

Thoughts?

- Paul

⇜Krishnan Mahadevan⇝

unread,
Mar 4, 2017, 9:59:36 AM3/4/17
to selenium-...@googlegroups.com
Paul,

Sometime back I created a blog post that talks about how to make use of the PageFactory and the PageObjects concept and work with external locators while still leveraging what the PageFactory provides as an infrastructure/base.


Please see if this is something that can be leveraged by you as a concept and get this done on your own in your project.

In short you would need a customized version of 
  • Sub-class of org.openqa.selenium.support.pagefactory.Annotations (For providing support to your new annotations viz., @AngularFindBy, wherein it would first invoke Annotations.buildBy() within a try..catch() block, gobbling all exceptions and if buildBy() returned null, then it would basically start parsing the annotation assuming it to be your @AngularFindBy variant.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-developers/01b88876-b5ec-4d1a-bb46-b4973000b143%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Hammant

unread,
Mar 4, 2017, 10:06:06 AM3/4/17
to selenium-developers
Good to know, Krishnan, thanks.

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsubscribe...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsub...@googlegroups.com.

Simon Stewart

unread,
Mar 6, 2017, 3:50:56 AM3/6/17
to selenium-developers
Hi Paul,

What are you trying to do? Get the same behaviour as the Annotations class? That code is trivial to write, especially for someone of your experience. Don't try and extend it --- ignore it and do something appropriate for your particular testing domain.

For a bit of history, the support package started off as a set of examples of how you might want to use WebDriver as a building block for more sophisticated tooling. Naively, I never thought people would actually use the APIs as anything other than inspiration. It's my own fault for not really considering the audience of the code. I think that attitude started to change with the Wait class in 2009 once I understood what was happening, but PageFactory dates to 2007. If you'd like to refactor the heck out of and keep the public-facing APIs pin-compatible, go for it.

Simon 


--

Paul Hammant

unread,
Mar 6, 2017, 5:13:47 AM3/6/17
to selenium-developers
Hi Simon,

You're saying that rather than rework things to get PageFactory.initElements(driver, objectThatCouldHaveMoreGeneralFindBys), I should consider AngularPageFactory.initElements(driver, objectThatHasAngularFindBys), and in some cases, end users would call both PageFactory.initElements(..) and AngularPageFactory.initElements(..)

And you're right, I don't really use the PageFactory stuff myself. I try to facilitate other peoples use cases, though.

- Paul

On Mon, Mar 6, 2017 at 3:50 AM, Simon Stewart <simon.m...@gmail.com> wrote:
Hi Paul,

What are you trying to do? Get the same behaviour as the Annotations class? That code is trivial to write, especially for someone of your experience. Don't try and extend it --- ignore it and do something appropriate for your particular testing domain.

For a bit of history, the support package started off as a set of examples of how you might want to use WebDriver as a building block for more sophisticated tooling. Naively, I never thought people would actually use the APIs as anything other than inspiration. It's my own fault for not really considering the audience of the code. I think that attitude started to change with the Wait class in 2009 once I understood what was happening, but PageFactory dates to 2007. If you'd like to refactor the heck out of and keep the public-facing APIs pin-compatible, go for it.

Simon 

On Sat, Mar 4, 2017 at 10:25 AM, Paul Hammant <pa...@hammant.org> wrote:
I'm maintaining locators in Java for Angular in the ngWebDriver project (yes yes, it's just a wrapper for Protractor from the Angular team).

I'm wanting to make new page-object style annotations for it.  NgWebDriver like Protractor is going to the client via the JavascriptExecutor and interoperating with the inner workings of Angular in order to do things. That manifests itself as another series of things like By.id(..). Those would be ByAngular.model(..), ByAngular.repeater(..) and more.

The annotation @FindBy is fixed. Sure, I'd want a new @AngularFindBy. So, looking at Annotations.buildBy(..) I don't think it is open for alternates.

I'm pretty sure there is a backwards-compatible way of changing WebDriver to allow for new @FindBy inspired annotations via parent annotations:

    public @interface FindBy extends PageObjectAnnotation {...}

The logic in Annotations.buildBy(..) would change to looking PageObjectAnnotation and calling methods within that.

Thoughts?

- Paul

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsubscribe...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsub...@googlegroups.com.

Simon Stewart

unread,
Mar 6, 2017, 12:08:02 PM3/6/17
to selenium-developers
I was suggesting writing your own class that folks should use all the time instead of PageFactory (you can delegate to pieces of it if you felt like it, just as Krishnan suggested)

If we were smart, you'd be able to register locator strategies that look for particular annotations/field types/whatever and apply the first matching one to that. Obviously, the old code shows that I wasn't thinking like that at the time :)

Simon

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsub...@googlegroups.com.

Paul Hammant

unread,
Mar 19, 2017, 8:49:43 PM3/19/17
to selenium-developers

- Paul

Serguei Kouzmine

unread,
Apr 21, 2017, 12:21:18 AM4/21/17
to Selenium Developers
Hello Paul

Does https://github.com/henrrich/jpagefactory accomlish what you intend ?
 

Thanks,

Serguei Kouzmine

Paul Hammant

unread,
Apr 21, 2017, 6:37:04 AM4/21/17
to selenium-developers
It could do, and I was unaware of that, but I've coded a solution within Selenium - https://github.com/SeleniumHQ/selenium/pull/3680

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-developers+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages