Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple sauce pseudocode #25

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions SauceExamples/SeleniumNunit/SimpleExamples/SimpleSauceTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Configuration;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
Expand All @@ -11,36 +12,32 @@ namespace Selenium.Nunit.Scripts.SimpleExamples
[Category("SimpleTest")]
public class SimpleSauceTest
{
IWebDriver _driver;
private IWebDriver _driver;
private SauceSession _sauce;

[Test]
public void SauceConnectTest()
public void DemoTest()
{
//TODO please supply your Sauce Labs user name in an environment variable
var sauceUserName = Environment.GetEnvironmentVariable(
"SAUCE_USERNAME", EnvironmentVariableTarget.User);
//TODO please supply your own Sauce Labs access Key in an environment variable
var sauceAccessKey = Environment.GetEnvironmentVariable(
"SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability(CapabilityType.Version, "latest", true);
options.AddAdditionalCapability(CapabilityType.Platform, "Windows 10", true);
options.AddAdditionalCapability("username", sauceUserName, true);
options.AddAdditionalCapability("accessKey", sauceAccessKey, true);
options.AddAdditionalCapability("name", TestContext.CurrentContext.Test.Name, true);
_sauce = new SauceSession
{
DataCenter = DataCenter.USEast, //TODO this will mean that it's headless
TestName = TestContext.CurrentContext.Test.Name
};
_driver = _sauce.Start();

_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"), options.ToCapabilities(),
TimeSpan.FromSeconds(600));
_driver.Navigate().GoToUrl("https://www.google.com");
Assert.Pass();
}

[TearDown]
public void CleanUpAfterEveryTestMethod()
{
var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
_driver?.Quit();
//TODO could also log a comment "Test finished execution"
//TODO will also log the error message if it failed. In the future can take the
//whole TestContext and parse out the relevant data
var isPassed = TestContext.CurrentContext.Result.Outcome.Status
== TestStatus.Passed;
_sauce.Stop(isPassed, TestContext.CurrentContext.Result.Message);
Copy link
Collaborator Author

@nadvolod nadvolod Dec 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially a security issue of getting the information from the client that might be sensitive TestContext.CurrentContext.Result.Message. Not Needed for Beta

}
}
}
32 changes: 16 additions & 16 deletions SauceExamples/Web.Tests/BestPractices/test/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@ public class BaseTest
private readonly string _browser;
private readonly string _browserVersion;
private readonly string _osPlatform;
public SauceJavaScriptExecutor SauceReporter;
private SauceLabsCapabilities SauceConfig { get; set; }
private SauceSession _sauce;

[SetUp]
public void ExecuteBeforeEveryTestMethod()
{
SauceConfig = new SauceLabsCapabilities
var options = new SauceOptions
{
IsDebuggingEnabled = bool.Parse(ConfigurationManager.AppSettings["isExtendedDebuggingEnabled"]),
IsHeadless = bool.Parse(ConfigurationManager.AppSettings["sauceHeadless"])
IsExtendedDebuggingEnabled = bool.Parse(ConfigurationManager.AppSettings["isExtendedDebuggingEnabled"]),
Browser = _browser,
BrowserVersion = _browserVersion,
OperatingSystem = _osPlatform
};
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);
_sauce = new SauceSession(options);
_sauce.DataCenter = DataCenter.USEast; //TODO this will mean that it's headless
Driver = _sauce.Start();
_sauce.TestName = TestContext.CurrentContext.Test.Name;
_sauce.BuildName = ConfigurationManager.AppSettings["buildName"]; //set by default, no need to explicitly state
}

[TearDown]
public void CleanUpAfterEveryTestMethod()
{
if (SauceConfig.IsUsingSauceLabs) ExecuteSauceCleanupSteps();
if (_sauce != null) 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);
//TODO could also log a comment "Test finished execution"
//TODO will also log the error message if it failed. In the future can take the
//whole TestContext and parse out the relevant data
_sauce.Stop(isPassed, TestContext.CurrentContext.Result.Message);
}

private void SetTestStatusUsingApi(bool isPassed)
Expand Down