Execute UI Tests in the Cloud- SauceLabs

WebDriver
7 Shares
Execute UI Tests SauceLabs

A while ago I started a new series of articles dedicated to the execution of UI tests in the cloud. The first publication was Execute UI Tests in the Cloud- Cross Browser Testing. The second one was dedicated to BrowserStack. Here I am going to present to you another cloud platform- SauceLabs. You can find even more useful information about the UI automated testing in my WebDriver Series.

Execute Tests in the Cloud- SauceLabs

How to Setup Your Tests?

1. Create a free registration at https://saucelabs.com/signup/trial

Create Free Account SauceLabs

2. Open the SauceLabs Documentation

3. Open the Instant Selenium C# Tests section

4. Copy the test initialize part

Copy TestInit Examples C# SauceLabs

5. Navigate to the Platform Configurator Page

6. Choose API = Selenium and select your preferred device- Windows or Mac

Configure OS SauceLabs

7. Select your preferred OS

Configure Exact OS SauceLabs

8. Select your preferred browser

Configure Browser SauceLabs

9. Configure Advanced Options such as resolution, video and screenshots recording

Configure Advanced Options SauceLabs

10. Copy the generated capabilities code

Copy Configuration Code SauceLabs

11. Copy your authentication key from the User Settings page

Copy Access Key SauceLabs

10. Create a new .NET library project

11. Install all required NuGet packages

<packages>
<package id="NUnit" version="3.7.1" targetFramework="net452" />
<package id="NUnit3TestAdapter" version="3.7.0" targetFramework="net452" />
<package id="Selenium.Chrome.WebDriver" version="2.29" targetFramework="net452" />
<package id="Selenium.Support" version="3.4.0" targetFramework="net452" />
<package id="Selenium.WebDriver" version="3.4.0" targetFramework="net452" />
</packages>

C# Setup Code Example

private string _username = "autoCloudTester";
private string _authkey = "70dccdcf-a9fd-4f55-aa07-12b051f6c83e";
private IWebDriver _driver;
[SetUp]
public void SetupTest()
{
var caps = new DesiredCapabilities();
caps.SetCapability("browserstack.debug", "true");
caps.SetCapability("build", "1.0");
caps.SetCapability("browserName", "Chrome");
caps.SetCapability("platform", "Windows 8.1");
caps.SetCapability("version", "49.0");
caps.SetCapability("screenResolution", "1280x800");
caps.SetCapability("username", _username);
caps.SetCapability("accessKey", _authkey);
caps.SetCapability("name", TestContext.CurrentContext.Test.Name);
_driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"), caps, TimeSpan.FromSeconds(180));
_driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
}
[TearDown]
public void TeardownTest()
{
var passed = TestContext.CurrentContext.Result.Outcome == ResultState.Success;
try
{
// Logs the result to Sauce Labs
((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
}
finally
{
_driver.Quit();
}
}

In the previous points, we have copied the user name and the access key we use them as values with the username and accesskey keys. Also, you have copied the other key value pairs we set them as well. By default the video and screenshots recording options are turned-on. If you want to disable them, you need to set recordScreenshots and recordVideo keys to false. For more comprehensive documentation about the test configuration options, you can visit this page.

Tests Examples

[Test]
public void ScrollFocusToControl_InCloud_ShouldFail()
{
_driver.Navigate().GoToUrl(@"https://automatetheplanet.com/compelling-sunday-14022016/");
var link = _driver.FindElement(By.PartialLinkText("Previous post"));
var jsToBeExecuted = $"window.scroll(0, {link.Location.Y});";
((IJavaScriptExecutor)_driver).ExecuteScript(jsToBeExecuted);
link.Click();
Assert.AreEqual("10 Advanced WebDriver Tips and Tricks - Part 1", _driver.Title);
}
[Test]
public void ScrollFocusToControl_InCloud_ShouldPass()
{
_driver.Navigate().GoToUrl(@"https://automatetheplanet.com/multiple-files-page-objects-item-templates/");
var link = _driver.FindElement(By.PartialLinkText("TFS Test API"));
var jsToBeExecuted = $"window.scroll(0, {link.Location.Y});";
((IJavaScriptExecutor)_driver).ExecuteScript(jsToBeExecuted);
var wait = new WebDriverWait(_driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.PartialLinkText("TFS Test API")));
clickableElement.Click();
Assert.AreEqual("TFS Test API Archives - Automate The Planet", _driver.Title);
}

These tests are absolutely the same as the one that we created for the CrossBrowserTesting platform.

SauceLabs- Cloud Execution- Features

On the first page of your SauceLabs dashboard, you can view all past/executing test runs.

Dashboard SauceLabs

You can watch the video recording of your test- the Watch tab.

Watch Tab Automated Test SauceLabs

Another feature of the platform is the so-called Commands logs, where you can see all executed commands followed by screenshots capturing the moments after the commands' completion.

Commands Logs SauceLabs

As in the other platforms, you can see the raw requests.

Complete Logs SauceLabs