I currently have app tests set up to use Browserstack.
I have written the tests using the NUnit Framework in Visual Studio 2017. These tests run successfully and Pass or Fail as they should and these are marked as such in the Test Runner in Visual Studio.
The only issue I have is that regardless of whether these tests pass or fail, Browserstack simply lists them down as completed when looking at the Browserstack App Automate Dashboard.
Is there a way to mark these tests as Passed or Failed as soon as the tests have completed in Visual Studio as the only other way to mark these tests Passed Or Failed is to use Browserstacks Rest API after the test has already finished running
You can mark the test passed/failed using the below REST API call. You can even set the reason for the failure status
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
For example, if you are trying to mark test failure for a test session id ABC123, then the cURL would look like:
curl -u "your_Username:your_Key" -X PUT -H "Content-Type: application/json" -d "{\"status\":\"failed\", \"reason\":\"Marking test failed via rest api\"}" https://api-cloud.browserstack.com/app-automate/sessions/ABC123.json
If you are looking to do this at runtime, you can call the above API using code snippet mentioned below(in Java) during runtime. If you are using a different language, port the above API call to any language of your choice:
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
public static void mark() throws URISyntaxException, UnsupportedEncodingException, IOException {
URI uri = new URI("https://your_username:[email protected]/app-automate/sessions/<session-id>.json");
HttpPut putRequest = new HttpPut(uri);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add((new BasicNameValuePair("status", "failed")));
nameValuePairs.add((new BasicNameValuePair("reason", "failure_reason")));
putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClientBuilder.create().build().execute(putRequest);
}
Reference: https://www.browserstack.com/automate/java#rest-api
session ID can be fetched at run time using:
((RemoteWebDriver)driver).getSessionId().toString();