Access to repository from user code collection
Access to repository from user code collection
Hi!
Is there a way to access a repo in user code collection (namespace Ranorex_Automation_Helpers)?
I tried a lot of things, but unfortunately had no luck.
I can easily use Report or Validate classes, but can't use repository.
Is there a way to access a repo in user code collection (namespace Ranorex_Automation_Helpers)?
I tried a lot of things, but unfortunately had no luck.
I can easily use Report or Validate classes, but can't use repository.
Re: Access to repository from user code collection
Hi,
You have to instantiate the repository in given code collection, e.g. like this:
This line of code should be added at start of your collection. Then you should be able to access the repository via "repo" keyword. Hope this helps?
You have to instantiate the repository in given code collection, e.g. like this:
Code: Select all
private static NameOfSolution.NameOfSolutionRepository repo = NameOfSolution.NameOfSolutionRepository.Instance;
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
Ranorex explorer at Descartes Systems
Please add these details to your questions:
- Ranorex Snapshot. Learn how to create one >here<
- Ranorex xPath of problematic element(s)
- Ranorex version
- OS version
- HW configuration
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Access to repository from user code collection
First of all, I wouldn't do this. You really want the code in a usercode collection to be generic, and you can edit the signature of the method to only accept specific object types if you really want to get down to it. This community would be more than willing to help you generalize a code sample to fit this ideal.
To answer your question, this is all just .Net so yes there is a way. You just need to instantiate a static/shared instance of the repository so it's accessible just like you have to with each usercode method on the collection. It's different for VB.Net and C# (shared versus static), but I'm fairly certain that's where your solution lies.
I skipped over Lesson 7 in the user guide because I'm assuming you're familiar with accessing the repository in user code that is NOT in a code collection.
To answer your question, this is all just .Net so yes there is a way. You just need to instantiate a static/shared instance of the repository so it's accessible just like you have to with each usercode method on the collection. It's different for VB.Net and C# (shared versus static), but I'm fairly certain that's where your solution lies.
I skipped over Lesson 7 in the user guide because I'm assuming you're familiar with accessing the repository in user code that is NOT in a code collection.
Doug Vaughan
Re: Access to repository from user code collection
I'm not a newbee in .NET world. Sure it was a first idea to instantiate a static repo field.
But the studio returns error: The type or namespace name 'my_solution' could not be found (are you missing a using directive or an assembly reference?)
the line - private static my_solution.my_solution.repository repo = my_solution.my_solution.repository.Instance;
goes right after the:
public class UserCodeCollection1
{
But the studio returns error: The type or namespace name 'my_solution' could not be found (are you missing a using directive or an assembly reference?)
the line - private static my_solution.my_solution.repository repo = my_solution.my_solution.repository.Instance;
goes right after the:
public class UserCodeCollection1
{
Re: Access to repository from user code collection
Suppose I need a method for getting some text value from wpf-based GUI. Then the text is parsed to DateTime for some comparisons. And this method could be used in different test-cases and recordings. Now the only way to do this is via inheritance of common class for every recording. And this is inconvenient way.Vaughan.Douglas wrote:First of all, I wouldn't do this. You really want the code in a usercode collection to be generic, and you can edit the signature of the method to only accept specific object types if you really want to get down to it. This community would be more than willing to help you generalize a code sample to fit this ideal.
...
That's why I need an access to my repo from User Code Library. Would be nice to have such feature.
Last edited by EugeneMkv on Fri Dec 01, 2017 2:34 pm, edited 1 time in total.
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Access to repository from user code collection
Your approach should work, so maybe there is an issue with your namespaces? How many projects do you have in your solution?
As for a more generalized means of accomplishing the same thing, you could split it up between multiple actions in the recorded module.
which also calls
As for a more generalized means of accomplishing the same thing, you could split it up between multiple actions in the recorded module.
- Perform a get on your WPF object and store it in a module variable/parameter
- Create a usercode method that does everything else ie convert from string to date and compare the value
Code: Select all
<UserCodeMethod> _
Public Shared Sub ValidateDate(myAttribute As String, myExpected As String, myObjectInfo As Ranorex.Core.Repository.RepoItemInfo)
Report.Log(ReportLevel.Info, String.Format("Attempting to convert the '{2}' attribute of '{3}'; '{0}' and the expected value '{1}' to valid dates for comparison", Host.Current.FindSingle(myObjectInfo.AbsolutePath).Item(myAttribute).ToString(), myExpected, myAttribute, myObjectInfo.Name))
Validate.AreEqual(StringToDate(Host.Current.FindSingle(myObjectInfo.AbsolutePath).Item(myAttribute).ToString()), StringToDate(myExpected), "Expect '{0}' to match '{1}'", False)
End Sub
Code: Select all
<UserCodeMethod> _
Public Shared Function StringToDate(ByVal dateAsString As String) As Date
Dim myDate As Date
Date.TryParse(dateAsString, myDate)
Return myDate
End Function
Last edited by Vaughan.Douglas on Fri Dec 01, 2017 2:44 pm, edited 1 time in total.
Doug Vaughan
Re: Access to repository from user code collection
Are you sure about the bold part?...
private static my_solution.my_solution.repository repo = my_solution.my_solution.repository.Instance;
Typically, the repository autogenerated repository name is my_solutionrepository so unless you changed repo name, the line should look more like this:
private static my_solution.my_solutionrepository repo = my_solution.my_solutionrepository.Instance;
private static my_solution.my_solution.repository repo = my_solution.my_solution.repository.Instance;
Typically, the repository autogenerated repository name is my_solutionrepository so unless you changed repo name, the line should look more like this:
private static my_solution.my_solutionrepository repo = my_solution.my_solutionrepository.Instance;
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
Ranorex explorer at Descartes Systems
Please add these details to your questions:
- Ranorex Snapshot. Learn how to create one >here<
- Ranorex xPath of problematic element(s)
- Ranorex version
- OS version
- HW configuration
Re: Access to repository from user code collection
There are no issues with my namespaces. There are only two of them: one for main solution and another - Ranorex_Automation_Helpers.Vaughan.Douglas wrote:You're approach should work, so maybe there is an issue with your namespaces? How many projects do you have in your solution?
I've just started my new project.
Thanks. But I prefer to do it in one place.As for a more generalized means of accomplishing the same thing, you could split it up between multiple actions in the recorded module.
I will not copy-paste the same a code for getting the text-values from GUI for every recording. This is ridiculous and goes against the OOP paradigm.
Re: Access to repository from user code collection
oh sorry, I've made a mistake copypasting these parts.odklizec wrote:Are you sure about the bold part?...
it looks like this (double checked):
private static my_solution.my_solutionRepository repo = my_solution.my_solutionRepository.Instance;
Last edited by EugeneMkv on Fri Dec 01, 2017 2:52 pm, edited 1 time in total.
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Access to repository from user code collection
You'll notice the <UserCodeMethod> tag in the provided code samples. They are part of a UserCode repository that resides in an entirely different project within the same solution. There is no copy/paste involved.EugeneMkv wrote: Thanks. But I prefer to do it in one place.
I will not copy-paste the same a code for getting the text-values from GUI for every recording. This is ridiculous and goes against the OOP paradigm.
Doug Vaughan
Re: Access to repository from user code collection
I'm not into VB.NET stuff but into C# instead.Vaughan.Douglas wrote: You'll notice the <UserCodeMethod> tag in the provided code samples. They are part of a UserCode repository that resides in an entirely different project within the same solution. There is no copy/paste involved.
Ok, then, the only one problem for me is to access my repo...
Re: Access to repository from user code collection
Hi,
I just tried it in my relative new project with user code collection and it works as expected? But the truth is that I did not tried it within the test automation helpers project.
I just tried it in my relative new project with user code collection and it works as expected? But the truth is that I did not tried it within the test automation helpers project.
You do not have the required permissions to view the files attached to this post.
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
Ranorex explorer at Descartes Systems
Please add these details to your questions:
- Ranorex Snapshot. Learn how to create one >here<
- Ranorex xPath of problematic element(s)
- Ranorex version
- OS version
- HW configuration
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Access to repository from user code collection
Sorry, this particular solution happens to be VB. Here it is as run through the code converter over at Telerik.comEugeneMkv wrote: I'm not into VB.NET stuff but into C# instead.
Ok, then, the only one problem for me is to access my repo...
Code: Select all
[UserCodeMethod]
public static void ValidateDate(string myAttribute, string myExpected, Ranorex.Core.Repository.RepoItemInfo myObjectInfo)
{
Report.Log(ReportLevel.Info, string.Format("Attempting to convert the '{2}' attribute of '{3}'; '{0}' and the expected value '{1}' to valid dates for comparison", Host.Current.FindSingle(myObjectInfo.AbsolutePath).Item(myAttribute).ToString(), myExpected, myAttribute, myObjectInfo.Name));
Validate.AreEqual(StringToDate(Host.Current.FindSingle(myObjectInfo.AbsolutePath).Item(myAttribute).ToString()), StringToDate(myExpected), "Expect '{0}' to match '{1}'", false);
}
Code: Select all
Host.Current.FindSingle(myObjectInfo.AbsolutePath).Item(myAttribute).ToString()
Doug Vaughan
Re: Access to repository from user code collection
But is it possible to create common User Code Library without using namespace Ranorex_Automation_Helpers?But the truth is that I did not tried it within the test automation helpers project.
Thanks, Vaughan Douglas! I get your point and will try this approach!Then the Host.Current.FindSingle takes a RxPath as an argument and it goes out and finds the object so you don't need a reference to your repository.
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Access to repository from user code collection
Sure thing!!EugeneMkv wrote: But is it possible to create common User Code Library without using namespace Ranorex_Automation_Helpers?
Right click on whatever project you want to add the user code collection.
Select 'Add New'
You'll see "User Code Collection" under the "Ranorex" category. My approach is to add a Ranorext Test Suite Module Library project to my solution and stick my user code collection(s) in there. That way I can keep all of my general modules and methods in one place and don't end up with circular references.
You do not have the required permissions to view the files attached to this post.
Doug Vaughan