Creating and accessing Lists

Ask general questions here.
Maikel
Posts: 3
Joined: Fri Aug 04, 2017 12:52 pm

Creating and accessing Lists

Post by Maikel » Fri Aug 04, 2017 1:49 pm

Hello,

I'm on Windows 10 Enterprise and using Ranorex Studio 7.1.0

I have a simple website with 4 combo boxes containing 4 items each which have a different value attribute each. When you choose 4 items, the added total (the 'price') is displayed below the boxes (if less options are chosen it just shows the text 'please choose 4 options'). The test I setup in Ranorex uses one test case to check 4 different combinations of items and then checks the price whether it compares to an expected price (also included in an Excel-sheet that drives the suite). Generally this works as intended (sometimes it checks for the added price too quickly and gives an error because it compares the expected price to the text 'please choose 4 options', I added some delays but if I run the same suite multiple times it sometimes gets it right and sometimes doesn't, no idea why that is the case).

What I want is to create a list before the iterations and the testcase, then store a value/message when it validates the price in the list and in the end - thus after the iterations - print the list in the report. This sounds rather basic, but if I get this right I can enhance it much further. The overall idea behind this is that I want to create some sort of summary which I can manipulate further (for example: show detailed information about which configuration didn't show the correct price, how much the difference to the expected price was, et cetera).

For this I added a Setup region before my testcase and added a code module (named SummarySetup) who's only purpose it is to create a list:
public static List<string> summaryList
        	{
        		get {return summaryList;}
        		set {summaryList = value;}
       		}
The price-checker consists of a small code block, which has two arguments (the checked price and the expected price) and compares if these are equal (and logs both if/else through Report.Info("price correct/price incorrect"). To this end I added one extra line: namespace.SummarySetup.summaryList.add(chosen value).

I also added a Teardown region which contains a code block to print all the values:
public static void PrintItAll()
    	{
    		foreach(string a in namespace.SummarySetup.summaryList)
    		{
    			Report.Info(a);
    		}
    	}
So long story, but two things are wrong here:
- Whenever the Add line is used, I immediately get a StackOverFlowException and the whole test execution freezes. Somehow the line/method is repeatedly called upon but I have no idea how to fix this or work around it. I tried moving the line to another place but it still results in the Exception whenever it is reached.
- The report doesn't seem to use the method PrintItAll to log it into the report (which is empty below that line). When I add extra lines like 'Report.Info("Test") these do show up in the console window, but not in the report, which is where I want it to show up so I can extend upon it.

My C# code knowledge is lacking, but if I do this in Visual Studio it works as intended.

Thanks in advance for any help!

Maikel

Maikel
Posts: 3
Joined: Fri Aug 04, 2017 12:52 pm

Re: Creating and accessing Lists

Post by Maikel » Tue Aug 08, 2017 3:43 pm

Ok some more info:

When I do not use a List but simply a string (which gets overwritten instead of added) I do get it to work as intended and my Report shows the given value/message.

Another approach: creating the list using the line:
public static List<string> summaryList {get;set;}
Now I just want to focus on the summary so I don't add anything through the modules but only add a few items right after creating the list. The error I get is: "object reference not set to an instance of an object" which is also displayed in the report. So I guess that's something, although far from correct.

Once again thanks for any help!

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

Re: Creating and accessing Lists

Post by krstcs » Tue Aug 08, 2017 5:08 pm

When declaring a complex type, you must instantiate the object before you can use it.

Code: Select all

private static List<string> summaryList = new List<String>();
public static List<string> SummaryList
{
    get
    {
        return summaryList;
    }
}
You should really control access to the base List object by wrapping it in a get like above. With a complex object, since you have to instantiate it, especially with a static declaration, you have to do this in two steps.
Shortcuts usually aren't...

Maikel
Posts: 3
Joined: Fri Aug 04, 2017 12:52 pm

Re: Creating and accessing Lists

Post by Maikel » Wed Aug 09, 2017 2:48 pm

Thanks krstcs, that mostly solved my problem.

Another error was in my PrintItAll method, it should have read Report.Log(ReportLevel.Info, "message") as someone from the Test Automation Engineers team kindly pointed out to me. This person also was kind enough to suggest using the WaitFor action instead of Delays, so that also made my test execution much more reliable.

Thus all my questions are answered!