SenseTalk: List of Lists Possible?

Hi Everyone,

This is my very first post on this forums. I've been using SenseTalk|Eggplant for more than a week. I've written a little over 500 lines of code to run automated tests.

So far so good. I've decided that before I continue with more complex and longer milestones in my test automation script I want to really understand my options with this code.

I want to know if a list of can reference another list. If yes, how do I access the the 'sublist'. The syntax looks correct. But I am having problems accessing / reading the 'sublist'. See code below:
Set ItemList = {
	(found:false,foundAt:0,name: "item1",image: "item1.png",sublist: itemList1),
	(found:false,foundAt:0,name: "item2",image: "item2.png",sublist: itemList2),
	(found:false,foundAt:0,name: "item3",image: "item3.png",sublist: itemList3)
}

Set itemList1 = {
	(found:false,foundAt:0,name: "sub1_item1",image: "sub1_item1.png",),
	(found:false,foundAt:0,name: "sub1_item2",image: "sub1_item2.png",),
	(found:false,foundAt:0,name: "sub1_item3",image: "sub1_item3.png",)
}

Set itemList2 = {
	(found:false,foundAt:0,name: "sub2_item1",image: "sub2_item1.png",),
	(found:false,foundAt:0,name: "sub2_item2",image: "sub2_item2.png",),
	(found:false,foundAt:0,name: "isub2_tem3",image: "sub2_item3.png",)
}
Set itemList3 = {
	(found:false,foundAt:0,name: "sub3_item1",image: "sub3_item1.png",),
	(found:false,foundAt:0,name: "sub3_item2",image: "sub3_item2.png",),
	(found:false,foundAt:0,name: "sub3_item3",image: "sub3_item3.png",)
}



Repeat with each item of ItemList
	Set I = repeatIndex()
	logSuccess (item I of ItemList).name &", " & (item I of ItemList).image & ", " & (item I of ItemList).found & ", " & (item I of ItemList).foundAt
	
	(* This part gives me an error: *)
	
	Set Sublist = (item I of ItemList).sublist
	Repeat with each item of Sublist
		Set II = repeatIndex()
		logSuccess (item II of SubList).name &", " & (item II of SubList).image & ", " & (item II of SubList).found & ", " & (item II of SubList).foundAt
	End Repeat
End Repeat

I've tried several ways to try to access the sublists unsuccessfully.

Error I got:
12/22/16, 9:04:44 AM START Running nestedList_test.script
12/22/16, 9:04:44 AM LogSuccess item1, item1.png, False, 0
12/22/16, 9:04:44 AM EndTestCase (Duration:"0.186", Errors:"1", Exceptions:"0", StartTime:"2016-12-22 09:04:44 -0600", Successes:"1", TestCase:"nestedList_test.script", Warnings:"0")
12/22/16, 9:04:44 AM FAILURE STUnknownMessage ERROR: No Such Function: 'name' -- Execution Time 0:00:00 nestedList_test.script

The purpose of having lists referencing other lists is because I want to loop or repeat through elements of a list (to validate they loaded) and after that make an action with each element and for each action you usually have another screen and another set of elements to validate and so on. In other words, I want to iterate on elements on a list, and jump to a different set of elements to validate as well, branching off the initial list.

What I am try to do sounds more complicated that it would actually be and it will my coding task easier.

Any ideas? Suggestions that can put me in the right direction?

Comments

  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    The script as it stands now almost works, but I have several suggestions and things to point out.

    First, the fundamental mistake is that when you create your master ItemList on the first line of the script, you set each “sublist” property in the list to the variables ItemList1, ItemList2, and ItemList3. However, those variables haven’t been assigned a value yet, so SenseTalk uses their name as their value. If you add a “put ItemList” command immediately after the assignment you’ll see what’s happening.

    So, lesson one here is that SenseTalk is very literal and normally assigns everything by copying, not by reference.

    There are two simple solutions to this problem. The first is simply to move the assignment of ItemList down to the end of your assignments, after ItemList1, ItemList2, and ItemList3 have been assigned their values.

    The second solution is to actually use references to variables instead of copies. Your initial assignment would then look like this:
    Set ItemList = { 
    	(found:false,foundAt:0,name: "item1",image: "item1.png",sublist: reference to itemList1), 
    	(found:false,foundAt:0,name: "item2",image: "item2.png",sublist: reference to itemList2), 
    	(found:false,foundAt:0,name: "item3",image: "item3.png",sublist: reference to itemList3) 
    } 
    
    The important thing to remember about references is that any later changes to the value of itemList1, for example, will change the corresponding value in ItemList. References aren’t typically used very much in eggPlant scripts, but you can certainly take this approach if you prefer.

    Either of those solutions will get your script working, but I hate to see people making things hard for themselves, so I’d like to also suggest some improvements to your repeat loops.

    When you use a loop like this:
    Repeat with each item of ItemList 
        // statements here
    End Repeat
    
    The variable IT is automatically assigned the value of each item of itemList on each time through the loop. So there’s no need to fiddle with the repeatIndex and accessing “item I of ItemList”. Of course, since you’re using nested repeat loops it becomes problematic to have the inner loop also using the default variable IT. It’s better in this case to provide your own loop variable for each loop.

    I would write the code more like this:
    Repeat with each item master of ItemList 
    	logSuccess master.name &", " & master.image & ", " & master.found & ", " & master.foundAt 
    	
    	Repeat with each item detail of master.sublist 
    		logSuccess detail.name &", " & detail.image & ", " & detail.found & ", " & detail.foundAt 
    	End Repeat 
    End Repeat 
    
    Hopefully that will get your coding off to a better start!
  • AndrewRAndrewR Member
    SenseTalkDoug,

    Thank you very much for clarifying. My intention was to add actual references but did not know how to do it properly.

    Also thanks for the IT advice. Its a quality of life improvement.
Sign In or Register to comment.