From 723c5a4e90c922d3acefdc3e0d8dd69210d53f32 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Tue, 18 Aug 2020 19:02:54 -0700 Subject: [PATCH] add visual example code --- .../Visual/CSharpWebdriverTest.csproj | 17 ++++ SauceExamples/Visual/CSharpWebdriverTest.sln | 17 ++++ SauceExamples/Visual/README.md | 41 ++++++++ SauceExamples/Visual/UnitTest1.cs | 99 +++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 SauceExamples/Visual/CSharpWebdriverTest.csproj create mode 100644 SauceExamples/Visual/CSharpWebdriverTest.sln create mode 100644 SauceExamples/Visual/README.md create mode 100644 SauceExamples/Visual/UnitTest1.cs diff --git a/SauceExamples/Visual/CSharpWebdriverTest.csproj b/SauceExamples/Visual/CSharpWebdriverTest.csproj new file mode 100644 index 00000000..a9443f64 --- /dev/null +++ b/SauceExamples/Visual/CSharpWebdriverTest.csproj @@ -0,0 +1,17 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + \ No newline at end of file diff --git a/SauceExamples/Visual/CSharpWebdriverTest.sln b/SauceExamples/Visual/CSharpWebdriverTest.sln new file mode 100644 index 00000000..b10ec42f --- /dev/null +++ b/SauceExamples/Visual/CSharpWebdriverTest.sln @@ -0,0 +1,17 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpWebdriverTest", "CSharpWebdriverTest.csproj", "{57ADB0BF-1786-4678-8152-73B12FA88CDC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {57ADB0BF-1786-4678-8152-73B12FA88CDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57ADB0BF-1786-4678-8152-73B12FA88CDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57ADB0BF-1786-4678-8152-73B12FA88CDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {57ADB0BF-1786-4678-8152-73B12FA88CDC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/SauceExamples/Visual/README.md b/SauceExamples/Visual/README.md new file mode 100644 index 00000000..44ce39f8 --- /dev/null +++ b/SauceExamples/Visual/README.md @@ -0,0 +1,41 @@ +# visual-testing-worker-tests + +## Table of Contents + +- [About](#about) +- [Getting Started](#getting_started) +- [Usage](#usage) + +## About + +These are the webdriver tests using C#. + +## Getting Started + +These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. + +### Prerequisites + +#### Using VS Studio + +* Create a new NUnit test project + + + +* Add all the necessary environment variables + ``` + setx SELENIUM_PROTOCOL "http" + setx SELENIUM_HOST "staging-hub.screener.io" + setx SELENIUM_PORT "80" + setx SAUCE_USERNAME + setx SAUCE_ACCESS_KEY + setx SCREENER_API_KEY + ``` +* Update the default test with the unit test: UnitTest1.cs + +## Usage + +From the folder containing the project run: +``` +dotnet test +``` diff --git a/SauceExamples/Visual/UnitTest1.cs b/SauceExamples/Visual/UnitTest1.cs new file mode 100644 index 00000000..79af1aa0 --- /dev/null +++ b/SauceExamples/Visual/UnitTest1.cs @@ -0,0 +1,99 @@ +using NUnit.Framework; +using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Remote; +using System; +using System.Collections.Generic; + +namespace WebDriver_CSharp_Example +{ + [TestFixture] + public class VisualE2ETest + { + RemoteWebDriver driver; + public string sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME"); + public string sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY"); + public string screenerApiKey = Environment.GetEnvironmentVariable("SCREENER_API_KEY"); + public string seleniumProtocol = Environment.GetEnvironmentVariable("SELENIUM_PROTOCOL"); + public string seleniumHost = Environment.GetEnvironmentVariable("SELENIUM_HOST"); + public string seleniumPort = Environment.GetEnvironmentVariable("SELENIUM_PORT"); + + [Test(Description = "Visual E2E Test against screener.io")] + public void testVisualE2E() + { + driver.Navigate().GoToUrl("https://screener.io"); + driver.ExecuteScript("/*@visual.init*/", "My Visual C# Test"); + driver.ExecuteScript("/*@visual.snapshot*/", "Home"); + var response = driver.ExecuteScript("/*@visual.end*/") as Dictionary; + Assert.IsTrue((Boolean)response["passed"]); + Assert.IsNotEmpty((String)response["message"]); + } + + [TearDown] + public void TearDownTest() + { + driver.Quit(); + } + + + [SetUp] + public void SetupTest() + { + + if (String.IsNullOrEmpty(sauceUserName)) + { + throw new Exception("SAUCE_USERNAME environment variable needs to be defined"); + } + + if (String.IsNullOrEmpty(sauceAccessKey)) + { + throw new Exception("SAUCE_ACCESS_KEY environment variable needs to be defined"); + } + + if (String.IsNullOrEmpty(screenerApiKey)) + { + throw new Exception("SCREENER_API_KEY environment variable needs to be defined"); + } + + if (String.IsNullOrEmpty(seleniumProtocol)) + { + throw new Exception("SELENIUM_PROTOCOL environment variable needs to be defined"); + } + + if (String.IsNullOrEmpty(seleniumHost)) + { + throw new Exception("SELENIUM_HOST environment variable needs to be defined"); + } + + if (String.IsNullOrEmpty(seleniumPort)) + { + throw new Exception("SELENIUM_PORT environment variable needs to be defined"); + } + + var chromeOptions = new ChromeOptions() + { + BrowserVersion = "latest", + PlatformName = "Windows 10", + UseSpecCompliantProtocol = true + }; + + var sauceOptions = new Dictionary + { + { "username", sauceUserName }, + { "accesskey", sauceAccessKey }, + { "name", TestContext.CurrentContext.Test.Name } + }; + + var visualOptions = new Dictionary + { + { "apiKey",screenerApiKey }, + { "projectName", "visual-e2e-test" }, + { "viewportSize", "1280x1024" } + }; + + chromeOptions.AddAdditionalCapability("sauce:options", sauceOptions, true); + chromeOptions.AddAdditionalCapability("sauce:visual", visualOptions, true); + + driver = new RemoteWebDriver(new Uri(String.Format("{0}://{1}:{2}/wd/hub", seleniumProtocol, seleniumHost, seleniumPort)), chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(600)); + } + } +}