如何使用响应中的网址

时间:2019-03-25 08:52:38

标签: python-2.7 tdd robotframework

帮助理解和指导下一步需要做的事情。我使用机器人框架版本3.1.1和Python 2.7编程语言编写自动测试脚本,现在有一项任务可以开发可与POST / GET请求一起使用的测试。因此,我向系统发出POST请求以启动付款。答案是付款已经创建,但是对于所有操作,都必须点击答案内的链接。

请求:

Create Session    allias    URL
&{params}=    Create Dictionary    params1=value1  params2=value2  
${resp}=    Post Request    allias    /init_payment.php    params=${params}
Should Be Equal As Strings    ${resp.status_code}    200

回复帖子回复:

 <?xml version="1.0" encoding="utf-8"?>
<response><pg_status>ok</pg_status><pg_payment_id>3016695</pg_payment_id><pg_redirect_url>https://test.paybox.money/payment.html?customer=028c44bf25b6be251199221d04b570c2</pg_redirect_url><pg_redirect_url_type>need data</pg_redirect_url_type><pg_salt>8OYUsodtnaTWKbOD</pg_salt><pg_sig>a26e61d6eb710c430d67150498d1f555</pg_sig></response>

如何显示框架以便它可以通过重定向URL?感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

机器人框架中的RequestsKeywors使用Requests HTTP客户端。从该库中读取quickstart,您可以在text字段中找到响应的内容。

${body}=    ${resp.text}

那么您很可能想解析XML。您可以为此使用XML library

这将是

${root} =   Parse XML   ${body}
${link} =   Get Element Text    ${root} response/pg_redirect_url

答案 1 :(得分:0)

这是当您尝试执行必要的操作以查找项目时发生的情况。 但是我仍然无法确定框架的目标。无论是在带有网址的行还是...

*** Settings ***
Library           Collections
Library           String
Library           RequestsLibrary
Library           OperatingSystem
Library           XML

${resp}=    Post Request    test.paybox.money    /init_payment.php    params=${params}
${XML}=    Parse XML    ${resp.text}
${text}=    Get Element Text    ${XML} /response/pg_redirect_url

20190327 17:31:43.827 :  INFO : Post Request using : alias=test.paybox.money, uri=/init_payment.php, data=<empty>, headers=None, files=None, allow_redirects=True 
20190327 17:31:43.827 :  INFO : 
c:\python27\lib\site-packages\urllib3\connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
20190327 17:31:43.828 :  INFO : ${resp} = <Response [200]>
20190327 17:31:43.829 :  INFO : ${XML} = <Element 'response' at 0x0426FCC8>
20190327 17:31:43.830 :  FAIL : ParseError: not well-formed (invalid token): line 1, column 9
20190327 17:31:43.830 : DEBUG : 
Traceback (most recent call last):
  File "<string>", line 62, in parse
  File "<string>", line 38, in parse