User Method to get repo item attribute values

Class library usage, coding and language questions.
brentmclauclan
Certified Professional
Certified Professional
Posts: 23
Joined: Wed Jun 25, 2014 11:59 am

User Method to get repo item attribute values

Post by brentmclauclan » Wed Jun 25, 2014 12:15 pm

Hi there.

My first post - I had a look and did not see what I was looking for...please forgive duplicate post if one with answer exists.

I'm looking for a code sample...I'd like to code a user method that I can pass in the name of the repository oitem and the attribute that I'm seeking and store the attribute's value in a variable.

For example I'd pass in the in repository item name (e.g. mysamplesite.login.usernamelable) and the attribute that I need the value of (e.g. innerText).

Thanks

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

Re: User Method to get repo item attribute values

Post by krstcs » Wed Jun 25, 2014 2:02 pm

Hi Brent!

First, could you answer a few questions:
What version and level of Ranorex you are using (5.0.3 Premium, 4.1.6 Professional, etc.)?
What OS you are running on?
What technology is your system under test developed in (HTML, .NET, Java, etc.)? I would guess HTML.

Second, what are you trying to achieve? I ask because we may be able to help you find a better solution. Passing in the text string representation of the repository object is cumbersome and brittle. And, Ranorex methods ONLY accept string objects, so you can't directly pass the repo object.

For example, if you are just wanting to validate that the attribute has a certain value, it would be far easier to use the action table and validate the value of the attribute there.

Say you have a label that has an innertext attribute. You can just drag the repo object representing that object onto the action table and select "Validate". Choose "AttributeEqual" (should be the default) for the validation type, and select InnerText from the attribute field. Then enter the value you expect in the value field. Do that for each item you want to validate.

It would look something like:
Validate -> AttributeEqual -> InnerText -> <Expected Value> -> <Repo Object>
Shortcuts usually aren't...

brentmclauclan
Certified Professional
Certified Professional
Posts: 23
Joined: Wed Jun 25, 2014 11:59 am

Re: User Method to get repo item attribute values

Post by brentmclauclan » Wed Jun 25, 2014 6:16 pm

Hi

My goal is to compare two repository item attributes and check if they match.

Lets say repo.userbalance has an innertext value of 1000 - and another repo item say repo.useraccountbalance should have 1000 as well.

At design time I don't know what value to expect - just that both repo items should have the same value. I'd like it as a user code method that can reuse for other repo items/attirbutes.

Thanks

Brent

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

Re: User Method to get repo item attribute values

Post by krstcs » Wed Jun 25, 2014 6:26 pm

OK, we can do that fairly easily as well.

There is a GetValue() action that can be used to get the value of a repo object's specified attribute and store it in a module variable. You could do this for each of the elements you want to check, and then, in user-code, you could use Validate.AreEqual(object1, object2) (I would suggest using the string variant since that is what Ranorex works with and what should be in the attributes.

So, your action table would be:

Get Value -> InnerText -> var1 -> <Regex for capture, if needed> -> <repo object 1>
Get Value -> InnerText -> var2 -> <Regex for capture, if needed> -> <repo object 2>
User Code -> CompareAttributes -> var1 -> var2

And your user code would have the following method:

Code: Select all

public void CompareAttributes(string v1, string v2) {
    Report.Info("USER", "Comparing attributes.");  //you can do whatever you want with this...
    Validate.AreEqual(v1, v2);
}

If you wanted to actually get the values in user code as well, you can do that, just take a look at the Ranorex generated code for the Get Value actions (right-click the Get Action row and select "View Code"). There are several ways to get the values, but I have found that using Get Value is usually easier and more consistent.
Shortcuts usually aren't...

brentmclauclan
Certified Professional
Certified Professional
Posts: 23
Joined: Wed Jun 25, 2014 11:59 am

Re: User Method to get repo item attribute values

Post by brentmclauclan » Fri Jun 27, 2014 12:05 pm

That seems to be the approach that I need. This is my first attempt at passing in string values into the method, I just need an example to get the string values I pass in converted to proper type.

1. I have one repository so that does not need to be passed in to method (lets call it repo).
2. The repository item will be passed in as a string(lets call it "Mainfom.UsernameEntryField")
3. The attribute required will be passed in as a string as well (lets call it "innertext")
4. Will create a variable to store that result (lets call it var1)

This would be done in a user code module - using the Smart Action GetValue works well (add viewing code just shows the hardcoded path so to speak) but this would be just a user created method.

Can you provide the correct syntax so that Ranoreox gets the innertext attribute value from the above example?



Many Thanks

Brent

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

Re: User Method to get repo item attribute values

Post by krstcs » Fri Jun 27, 2014 1:37 pm

Could I ask why you want to pass the repo item in as a string, and what the goal is for the module?

Without knowing exactly what you are trying to accomplish, it sounds to me like you are trying to re-invent the wheel. :D
Shortcuts usually aren't...

brentmclauclan
Certified Professional
Certified Professional
Posts: 23
Joined: Wed Jun 25, 2014 11:59 am

Re: User Method to get repo item attribute values

Post by brentmclauclan » Fri Jun 27, 2014 3:49 pm

Hi.

I'm trying to create a method within a code module - that will be called and passed 4 arguments.

Arg1 = repo item 1
Arg2 = attribute value needed (say innerText)

Arg3 - repo item 2
Arg4 = attribute value needed (say innerText)

I need to see if the (for example) innerText value of these 2 items are equal or not. I wanted to have a method that can be reused and not just able to verify 2 hardcoded items and attribute.

Within the this method it would use the Validate.AreEqual(v1, v2) , but not sure just how to get V1 and V2 to a state that are set correctly.

So I can use it like this:

myMethod("mainform.username","innerText", "mainform.mainHeader.loggedinName","innerText")

or

myMethod("mainform.usernameLogonButton","Text", "mainform.usernameSomeOtherThing","Text")

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: User Method to get repo item attribute values

Post by Support Team » Tue Jul 01, 2014 3:29 pm

Hello brentmclauclan,

Unfortunately, we cannot provide the necessary user code for resolving this issue since it’s very specific. That aside, you can use following code snippet to access the attributes from your repository item:
repoItem.Element.GetAttributeValue(<NameOfTheAttribute>);
Regards,
Robert
.
Image

axcha15
Certified Professional
Certified Professional
Posts: 4
Joined: Tue Sep 02, 2014 9:56 pm

Re: User Method to get repo item attribute values

Post by axcha15 » Mon Sep 08, 2014 7:15 pm

Hello!,


I have a similar issue here, I have some prices in my web page, they change very often, so that I don't want to have them hardcoded.

I'd like to get the price of the product and them compare if the same price is being displayed once that it is in the cart.

So that I'm trying to get this variable using this method

string price = repo.Store.StrongTagDollar3749Info.GetAttributeValue();

This GetAttributeValue or GetValue doesn't exists, I need to get that variable and then I can compare with the one displayed in the cart, can you help me please? thanks!

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: User Method to get repo item attribute values

Post by Support Team » Tue Sep 09, 2014 10:01 am

Hello axcha15,

It’s pretty difficult to find a proper attribute/property for the „price“ without knowing which kind of element your repository item actually is. That aside, I’m afraid that you have mixed up something within your code snippet.

Please try following line of code in order to access the “price”:
string price = repo.Store.StrongTagDollar3749.Element.GetAttributeValue(<NameOfTheAttribute>);
Regards,
Robert
.
Image