Reading Xpaths from Properties File...

2,246 views
Skip to first unread message

Bharath M

unread,
Jun 13, 2011, 2:28:43 AM6/13/11
to webdriver
hey all,

@FindBy (id="header")
WebElement Header;

@FindBy(xpath="footer")
WebElement Footer;

I have this simple FindBy statements. I have 2 websites. both of them
have header and footer. First one has id name has "header" second one
has just "head" so to have one script to work for both websites am
trying to read this id names from properties file. but am not able to
do that way in webdriver. Can anyone provide me an sample script to
reading id names from properties file..???

QA Automation at nixsolutions.com

unread,
Jun 14, 2011, 8:36:47 AM6/14/11
to webd...@googlegroups.com
Hi Bharath,

in order to identify object which properly could be changed, you can use | sign in Xpath query to set several Xpaths for one particular object.

For example if first website contains button with name btnI and the second website contains the same button with name btnG you Xpath query will be like:

//input[@name='btnI']|//input[@name='btnG']

As for using external file:
My advice to you: using xml file. You can use something like XmlDocument (for .net) which placed at: System.XML namespace. 

 public static string getXpathForObject(string strObjectName)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("objects.xml");
            var ObjNode = xDoc.SelectSingleNode(string.Concat("//", strObjectName));
                if (ObjNode != null)
                    return ObjNode.InnerText;
            else
                    return null;
        }

The sample of objects.xml:
<?xml version="1.0" encoding="utf-8" ?>
<objects>
  <searchButton>//input[@class='SearchB']</searchButton>
</objects>

usage: IWebElement theobj = driver.FindElement(By.XPath(getXpathForObject("searchButton")));

Bharath M

unread,
Jun 15, 2011, 6:57:34 AM6/15/11
to webdriver
HI Nix,

Right now am doing the same way which u have mentioned.. only change
is, am using properties file...

Since Xpaths will be slow i dont want to use xpaths... and i want to
use @FindBy with ID method.....

want something like this...

@FindBy (id=Getproperties.key)
private WebElement element:

Is there any workarounds to do something like this..????

On Jun 14, 5:36 pm, "QA Automation at nixsolutions.com"
<bakaev.dmit...@gmail.com> wrote:
> Hi* Bharath,*
> *
> *

QA Automation at nixsolutions.com

unread,
Jun 15, 2011, 7:19:24 AM6/15/11
to webd...@googlegroups.com
Hi Bharath,

could you please prove me an example of properties file?

Anyway you could use the same logic as I described earlier. 

<?xml version="1.0" encoding="utf-8" ?>
<objects>
  <searchButtonID>SearchBtn</searchButtonID>
</objects>

 public static string getID(string strObjectName)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("objects.xml");
            var ObjNode = xDoc.SelectSingleNode(string.Concat("//", strObjectName));
                if (ObjNode != null)
                    return ObjNode.InnerText;
            else
                    return null;
        }

 IWebElement theobj = driver.FindElement(By.ID(getID("searchButtonID")));

Daniel Wagner-Hall

unread,
Jun 15, 2011, 10:19:07 AM6/15/11
to webd...@googlegroups.com
On 15 June 2011 19:57, Bharath M <wordl...@gmail.com> wrote:
> Right now am doing the same way which u have mentioned.. only change
> is, am using properties file...
>
> Since Xpaths will be slow i dont want to use xpaths... and i want to
> use @FindBy with ID method.....
>
> want something like this...
>
> @FindBy (id=Getproperties.key)
> private WebElement element:
>
> Is there any workarounds to do something like this..????

FindBy is quite hard-coded at the moment to only support static
values. If you want to use dynamic locators with the FindBy
annotation, you will need to implement your own @DynamicFindBy
annotation which does the looking up. You may need to extend or
re-implement the PageFactory class - I don't know how extensible it
is, but it's not a huge amount of code or too complex. You may just
need to implement a new FieldDecorator and/or ElementLocator. Have a
look at $TRUNK/java/client/src/org/openqa/selenium/support/{PageFactory.java,
pagefactory/*.java} to see how it currently works. Unfortunately, I'm
not familiar enough with Java annotations to be more help without
reading up a bit more.

If you do do this, we'd be very happy to accept patches which make our
code more general and extensible :)

Mark Collin

unread,
Oct 15, 2012, 4:21:58 PM10/15/12
to webd...@googlegroups.com

Don’t use a property file, but set the locators in the annotation. 

 

The @FindBy annotation doesn’t support the use of variables.

 

Somebody was trying to write something to get around this a while back, but I don’t think he ever completed it:

 

https://groups.google.com/d/topic/webdriver/awxOw0FoiYU/discussion

 

 

From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Julie
Sent: 15 October 2012 12:29
To: webd...@googlegroups.com
Subject: [webdriver] Re: Reading Xpaths from Properties File...

 

Hi Bharath,

 

 

Have you solved this problem? 

I am having the same problem, I want the ids, xpaths and so on to be loaded from some properties file but Java gives me 

"the value for annotation must be a constant expression"

           

           

            @FindBy(how = How.XPATH, using = propertiesFile.getValueForKey(A))

            public WebElement termsAndConditionsLink;

 

Could you please help ?

 

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/e_hTgztpUh0J.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Reply all
Reply to author
Forward
0 new messages