有没有办法在运行时将 Browserstack 应用程序自动化的测试标记为失败?
Is there a way to mark tests as failed for Browserstack app automate during runtime?
我目前已将应用测试设置为使用 Browserstack。
我在 Visual Studio 2017 年使用 NUnit 框架编写了测试。这些测试 运行 成功并通过或失败,它们应该在 [=] 的测试运行器中标记为这样18=].
我遇到的唯一问题是,无论这些测试是通过还是失败,Browserstack 在查看 Browserstack App Automate 仪表板时都会简单地将它们列为已完成。
有没有办法在 Visual Studio 中的测试完成后立即将这些测试标记为通过或失败,因为将这些测试标记为通过或失败的唯一其他方法是使用 Browserstacks Rest API测试已经结束后运行ning
您可以使用下面的 REST API 调用标记测试 passed/failed。您甚至可以设置失败状态的原因
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
例如,如果您尝试将测试会话 ID ABC123 标记为测试失败,则 cURL 将如下所示:
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
如果您希望在 运行 时间执行此操作,您可以在 运行 时间使用下面提到的代码片段(在 Java 中)调用上面的 API .如果您使用其他语言,请将上述 API 调用移植到您选择的任何语言:
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:your_key@api.browserstack.com/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);
}
参考:https://www.browserstack.com/automate/java#rest-api
会话 ID 可以在 运行 时使用以下方法获取:
((RemoteWebDriver)driver).getSessionId().toString();
以防万一其他人遇到同样的问题,下面是我用于在 C# 运行时将测试标记为失败的代码:
var sessionid = Driver.SessionId.ToString();
string reqString = "{\"status\":\"failed\", \"reason\":\"Incorrect Text Found\"}";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
Uri myUri = new Uri(string.Format($"https://api-cloud.browserstack.com/app-automate/sessions/{sessionid}.json"));
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
myWebRequest.ContentType = "application/json";
myWebRequest.Method = "PUT";
myWebRequest.ContentLength = requestData.Length;
using (Stream st = myWebRequest.GetRequestStream()) st.Write(requestData, 0, requestData.Length);
NetworkCredential myNetworkCredential = new NetworkCredential("<username>", "<AccessKey>");
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;
myWebRequest.GetResponse().Close();
我目前已将应用测试设置为使用 Browserstack。
我在 Visual Studio 2017 年使用 NUnit 框架编写了测试。这些测试 运行 成功并通过或失败,它们应该在 [=] 的测试运行器中标记为这样18=].
我遇到的唯一问题是,无论这些测试是通过还是失败,Browserstack 在查看 Browserstack App Automate 仪表板时都会简单地将它们列为已完成。
有没有办法在 Visual Studio 中的测试完成后立即将这些测试标记为通过或失败,因为将这些测试标记为通过或失败的唯一其他方法是使用 Browserstacks Rest API测试已经结束后运行ning
您可以使用下面的 REST API 调用标记测试 passed/failed。您甚至可以设置失败状态的原因
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
例如,如果您尝试将测试会话 ID ABC123 标记为测试失败,则 cURL 将如下所示:
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
如果您希望在 运行 时间执行此操作,您可以在 运行 时间使用下面提到的代码片段(在 Java 中)调用上面的 API .如果您使用其他语言,请将上述 API 调用移植到您选择的任何语言:
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:your_key@api.browserstack.com/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);
}
参考:https://www.browserstack.com/automate/java#rest-api
会话 ID 可以在 运行 时使用以下方法获取:
((RemoteWebDriver)driver).getSessionId().toString();
以防万一其他人遇到同样的问题,下面是我用于在 C# 运行时将测试标记为失败的代码:
var sessionid = Driver.SessionId.ToString();
string reqString = "{\"status\":\"failed\", \"reason\":\"Incorrect Text Found\"}";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
Uri myUri = new Uri(string.Format($"https://api-cloud.browserstack.com/app-automate/sessions/{sessionid}.json"));
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
myWebRequest.ContentType = "application/json";
myWebRequest.Method = "PUT";
myWebRequest.ContentLength = requestData.Length;
using (Stream st = myWebRequest.GetRequestStream()) st.Write(requestData, 0, requestData.Length);
NetworkCredential myNetworkCredential = new NetworkCredential("<username>", "<AccessKey>");
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;
myWebRequest.GetResponse().Close();