Browserstack restSharp cURL PUT请求转换

时间:2016-12-14 12:06:03

标签: c# curl restsharp browserstack

我正在尝试使用restSharp将以下cURL命令转换为C# 这样我就可以标记我的自动Browserstack测试通过或失败。

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

请注意我是C#的新手我有以下代码,当前返回一个空的json响应,我知道我在正确的路径上将请求方法更改为POST返回我的会话/测试的详细信息(如预期) :

    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 个答案:

答案 0 :(得分:0)

您是否尝试过在其文档中共享的示例代码段 - https://www.browserstack.com/automate/c-sharp

我刚刚在那里提取了一些代码片段,并且能够设置示例测试运行,获取会话ID,然后通过REST API更新会话状态。

  1. 样品测试 - https://www.browserstack.com/automate/c-sharp#getting-started
  2. 会话ID - https://www.browserstack.com/automate/c-sharp#session-id
  3. 通过REST API更新会话状态 - https://www.browserstack.com/automate/c-sharp#rest-api
  4. 请参阅以下要点: https://gist.github.com/ashwingonsalves/56d7724671054bf623081bdcb30d40b8