We have a usecase where we are testing the Offline Mode. Is there any way to achieve this without modifying the existing capabilities?
Yes , you can achieve the same by using LambdaTest’s Mobile REST API endpoint to update the network conditions of an active mobile automation session.
Code snippet in C# is below:
string userName = Environment.GetEnvironmentVariable("LT_USERNAME") == null ?"LT_USERNAME" : Environment.GetEnvironmentVariable("LT_USERNAME");
string accessKey = Environment.GetEnvironmentVariable("LT_ACCESS_KEY") == null ?
"LT_ACCESS_KEY" : Environment.GetEnvironmentVariable("LT_ACCESS_KEY");
var authToken = Convert.ToBase64String(
Encoding.ASCII.GetBytes($"{userName}:{accessKey}")
);
/* Get session id */
string sessionId = _driver.SessionId.ToString();
/****** Setting offline mode *****/
using (var client = new HttpClient())
{
var url = "https://mobile-api.lambdatest.com/mobile-automation/api/v1/sessions/" + sessionId + "/update_network";
var json = "{\"mode\": \"offline\"}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", authToken);
var response = client.PostAsync(url, content).Result;
Console.WriteLine(response);
}
/****** Setting online mode *****/
using (var client = new HttpClient())
{
var url = "https://mobile-api.lambdatest.com/mobile-automation/api/v1/sessions/" + sessionId + "/update_network";
var json = "{\"mode\": \"online\"}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", authToken);
var response = client.PostAsync(url, content).Result;
Console.WriteLine(response);
}
Sample execution snapshot is below:
Hope this helped, feel free to reach us out when stuck.
