Working with XPath in code

Class library usage, coding and language questions.
peter92
Posts: 3
Joined: Mon Apr 14, 2014 2:43 pm

Working with XPath in code

Post by peter92 » Fri May 09, 2014 5:28 pm

Hi,
As an example, say I have the following xpath which is looking at a cell in a table:

/dom.../text[@caption='JohnDoe']

Is it possible in the code (we are using c#) to search for the appropriate text value?
EG., could I do something to the effect of:

var aCell = @"/dom.../text[@caption='+varName+']

where "varName" is a value we are reading from an excel doc.
The intention is to be able to create a way of finding an item in the table whose name may be changed (which would be reflected in the excel doc) and then perform, say, a Click action, or verify it exists, etc. So we would have the repo item and change the part which is not going to remain constant (in this case, the name).
Can this be done?

Thanks!

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Working with XPath in code

Post by krstcs » Fri May 09, 2014 5:42 pm

You can absolutely do that, but you need to structure the string correctly. You can do it one of two ways:

Code: Select all

var aCell = "/dom.../text[@caption='"+varName+"']";
// Notice the addition of "..." for a proper .net string, so you would have ...<single-quote><double-quote> + 
// varName + <double-quote><single-quote>]<double-quote>.

// or, my preference for readability...

var aCell = string.Format("/dom.../text[@caption='{0}' and @id='{1}']", varName, varID);
// This is the .net in-line formatter.  You use "{<index>}" in the spot in the string where you want the variable 
// in that spot in the following list placed in the string.  
// Format(<string to be formatted>, <0-index argument>, <1-index argument>, ... <n-index argument>).
Shortcuts usually aren't...

Jit Saini
Posts: 2
Joined: Mon Oct 20, 2014 9:24 am

Re: Working with XPath in code

Post by Jit Saini » Sun Nov 09, 2014 8:41 pm

I have also a Question about this. I created a variable which conatins an Xpath to a Table Cell with described Method. How can i now get the value of this cell into another Variable?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Working with XPath in code

Post by krstcs » Mon Nov 10, 2014 3:28 pm

Could you post your XPath and the code you are using?

Also, please include the following information when you have questions as it will help us understand what you are using:

1. What version of Ranorex are you using?
2. What version of Windows are you using?
3. What technology is your System Under Test (SUT) developed in?
Shortcuts usually aren't...