Skip to content

Instantly share code, notes, and snippets.

@ashwingonsalves
Created December 15, 2016 16:18
Show Gist options
  • Save ashwingonsalves/56d7724671054bf623081bdcb30d40b8 to your computer and use it in GitHub Desktop.
Save ashwingonsalves/56d7724671054bf623081bdcb30d40b8 to your computer and use it in GitHub Desktop.
Sample script runs a Selenium test, captures the session ID and updates the session status on BrowserStack
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace SeleniumClientTry
{
public class CustomRemoteWebDriver : RemoteWebDriver
{
public CustomRemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities) : base(remoteAddress, desiredCapabilities)
{
}
public string getSessionID()
{
return base.SessionId.ToString();
}
}
class Program
{
static void Main(string[] args)
{
CustomRemoteWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.InternetExplorer();
capability.SetCapability("browserstack.user", "<USERNAME>");
capability.SetCapability("browserstack.key", "<ACCESS_KEY>");
capability.SetCapability("browserName", "internet explorer");
driver = new CustomRemoteWebDriver(
new Uri("http://hub.browserstack.com/wd/hub/"), capability
);
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Browserstack");
query.Submit();
Console.WriteLine(driver.Title);
//Works without using the subclass for client-side bindings <= 2.48.2
//var sessionIdProperty = typeof(RemoteWebDriver).GetProperty("SessionId", BindingFlags.Instance | BindingFlags.NonPublic);
//SessionId sessionId = sessionIdProperty.GetValue(driver, null) as SessionId;
string sessionId = driver.getSessionID();
Console.WriteLine(sessionId.ToString());
System.Threading.Thread.Sleep(10);
var url = string.Format("https://www.browserstack.com/automate/sessions/{0}.json", sessionId);
Uri myUri = new Uri(url);
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
NetworkCredential myNetworkCredential = new NetworkCredential("USERNAME", "ACCESS_KEY");
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream responseStream = myWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
Console.Write(pageContent);
responseStream.Close();
myWebResponse.Close();
System.Threading.Thread.Sleep(10);
driver.Quit();
System.Threading.Thread.Sleep(3);
string reqString = "{\"status\":\"Failed\", \"reason\":\"There was a Error!\"}";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
Uri myUri1 = new Uri(string.Format("https://www.browserstack.com/automate/sessions/"+sessionId+".json"));
WebRequest myWebRequest1 = HttpWebRequest.Create(myUri1);
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)myWebRequest1;
myWebRequest1.ContentType = "application/json";
myWebRequest1.Method = "PUT";
myWebRequest1.ContentLength = requestData.Length;
using (Stream st = myWebRequest1.GetRequestStream()) st.Write(requestData, 0, requestData.Length);
NetworkCredential myNetworkCredential1 = new NetworkCredential("USERNAME", "ACCESS_KEY");
CredentialCache myCredentialCache1 = new CredentialCache();
myCredentialCache1.Add(myUri, "Basic", myNetworkCredential1);
myHttpWebRequest1.PreAuthenticate = true;
myHttpWebRequest1.Credentials = myCredentialCache;
myWebRequest1.GetResponse().Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment