This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBTAPI.cs
91 lines (82 loc) · 3.41 KB
/
CBTAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using OpenQA.Selenium;
using RestSharp;
using RestSharp.Authenticators;
namespace SeleniumTest
{
public class CBTAPI
{
public string apiBaseUrl = "https://crossbrowsertesting.com/api/v3";
private RestClient client;
private string username;
private string authkey;
public CBTAPI(string username, string authkey)
{
this.client = new RestClient(apiBaseUrl);
this.username = username;
this.authkey = authkey;
this.client.Authenticator = new HttpBasicAuthenticator(this.username, this.authkey);
}
public ICapabilities GetCapabilitiesFromBrowserName(string browserName, string browserVersion, string platformName, Dictionary<string, object> optionalCapabilities = null)
{
optionalCapabilities = optionalCapabilities ?? new Dictionary<string, object>();
DriverOptions browserOptions;
switch (browserName)
{
case "firefox":
browserOptions = new OpenQA.Selenium.Firefox.FirefoxOptions();
break;
case "safari":
browserOptions = new OpenQA.Selenium.Safari.SafariOptions();
break;
case "internet explorer":
browserOptions = new OpenQA.Selenium.IE.InternetExplorerOptions();
break;
case "microsoft edge":
browserOptions = new OpenQA.Selenium.Edge.EdgeOptions();
break;
default:
browserOptions = new OpenQA.Selenium.Chrome.ChromeOptions();
break;
}
browserOptions.BrowserVersion = browserVersion;
browserOptions.PlatformName = platformName;
var cloudOptions = new Dictionary<string, object>();
foreach (var optionalCapability in optionalCapabilities)
{
cloudOptions.Add(optionalCapability.Key, optionalCapability.Value );
}
cloudOptions.Add("username", this.username);
cloudOptions.Add("password", this.authkey);
browserOptions.AddAdditionalOption("cbt:options", cloudOptions);
return browserOptions.ToCapabilities();
}
private void makeRequest(string urlPath, RestSharp.Method requestMethod, string optionalJsonData = null)
{
var request = new RestRequest(urlPath, requestMethod, DataFormat.Json);
if (optionalJsonData != null)
{
request.AddJsonBody(optionalJsonData);
}
var res = client.Execute(request);
Console.WriteLine(res.Content.ToString());
}
public void SetScore(string sessionId, string score)
{
Dictionary<string, string> putDataDict = new Dictionary<string, string>();
putDataDict.Add("action", "set_score");
putDataDict.Add("score", score);
string putData = JsonConvert.SerializeObject(putDataDict);
makeRequest("/selenium/" + sessionId, RestSharp.Method.PUT, putData);
}
public void TakeSnapshot(string sessionId)
{
makeRequest("/selenium/" + sessionId + "/snapshots", RestSharp.Method.POST);
}
}
}