How to use Browserstack Automate API for mark test status in ROBOT Framework PyCharm

690 views Asked by At

I tried with CURL given in BrowserStack for mark tests as fail/pass. I have implemented the automation code with the robot framework in pycharm. I added the test status for each test case in my test file.

Used curl

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 Console Says

HTTPError: 400 Client Error: Bad Request for url: https://api.browserstack.com/automate/sessions/.json

Previously it showed a 422 Unprocessable Entity error.

My robot fw code is shown below

*** 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}
1

There are 1 answers

0
dravit On

Browserstack provides a feature that allows you to mark the session status even before the test is terminated.

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

I am not sure what will be the right way to do it from robot fw but something like this should do the job

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

Essentially you want to leverage Execute JavaScript to send Bstack a JSON in the above format.