robot框架中的模式匹配:python

2024-09-28 22:26:21 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个代码,可以比较使用robot框架点击url时生成的cookie模式, 使用字符串变量

REGMATCH
     ${string}=  set variable   hgf
     ${matches}=  get regexp matches  ${string}  asdfhgfpoyrjgrrhkjhoolk
     should be equal as strings  ${matches}  hgf

但这个结果是错误的

我在下面试过这个

Get Lines Matching Pattern Matching Some Lines
    Test Get Lines Matching Pattern    asdfhgfpoyrjgrrhkjhoolk    ??????   hgf
 
 ***KEYWORD***
Test Get Lines Matching Pattern
    [Arguments]    ${input}    ${pattern}    ${expected}    ${case-insensitive}=no
    ${actual} =    Get Lines Matching Pattern    ${input}    ${pattern}    ${case-insensitive}
    Should Be Equal    ${actual}    ${expected}

有人能帮我吗


Tags: testinputgetstringpatternexpectedlinescase
1条回答
网友
1楼 · 发布于 2024-09-28 22:26:21
${actual} =    Get Lines Matching Pattern    ${input}    ${pattern}    ${case-insensitive}

您的模式是?????,这意味着正好有5个字符,输入超过5个字符

asdfhgfpoyrjgrrhkjhoolk可以使用那么多的“?”作为?以全局格式报告一个字符,您可以使用

${actual} =    Get Lines Matching Pattern    ${input}    *    ${case-insensitive}

这意味着整个字符串

对于y=用例,您可以使用

Should Match Regexp    hithsihdisdh hsdaasidh 123 dfsdfdsf   123

这将检查字符串是否有“123”。这个关键字来自柏林图书馆

Library    BuiltIn

下面的示例显示了每个关键字的正确用法:

*** Setting ***
Library    String
Library    BuiltIn
Library  Selenium2Library


*** Test Cases ***
Example Search
    [Tags]    you    probably    do    not    have    this    many    tags    in    real    life
    ${first} =  Catenate    SEPARATOR=\n    Not in second   Same stuff  Differs Same
    #this is use ful to find line the pattern       
    ${a}=    Get Lines Matching Pattern     ${first}   Same*    case_insensitive=true

    #this match the entire multi line string has any match
    ${b}=    Should Match Regexp    ${first}   Same

    #this gives a list of all matches
    ${c}=    Get Regexp Matches    ${first}   Same

    Log    ${a}
    Log    ${b}
    Log    ${c}

相关问题 更多 >