Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Add Attachment To Result using SpecFlow

Trying to Send an attachment after a failed case, but I am unsure what I am doing incorrectly. The response I receive back is always:

Message: Gurock.TestRail.APIException : TestRail API returned HTTP 400 (“No file attached or upload size was exceeded.”)

Here’s an example (C#) I am running with. Everything seems to upload correctly except the image attachment, so any feedback would be greatly appreciated.

 public void TestRailsReporting()
        {
            // Grab the first tag in each scenario as the TestRails TestCase Number
            var testCaseID = ScenarioContext.Current.ScenarioInfo.Tags.GetValue(0).ToString();

            // TestRail API Setup
            APIClient client = new APIClient("https://xxxxx.testrail.io/");

            client.User = "xxxx@xxxxx.com";
            client.Password = "xxxxxxx";

            // TestRail Post Request Body
            Dictionary<string, object> testResult = new Dictionary<string, object>();
            if (null != ScenarioContext.Current.TestError)
            {
                // Take a Screenshot
                Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
                string title = ScenarioContext.Current.StepContext.StepInfo.Text;
                string screenshotfilename = "C:\\screenshots\\the.jpg";
                screenShot.SaveAsFile(screenshotfilename);

                testResult["status_id"] = "5"; // failed
                testResult["comment"] = ScenarioContext.Current.CurrentScenarioBlock.ToString()
                                      + " "
                                      + ScenarioContext.Current.StepContext.StepInfo.Text
                                      + "\n\n SpecFlow Test **Failed** \n\n"
                                      + ScenarioContext.Current.TestError.ToString();
                JObject c = (JObject)client.SendPost("add_attachment_to_result/" + testCaseID, "C:\\screenshots\\the.jpg");


            }
            else
            {
                testResult["status_id"] = "1"; // passed
                testResult["comment"] = ScenarioContext.Current.CurrentScenarioBlock.ToString()
                                      + " "
                                      + ScenarioContext.Current.StepContext.StepInfo.Text
                                      + "\n\n SpecFlow Test Definition **Passed**";
            }

            // Send Post Request
            JObject r = (JObject)client.SendPost("add_result/" + testCaseID, testResult);
        }