Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Ask general questions here.
Ben Jones
Posts: 12
Joined: Mon Oct 21, 2019 10:37 am

Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by Ben Jones » Wed Apr 08, 2020 12:37 pm

Hi,
I am running Ranorex version: 9.1.2+git.afa46adf
and .NET Runtime version: 4.0.30319.42000
(upgrading to 9.2 is not an option at this moment in time).

In summary
I would like to use the Validate.NotExisits method on a RepoItemInfo object that relates to a web element that does not exist and is saved as a RepoItemInfo variable in c# code without throwing "no element found" error when creating the repoItemInfo variable. I have attached a compressed folder containing the files "WebflowIndexFormFieldFunctions.cs" and "IndexField.cs".

Where:
WebflowIndexFormFieldFunctions.cs contains the c# method run by the CodeModule IndexField.
IndexField.cs is the code module used in the test suite.

My code flow
  1. My code module IndexField takes a string parameter and changes a repository variable (file: IndexField.cs)
        	private static R4ItemRepository r4Repo;
        	
            /// <summary>
            /// Constructs a new instance.
            /// </summary>
            public IndexField()
            {
            	r4Repo = new R4ItemRepository();
            }
    
            [TestVariable("2a058703-65c5-4cb4-9ebf-d9f1705c1ca8")]
            public string fieldToIndex
            {
            	get { return r4Repo.HeaderIndexName; }
            	set { r4Repo.HeaderIndexName = value; }
            }
    
  2. It passes the reference to the static repository (and other string variables) to my IndexFields function (file: IndexField.cs)
            WebflowIndexFormFieldFunctions webflowFunctions = new WebflowIndexFormFieldFunctions();
    
            void ITestModule.Run()
            {
                Mouse.DefaultMoveTime = 300;
                Keyboard.DefaultKeyPressTime = 100;
                Delay.SpeedFactor = 1.0;
                
                webflowFunctions.IndexFields(r4Repo, headerIndexingValue, disabled, mandatory, readOnly, specialFeature);
                           
            }
    
  3. The IndexFields function creates a repoItemInfo and repoItem variable that reference the repository item to validate. (File: WebflowIndexFieldFormFuncitons.cs)
    		public void IndexFields(R4ItemRepository r4Repo, string indexValue, string disabled, string mandatory, string readOnly, string specialFeature)
    		{
    			bool disabledBool = Convert.ToBoolean(disabled);
    			
    			if (disabledBool)
    			{
    				
    			}
    			
    			string headerName = r4Repo.HeaderIndexName;
    			Report.Info("Creating variables");
    			string repoItemName = r4Repo.DocumationWorkflow.IndexingPanel.HeaderIndexScreen.HeaderField.Name;
    			Report.Info("Created Repo item name");
    			RepoItemInfo repoItemInfo = r4Repo.DocumationWorkflow.IndexingPanel.HeaderIndexScreen.HeaderFieldInfo;
    			Report.Info("Created Repo item info");
    			InputTag repoItem = r4Repo.DocumationWorkflow.IndexingPanel.HeaderIndexScreen.HeaderField;
    			Report.Info("Created Repo item");
                      .
                      .
                      .
    
  4. Later in the IndexFields method the ValidateFieldDisabled method is called that validates if the web element exists if it should and validates it doesn't exist when it shouldn't. (File: WebflowIndexFieldFormFuncitons.cs)
    		public bool ValidateFieldDisabled(RepoItemInfo repoItemInfo, string disabled)
    		{
    			bool disabledBool = Convert.ToBoolean(disabled);
    			
    			if (disabledBool)
    			{
    				Report.Info("Expected field to be disabled. Checking the field does not exist.");
    				Validate.NotExists(repoItemInfo);
    				return true;
    			}
    			else
    			{
    				Report.Info($"Expected field to be enabled. Checking field exists.");
    				Validate.Exists(repoItemInfo);
    				return false;
    			}
    		}
    
Outcome
When either of the lines are executed
RepoItemInfo repoItemInfo = r4Repo.DocumationWorkflow.IndexingPanel.HeaderIndexScreen.HeaderFieldInfo;
InputTag repoItem = r4Repo.DocumationWorkflow.IndexingPanel.HeaderIndexScreen.HeaderField;
the attached error is thrown.

Expected
I expected the variables to be initialised as when the HeaderField repository item exists the code is executed without error.
Also, when I create a recording module and use the validate action on the HeaderField repo item it works and validates successfully when the repoitem references a web element that doesn't exist.
I could implement this into my test but I would much prefer to perform the validation in the IndexFields method called in IndexField.cs as this would be easier to maintain and would keep the test suite looking neat and easy to read. It would also keep all the relevant actions in one place making it easier to reuse the steps in other test cases.


Thanks in advance :D
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by odklizec » Wed Apr 08, 2020 12:54 pm

Hi,

Well, since you are using an old (obsolete) version of Ranorex (current is 9.3.1), it may be hard to help you with your issue.

At first, please post a Ranorex snapshot of the problematic element. Ideally post a complete (simplified) solution, which could be compiled and tested against Ranorex or any other web.

Also, please make sure the app under test is whitelisted, as mentioned in the error. I'm not quite sure if it's OK to pass the repo elements to methods the way you did it? Usually, it's recommended (and supported) to use RepoItemInfo or Adapter parameters to pass repo elements to methods.
Pavel Kudrys
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

Ben Jones
Posts: 12
Joined: Mon Oct 21, 2019 10:37 am

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by Ben Jones » Wed Apr 08, 2020 2:31 pm

Hi,

In answer to your statements:
At first, please post a Ranorex snapshot of the problematic element. Ideally post a complete (simplified) solution, which could be compiled and tested against Ranorex or any other web.
Providing a snapshot like the one you mentioned will take some time, I will try to get one created for you but will not be soon.

please make sure the app under test is whitelisted
The app is whitelisted as I have no trouble validating the web element exists when it should.

Usually, it's recommended (and supported) to use RepoItemInfo or Adapter parameters to pass repo elements to methods.
I am using a RepoItemInfo object.
How would I use an Adapter to pass the repository item to the NotExists method? And if I can would I be able to instantiate a variable and use that to reference to Adapter in other methods in the class?


Regards

Ben

User avatar
RobinHood42
Posts: 324
Joined: Fri Jan 09, 2015 3:24 pm

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by RobinHood42 » Fri Apr 10, 2020 11:37 am

Hi Ben,

The information your get from the report is that the element with the specific RxPath cannot be found. I would check if the element can be found with this RxPath at the time. You wrote that it works when using a recording. You can compare the RxPath in the report with the corresponding element in the repository. Also, you can open the cs file of the recording and the repository and compare it with your implementation.

Cheers,
Robin

Ben Jones
Posts: 12
Joined: Mon Oct 21, 2019 10:37 am

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by Ben Jones » Tue Apr 14, 2020 12:57 pm

Hi, thanks for your reply Robin. I have checked the paths and they are the same

Ben Jones
Posts: 12
Joined: Mon Oct 21, 2019 10:37 am

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by Ben Jones » Tue Apr 14, 2020 1:00 pm

I have created a simplified solution to highlight my issue. I have attached it as a zip file. The solution is called RanorexSnapshot though I now realize a snapshot is something different
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by odklizec » Tue Apr 14, 2020 1:50 pm

Hi,

I'm still in doubt about why you are accessing repo elements like this, but I now probably understand what the issue is.

The problem is here:
AdapterFailure.png
What you are trying to do here is to create a variable from Adapter. But if the element behind the adapter does not exists, it will throw "element does not exists" exception. This is normal and expected behavior.

If you want to avoid this error, you must use RepoItemInfo element, check its existence and only then you may try to create a variable from Adapter! So the code should look like this:
AdapterFailure_fix.png
You see, you should not access the adapter without checking the existnce of RepoInfo element. Hope this helps?
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 Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Ben Jones
Posts: 12
Joined: Mon Oct 21, 2019 10:37 am

Re: Initialising RepoItem and RepoItemInfo variables from repository item for web element that does not exist

Post by Ben Jones » Tue Apr 14, 2020 2:23 pm

Hi, yup this helps a lot. I have implemented your suggestion:
			Button repoItem = null; // set to null to avoid build errors. Code should never call on this if it remains null as no element exists to interact with.
			if (repoItemInfo.Exists())
			{
				repoItem = repo.GoogleGoogleChrome.Element;
				Report.Info("Created Repo item");
				
			}
			else
			{
				Report.Info("Unable to create repoItem as repoItemInfo.Exists() returned false.");
				
			}

and built and run the test successfully