String comparison with List

Hi Team,
I want to compare a string to a list and see if the string is present in the list.

The string is - phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name

But the test case is failing saying pattern does not match.

Is it because of the square brackets present in the code?, Please suggest

My Code
Get Verify Config Results
${verify_results}= Create List
@{results_list} Get WebElements ${VERIFY CONFIG RESULTS}
FOR ${element} IN @{results_list}
${results} Get Text ${element}
Append To List ${verify_results} ${results}
END
[Return] ${verify_results}

Verify Message In Verify Config Results
Open Verify Drawer
${config_results} Get Verify Config Results
Should Contain Match ${config_results}
… phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name

Hi Madhurya,

At first I was going to ask you to add a Log ${config_results} to verify it’s actually a list of strings, but then I tried it and got the same result as you using Robot Framework 5.0 (Python 3.9.10 on darwin), I then upgraded to Robot Framework 6.0.2 (Python 3.9.10 on darwin) and reproduced the same result again.

I also confirmed this behaviour with square brackets is different to the equivalent string using round or curly brackets, this issue is specific to square brackets.

I believe you may have found a bug. @pekkaklarck do you know who maintains Collections Library?

Here is my test example (Only uses BuiltIn and Collections Libraries)

madhurya.robot

*** Settings ***
Library		Collections


*** Variables ***

${verify_message}		phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name

*** Test Cases ***
madhurya
	${config_results} 	Get Verify Config Results
	Log		${config_results}
	# Should Contain Match 	${config_results}
	# ...	phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name
	Should Contain Match 	${config_results} 	${verify_message}


madhurya 2
	${verify_message}=	Set Variable 	madhurya_3
	@{results}=		Create List		madhurya_1	madhurya_2	madhurya_3	madhurya_4	madhurya_5	madhurya_6
	Log		${results}
	Should Contain Match 	${results} 	${verify_message}

madhurya 3
	@{config_results}= 	Get Verify Config Results Strings
	Log		${config_results}
	# Should Contain Match 	${config_results}
	# ...	phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name
	Should Contain Match 	${config_results} 	${verify_message}

madhurya 4
	${verify_message}=	Set Variable 	example [1] string [1]
	@{config_results}= 	Build a list of strings with square brackets
	Log		${config_results}
	Should Contain Match 	${config_results} 	${verify_message}

madhurya 5
	${verify_message}=	Set Variable 	example (1) string (1)
	@{config_results}= 	Build a list of strings with round brackets
	Log		${config_results}
	Should Contain Match 	${config_results} 	${verify_message}

madhurya 6
	${verify_message}=	Set Variable 	example {1} string {1}
	@{config_results}= 	Build a list of strings with curly brackets
	Log		${config_results}
	Should Contain Match 	${config_results} 	${verify_message}

*** Keywords ***
Get Verify Config Results
	@{results}=		Create List		phy-interface [test] of type [Control] with port [0] and slot [0] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [0] and slot [1] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [0] and slot [2] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [1] and slot [0] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [2] and slot [0] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [2] and slot [1] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [3] and slot [0] has invalid name
	Append To List	${results}	phy-interface [test] of type [Control] with port [3] and slot [1] has invalid name

	[Return]	${results}


Get Verify Config Results Strings
	@{results}=		Create List

	FOR 	${i}	IN RANGE	3
		FOR 	${j}	IN RANGE	3
			${strng}=		Set Variable		phy-interface [test] of type [Control] with port [${i}] and slot [${j}] has invalid name
			Append To List	${results}	${strng}
		END
	END
	[Return]	${results}

Build a list of strings with square brackets
	@{results}=		Create List

	FOR 	${i}	IN RANGE	3
		FOR 	${j}	IN RANGE	3
			${strng}=		Set Variable		example [${i}] string [${j}]
			Append To List	${results}	${strng}
		END
	END
	[Return]	${results}


Build a list of strings with round brackets
	@{results}=		Create List

	FOR 	${i}	IN RANGE	3
		FOR 	${j}	IN RANGE	3
			${strng}=		Set Variable		example (${i}) string (${j})
			Append To List	${results}	${strng}
		END
	END
	[Return]	${results}

Build a list of strings with curly brackets
	@{results}=		Create List

	FOR 	${i}	IN RANGE	3
		FOR 	${j}	IN RANGE	3
			${strng}=		Set Variable		example {${i}} string {${j}}
			Append To List	${results}	${strng}
		END
	END
	[Return]	${results}

What would be the solution for this @damies13

Is this an issue? if so then how will I know about the issue and track it when resolved?

What can I do now for verifying this?

Hi Madhurya,

I’m not sure where Collections Library is maintained, I think it’s one of the BuitIn Libraries, but most issue tracking systems have a follow option if there’s an existing issue, otherwise if you need to raise it you’ll automatically get notified.

Hopefully we’ll both find out in the next few days.

Dave.

Thanks Dave, Is there any alternative for this issue or shall I mark it as blocked for now, till I hear from anyone if it is an issue or not.

Hi Madhurya,

I don’t know of any workaround other than to not use square brackets, which is not great as you can’t really change the system your testing.

I also tried escaping the square brackets (e.g. \[ and \]) but it didn’t help same issue.

Dave.

Hi Madhurya,

I had another look and found the project where Collections Library exists: Collections.py

I would suggest you raise an issue here and link to this forum post.

Dave.

Sure Dave, I will create an issue

1 Like

The issue raised for this issue -

This keyword uses glob patterns for matching and with them square brackets have a special meaning so that, for example, [abc] matches either a, b or c. If you want to have literal [abc] in a glob pattern, you need to escape it like [[]abc]. You could also use ?abc], but then then any character will match ?. If you use [[], it will only match [.

1 Like

Hi @pekkaklarck

I want to verify the below string
String - phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name

So the right way would be
phy-interface [test] of type [Control] with port [1] and slot [1] has invalid name

Is this correct? And is it a good programming practice

And this works only when we are hardcoding the value (passing the value to the keyword) and comparing

What if we need to compare the string run time, in that case, this wont work right.

Your example is formatted somewhat strangely so it’s hard to tell is it correct or not. The easiest way for you is to test it.

Notice that if you don’t need pattern matching, you could simply use Should Contain instead of Should Contain Match.