0

我尝试使用 BrowserStack 中给出的 CURL 将测试标记为失败/通过。我已经在 pycharm 中使用机器人框架实现了自动化代码。我在我的测试文件中添加了每个测试用例的测试状态。

使用卷曲

curl -u "<username>:<key>" -X PUT -H "Content-Type: application/json" -d "{\"status\":\"<new-status>\", \"reason\":\"<reason text>\"}" https://api-cloud.browserstack.com/app-automate/sessions/<session-id>.json

Pycharm 控制台说

HTTPError:400 客户端错误:对 url 的错误请求:https: //api.browserstack.com/automate/sessions/.json

以前它显示 422 Unprocessable Entity 错误。

我的机器人固件代码如下所示

*** Settings ***
Library           SeleniumLibrary
Library           RequestsLibrary
Library           Collections
Library           JSONLibrary

*** Variables ***

${SiteUrl}        <test_web_url>
${BSUser}   <username>
${AccessKey}   <password>
${RemoteUrl}   http://${BSUser}:${AccessKey}@hub.browserstack.com/wd/hub

${BaseURL}    https://api.browserstack.com


${ver_txt1}   Table
${ver_txt2}    Take a look at our menu below and call the server to order
${ver_txt3}   More Information


    *** Test Cases ***
    
    
    landing page test
    
        Open Browser   url=${SiteUrl}   browser=chrome   remote_url=${RemoteURL}   desired_capabilities=browser:chrome,browser_version:latest,os:Windows,os_version:10,build:test_build1
        set window size  440  717
    
    
        ${auth}=    create list  <Username>  <password>
        create session  mysession  ${BaseURL}   auth=${auth}
        ${response}=      get request  mysession  /automate/builds/<build-id>/sessions.json?limit=1
        ${jsonresponse}=    set variable  ${response.json()}
    
        Log to console  ${jsonresponse}
    
        @{session_id_data}=  get value from json  ${jsonresponse}  [0].automation_session.hashed_id
    
        ${session_id}=  get from list  ${session_id_data}  0
    
        Log to console  ${session_id}
    
        Set Global Variable      ${session_id}
    
        wait until element is visible  //div[contains(text(),'Table')]
    
        ${txt}    Get Text    xpath=//div[contains(text(),'Table')]
        ${res}    Should Be Equal As Strings    ${txt}    ${ver_txt1}
    
         Run keyword if   '${txt}' == '${ver_txt1}'   Test keyword 1     ELSE  Test keyword 2
    
         sleep  3s
    
    
    *** Keywords ***
    
    Test keyword 1
    
            Log to console  Success!!!!
    
                ${body}=    create dictionary  status=Pass  reason=Validation Passed
                ${header}=  create dictionary  Content-Type=application/json
                ${response}=    PUT On Session  mysession   /automate/sessions/${session_id}.json  data=${body}  headers=${header}
    
                 ${jsonresponse}=    set variable  ${response.json()}
    
                 ${code}=  convert to string  ${response.status_code}
                 should be equal  ${code}  200
    
                 Log to console  ${jsonresponse}
    
    Test keyword 2
    
            Log to console  Test Failed...
    
    
                ${body}=    create dictionary  status=Fail  reason=Validation Failed
                ${header}=  create dictionary  Content-Type=application/json
                ${response}=    PUT On Session  mysession   /automate/sessions/${session_id}.json  data=${body}  headers=${header}
    
                 ${jsonresponse}=    set variable  ${response.json()}
    
                 ${code}=  convert to string  ${response.status_code}
                 should be equal  ${code}  200
    
                 Log to console  ${jsonresponse}
4

1 回答 1

0

Browserstack 提供了一项功能,允许您在测试终止之前标记会话状态。

看看https://www.browserstack.com/docs/automate/selenium/set-name-and-status-of-test#setting-the-test-status

我不确定从机器人 fw 做这件事的正确方法是什么,但这样的事情应该可以完成

Execute JavaScript 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"<passed/failed>", "reason": "<reason>"}}'

本质上,您希望利用Execute JavaScript上述格式向 Bstack 发送 JSON。

于 2021-11-14T09:10:37.950 回答