I have this spec
Scenario Outline: Display widget
Given I have a valid connection
When I navigate to home using <browser>
Then The element in css selector #<id> > svg > g.x.axis.percent > text:nth-child(1) should be <value>
Examples:
| browser | id | valye |
| Chrome | Widget1 | 213.00 |
With this page definition
class BarSummaryPage
{
[FindsBy(How = How.CssSelector, Using="#{DYNAMIC-ID} > svg > g.x.axis.percent > text:nth-child(1)")]
private IWebElement Mes;
}
I need to configure the Using
property in FindsBy
dynamic, like above: SEE #{DYNAMIC-ID}
As far as I know, this doesn't exist out of the box. The
FindBy
annotation takes staticStrings
only. You probably need to custom modify theFindBy
annotation processor similarly to what this blogger did: https://web.archive.org/web/20180612042724/http://brimllc.com/2011/01/selenium-2-0-webdriver-extending-findby-annotation-to-support-dynamic-idxpath/Another discussion thread here: https://groups.google.com/forum/#!topic/webdriver/awxOw0FoiYU where Simon Stewart shows an example of how this could be accomplished.
UPDATE:
I have actually implemented this because I needed it enough to try. I didn't create a custom finder annotation (which I may have to do in the future).
I wrote implementations for
ElementLocator
andElementLocatorFactory
that allow for string substitutions for locators specified using the existing annotations. If you know, or can determine, at runtime the values to substitute, this will work for you.By default,
PageFactory
uses theclasses
DefaultElementLocator
andDefaultElementLocatorFactory
implementations of theElementLocator
andElementLocatorFactory
interfaces
for setting up the processing of annotations, but the real logic is in theAnnotations class
. I wrote my own implementations ofElementLocator
, andElementLocatorFactory
and wrote my own version ofAnnotations
to do the processing. There are just a few differences between the source of my customizedclasses
and the ones that are in the Selenium source code.And here is the
DynamicElementLocatorFactory
:And here is my custom annotation processor. This is where most of the work was:
Example usage:
UPDATED 5/1/2019: I had to use a Web Archive link for the blog post I referenced at the beginning of my answer because that blog post is not accessible at its original link.