Using read_until_regexp() with changing prompts

209 views
Skip to first unread message

Andrew Palmer

unread,
Mar 4, 2016, 5:29:09 PM3/4/16
to robotframework-users
Hi, 

I have a question I was hoping to get some advice on. I have a library currently that executes a given cmd. Let's say, for a linux cmd: my_cmd, which produces an interactive menu requiring a series of answers. So in my case:

prompt = ':'
for answer in menu
    ssh.write(my_cmd)
    read_until_regexp(prompt)

Now, let's say the output for the cmds are as follows:

[ #] some_cmd
Would you like to continue?
y
Enter a value:
andy
You entered andy, please try again:
(valid values are a, b, c)


Now you can see, if I provide ':' as a prompt, the problem that will happen. In the third question, there is still a line after the ':'. 

What I'm wondering, has anyone found a way to just read until the prompt is finished? So that if the output is:
"Enter your response:"
or
"Please specify a value:
(a, b, or c)"
It would know to wait until the very end regardless of match.
Since it seems that it reads the output one char at a time, it would be difficult to use a regex or some other match to check for last end of line or new line. Hopefully my question is clear.

Thanks you.

Hélio Guilherme

unread,
Mar 4, 2016, 6:18:26 PM3/4/16
to robotframework-users

What I'm wondering, has anyone found a way to just read until the prompt is finished? So that if the output is:
"Enter your response:"
or
"Please specify a value:
(a, b, or c)"
It would know to wait until the very end regardless of match.
Since it seems that it reads the output one char at a time, it would be difficult to use a regex or some other match to check for last end of line or new line. Hopefully my question is clear.

Thanks you.

From the documentation [1] we see that you could try using multiline modifier, and I think you can try more complex regex.
Here are my rough proposals:
prompt = '(?m)(:\n[(].*)|(:)'
This was tested at https://regex101.com/#python and the result is in the image below:
 

That may require further processing of the output.

Other alternative, if you would be waiting for the complete output is waiting for the system prompt and process the whole output (using a file and String library).


[1] http://robotframework.org/SSHLibrary/latest/SSHLibrary.html

 

Andrew Palmer

unread,
Mar 7, 2016, 12:26:15 PM3/7/16
to robotframework-users
Thanks for your response. Though the regex does work on the text string, because read_until goes char by char, it will end up matching the ':' first. Still looking through alternative solutions such as adding a new arg that will take both the answers and prompts in a list and switch the prompt for each answer.

Definitely would appreciate any other feedback or options!

Thanks,
Reply all
Reply to author
Forward
0 new messages