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

add visual example code #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions SauceExamples/Visual/CSharpWebdriverTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="system" Version="4.1.0311.2" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions SauceExamples/Visual/CSharpWebdriverTest.sln
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions SauceExamples/Visual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# visual-testing-worker-tests

## Table of Contents

- [About](#about)
- [Getting Started](#getting_started)
- [Usage](#usage)

## About <a name = "about"></a>

These are the webdriver tests using C#.

## Getting Started <a name = "getting_started"></a>

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

<img src="assets/nunit.png"/>

* Add all the necessary environment variables
```
setx SELENIUM_PROTOCOL "http"

Choose a reason for hiding this comment

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

As mentioned in another comment, let's simplify and remove the SELENIUM_PROTOCOL, SELENIUM_HOST, and SELENIUM_PORT environment variables

setx SELENIUM_HOST "staging-hub.screener.io"
setx SELENIUM_PORT "80"
setx SAUCE_USERNAME <YOUR_SAUCE_USERNAME>
setx SAUCE_ACCESS_KEY <YOUR_SAUCE_ACCESS_KEY>
setx SCREENER_API_KEY <YOUR_SCREENER_API_KEY>
```
* Update the default test with the unit test: UnitTest1.cs

## Usage <a name = "usage"></a>

From the folder containing the project run:
```
dotnet test
```
99 changes: 99 additions & 0 deletions SauceExamples/Visual/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -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");

Choose a reason for hiding this comment

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

As mentioned in another comment, let's simplify and remove the SELENIUM_PROTOCOL, SELENIUM_HOST, and SELENIUM_PORT environment variables and checks in this file

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<string, object>;
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<string, object>
{
{ "username", sauceUserName },
{ "accesskey", sauceAccessKey },
{ "name", TestContext.CurrentContext.Test.Name }
};

var visualOptions = new Dictionary<string, object>
{
{ "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));

Choose a reason for hiding this comment

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

Looking at this again, I think there's too many environment variables making it potentially hard to read for an end user.
Can you simplify to:
new Uri("https://hub.screener.io:443/wd/hub")

}
}
}