forked from nikolay-advolodkin/dot-net-sauce
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathBaseTest.cs
99 lines (87 loc) · 3.75 KB
/
BaseTest.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
92
93
94
95
96
97
98
99
using System;
using System.Configuration;
using Common;
using Common.SauceLabs;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using RestSharp;
using RestSharp.Authenticators;
namespace Selenium3.Nunit.Framework.BestPractices.test
{
//TODO this whole class is a duplication of BaseCrossBrowserTEst.cs
//The reason why it was duplicated was because I needed to be able to configure
//the [Setup] methods. Some needed to be able to set the Build Name and do some actions,
//other tests didn't need to set the build name, and others, only needed to set
//the build name. It seems as though maybe a Strategy pattern might solve these problems
//It might make sense to create a ISetupStrategy that is defined in the constructor
//of every single feature file. That feature file will define the setup Strategy.
//Then, those operations will be performed int the [Setup] of the BaseTest
[TestFixture]
[Category("AcceptanceTests")]
[Category("CrossBrowser")]
[Category("NUnit")]
[Category("BestPractices")]
[Category("NunitFramework")]
public class BaseTest
{
private readonly string _browser;
private readonly string _browserVersion;
private readonly string _osPlatform;
public SauceJavaScriptExecutor SauceReporter;
private SauceLabsCapabilities SauceConfig { get; set; }
[SetUp]
public void ExecuteBeforeEveryTestMethod()
{
SauceConfig = new SauceLabsCapabilities
{
IsDebuggingEnabled = bool.Parse(ConfigurationManager.AppSettings["isExtendedDebuggingEnabled"]),
IsHeadless = bool.Parse(ConfigurationManager.AppSettings["sauceHeadless"])
};
SauceLabsCapabilities.BuildName = ConfigurationManager.AppSettings["buildName"];
Driver = new WebDriverFactory(SauceConfig).CreateSauceDriver(_browser, _browserVersion, _osPlatform);
SauceReporter = new SauceJavaScriptExecutor(Driver);
SauceReporter.SetTestName(TestContext.CurrentContext.Test.Name);
SauceReporter.SetBuildName(SauceLabsCapabilities.BuildName);
}
[TearDown]
public void CleanUpAfterEveryTestMethod()
{
if (SauceConfig.IsUsingSauceLabs) ExecuteSauceCleanupSteps();
Driver?.Quit();
}
private void ExecuteSauceCleanupSteps()
{
var isPassed = TestContext.CurrentContext.Result.Outcome.Status
== TestStatus.Passed;
SauceReporter.LogTestStatus(isPassed);
//SetTestStatusUsingApi(isPassed);
SauceReporter.LogMessage("Test finished execution");
SauceReporter.LogMessage(TestContext.CurrentContext.Result.Message);
}
private void SetTestStatusUsingApi(bool isPassed)
{
var userName = SauceUser.Name;
var accessKey = SauceUser.AccessKey;
var sessionId = ((RemoteWebDriver)Driver).SessionId;
var client = new RestClient
{
Authenticator = new HttpBasicAuthenticator(userName, accessKey),
BaseUrl = new Uri(new SauceLabsEndpoint().HeadlessRestApiUrl)
};
var request = new RestRequest($"/{userName}/jobs/{sessionId}",
Method.PUT)
{ RequestFormat = DataFormat.Json };
request.AddJsonBody(new { passed = isPassed });
client.Execute(request);
}
public BaseTest(string browser, string browserVersion, string osPlatform)
{
_browser = browser;
_browserVersion = browserVersion;
_osPlatform = osPlatform;
}
public IWebDriver Driver { get; set; }
}
}