Browserstack restSharp cURL PUT request conversion

578 views Asked by At

I am trying to convert the following cURL command to C# using restSharp so that I can mark my automated Browserstack tests passed or failed.

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

Please note I am very new to C# I have the following code that currently returns an empty json response, I know I am on the right path as changing the request method to POST returns details (as expected) for my session/test:

    private string markTestPassedorFail(string sesID)

    {
        var Client = new RestClient();
        var Request = new RestRequest();
        string sResponse = "";  
        Client.BaseUrl = new Uri(CapConf.BROWSERSTACK_SESSIONS_URL);
        Client.Authenticator = new HttpBasicAuthenticator(CapConf.BROWSERSTACK_USER_NAME, CapConf.BROWSERSTACK_KEY_PASS);
        Request.Resource = sesID + ".json";
        Request.Method = Method.PUT;
        Request.AddHeader("Content-Type", "application/json");
        Request.AddJsonBody("{\"status\":\"failed\", \"reason\":\"failed\"}");
        try
        {

            IRestResponse response = Client.Execute(Request);
            sResponse = response.Content;

        }
        catch (Exception ex)
        {
            Console.WriteLine("Error Marking Test Passed or Fail : \n" +  ex.Message); 
        }

        return sResponse;
    }
1

There are 1 answers

0
Mukesh Tiwari On

Have you tried the sample code snippet shared in their documentation here - https://www.browserstack.com/automate/c-sharp

I just pulled up bits of the code snippet there and was able to setup a sample test run, fetch the session ID and later update the session status via REST API.

  1. Sample test - https://www.browserstack.com/automate/c-sharp#getting-started
  2. Session ID - https://www.browserstack.com/automate/c-sharp#session-id
  3. Session status update via REST API - https://www.browserstack.com/automate/c-sharp#rest-api

Refer to the following gist: https://gist.github.com/ashwingonsalves/56d7724671054bf623081bdcb30d40b8