Matching String for Evaluate regex

891 views
Skip to first unread message

Jürgen Müller

unread,
Feb 4, 2014, 9:18:31 AM2/4/14
to robotframe...@googlegroups.com
Hi there,

I've Problems with the following Keywords (Part of Testcases)

Grep Password "${user}"
    Sleep    5.0
    @{titles}    Get Window Titles
    :FOR    ${title}    IN    @{titles}
    \    Log    ${title}
    \    ${match}=    Evaluate    re.search('(Firmenname: - .*)','${title}')    re
    \    Run Keyword If    ${match}    Assign Popup Title    ${match}
    Select Window    ${POPUP TITLE}
    .
    .
    .
Assign Popup Title
    [Arguments]    ${matchObject}
    ${popUpTitle}=    Evaluate    ${matchObject}.group(1)    re
    Set Test Variable    ${POPUP TITLE}    ${popUpTitle}

What I'm trying to do is to switch to some popup I opened from my main window. The Problem is that the title of the popup changes each time the parent page is loaded and the popup is opened again. So my way is to get all titles of all available Windows after opening the popup and then get the actual title by doing an robotframework builtin eval with python's re Engine. I can't do this by the whole bunch of "should" or "should not" keywords, because of all the windows titles, only one is matching!

Now I've problems getting the "real" text from the _sre.SRE_Match object which I get assigned to ${match}.
My first attempt was to just say

   \    ${match}=    Evaluate    re.search('(Firmenname: - .*)','${title}').group(1)    re

but then where is again a problem because of the titles not matching (they don't have a method "group(1)"). So I ended up with trying to evaluate conditional and do the assignment in a custom keyword. But this ends up in:

20140204 14:25:42.945 :  INFO : Slept 5 seconds
20140204 14:25:43.483 :  INFO : 
Altogether 2 items.
1: XXXXXXXXXXXXXXXX
2: Firmenname: - PPza9
20140204 14:25:43.484 :  INFO : @{titles} = [ XXXXXXXXXXXXXXXX | Firmenname: - PPza9 ]
20140204 14:25:43.485 :  INFO : XXXXXXXXXXXXXXXX
20140204 14:25:43.487 :  INFO : ${match} = None
20140204 14:25:43.489 :  INFO : Firmenname: - PPza9
20140204 14:25:43.490 :  INFO : ${match} = <_sre.SRE_Match object at 0x000000000471D300>
20140204 14:25:43.496 :  FAIL : Evaluating expression '<_sre.SRE_Match object at 0x000000000471D300>.group(1)' failed: SyntaxError: invalid syntax (<string>, line 1)
Ending test:   TestPI.Testsuite.Fachadmin.Test Search User

So my question again: "How can I get the matching String in robotframework on basis of the match object I already have (or think to have)
Any other suggestions are also welcome of how to Switch to a popup when only the first part of the popup title is known at test start time.

greetings
Jürgen

Jürgen Müller

unread,
Feb 4, 2014, 10:04:52 AM2/4/14
to robotframe...@googlegroups.com
Hallo again,

my original problem is solved as I can just assign the ${title} to my Variable. So it becomes:

Grep Password "${user}"
    Sleep    5.0
    @{titles}    Get Window Titles
    :FOR    ${title}    IN    @{titles}
    \    Log    ${title}
    \    ${match}=    Evaluate    re.search('(Firmenname: - .*)','${title}')    re
    \    Run Keyword If    ${match}    Set Test Variable    ${POPUP TITLE}    ${title}
    Select Window    ${POPUP TITLE}
    .
    .
    .

But it would be nethertheless interesting to know if it's possible to get the match string in robotframework test case. Or does one have to make it in a python lib providing a custom keyword?

greetings
Jürgen

Kevin O.

unread,
Feb 4, 2014, 1:28:25 PM2/4/14
to robotframe...@googlegroups.com
Another reason to do it in Python is embedded quotes. They can be mitigated somewhat by using triple-quotes, but can still blow up if the string starts or ends with a quote.

When using a variable in Evaluate, it will only work like you want it to if the object has a string representation of itself that eval can use. Something like this eval(repr(x))==x.
For a match object, this clearly does not work.
Once you have the match object, you do not need Evaluate.

Try changing
    ${popUpTitle}=    Evaluate    ${matchObject}.group(1)    re
to
    ${popUpTitle}=    Set Variable    ${matchObject.group(1)}
or
    ${popUpTitle}=    Call Method    ${matchObject}    group    1
Reply all
Reply to author
Forward
0 new messages