Skip to content

Commit

Permalink
Dotnet env var tests (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-kerem authored Jun 13, 2024
1 parent 93f05b1 commit 9db1380
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/dotnet-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ jobs:
run: dotnet build .\SauceLabs.Visual\SauceLabs.Visual.csproj --no-restore
- name: Test
run: dotnet test --verbosity normal

integration-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push to local registry
uses: docker/build-push-action@v5
with:
context: '{{defaultContext}}:visual-dotnet'
tags: saucelabs/visual-dotnet
file: Dockerfile
load: true
- name: Run the integration tests
run: |
npm ci
npm run test
working-directory: tests
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
CONTAINER_IMAGE_NAME: saucelabs/visual-dotnet
11 changes: 11 additions & 0 deletions visual-dotnet/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0

WORKDIR /app
COPY . .

RUN dotnet restore

ENV RUN_IT true

# run tests on docker run
ENTRYPOINT ["dotnet", "test", "--filter", "FullyQualifiedName~IntegrationTests"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;

namespace SauceLabs.Visual.IntegrationTests;

[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class IntegrationTestAttribute : NUnitAttribute, IApplyToTest
{
public void ApplyToTest(Test test)
{
if (test.RunState == RunState.NotRunnable)
{
return;
}

if (Environment.GetEnvironmentVariable("RUN_IT") != "true")
{
test.RunState = RunState.Ignored;
test.Properties.Set(PropertyNames.SkipReason, "This test runs only when RUN_IT is \"true\"");
}
}
}
42 changes: 42 additions & 0 deletions visual-dotnet/SauceLabs.Visual.IntegrationTests/LoginPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium.Remote;

namespace SauceLabs.Visual.IntegrationTests;

public class LoginPage
{
private RemoteWebDriver? Driver { get; set; }
private VisualClient? VisualClient { get; set; }

[SetUp]
public async Task Setup()
{
var browserOptions = Utils.GetBrowserOptions();
var sauceOptions = Utils.GetSauceOptions();
browserOptions.AddAdditionalOption("sauce:options", sauceOptions);

var sauceUrl = Utils.GetOnDemandURL();
Driver = new RemoteWebDriver(sauceUrl, browserOptions);
Driver.ExecuteScript("sauce:job-name=NUnit C#/.Net Visual Session");

VisualClient = await VisualClient.Create(Driver, Region.UsWest1);
TestContext.Progress.WriteLine($"Build: {VisualClient.Build.Url}");
}

[IntegrationTest]
[Test]
public async Task LoginPage_ShouldOpen()
{
Driver.Navigate().GoToUrl("https://www.saucedemo.com");
await VisualClient.VisualCheck("Login Page");
}

[TearDown]
public async Task Teardown()
{
await VisualClient.Finish();
Driver?.Quit();
VisualClient?.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="NUnit" Version="3.13.2"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0"/>
<PackageReference Include="Selenium.WebDriver" Version="4.18.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SauceLabs.Visual\SauceLabs.Visual.csproj" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions visual-dotnet/SauceLabs.Visual.IntegrationTests/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Safari;

namespace SauceLabs.Visual.IntegrationTests;

internal static class Utils
{
public static Dictionary<string, string> GetSauceOptions()
{
return new Dictionary<string, string>
{
{ "username", GetSauceUsername() },
{ "accessKey", GetSauceAccessKey() }
};
}

public static string GetSauceUsername()
{
var username = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
if (string.IsNullOrEmpty(username))
{
throw new Exception("No SAUCE_USERNAME found");
}

return username;
}

public static string GetSauceAccessKey()
{
var accessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");
if (string.IsNullOrEmpty(accessKey))
{
throw new Exception("No SAUCE_ACCESS_KEY found");
}

return accessKey;
}

public static string GetSauceRegion()
{
var region = Environment.GetEnvironmentVariable("SAUCE_REGION");
if (string.IsNullOrEmpty(region))
{
return "us-west-1";
}

return region;
}

public static Uri GetOnDemandURL()
{
var regionName = GetSauceRegion();
var tld = regionName == "staging" ? "net" : "com";
return new Uri("https://ondemand." + regionName + ".saucelabs." + tld + "/wd/hub");
}

public static DriverOptions GetBrowserOptions()
{
var browser = Environment.GetEnvironmentVariable("BROWSER_NAME");
DriverOptions browserOptions = browser switch
{
"Firefox" => new FirefoxOptions(),
"Safari" => new SafariOptions(),
_ => new ChromeOptions(),
};

browserOptions.PlatformName =
Environment.GetEnvironmentVariable("PLATFORM_NAME") ?? "Windows 11";
browserOptions.BrowserVersion =
Environment.GetEnvironmentVariable("BROWSER_VERSION") ?? "latest";
return browserOptions;
}
}
6 changes: 6 additions & 0 deletions visual-dotnet/SauceLabs.Visual.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SauceLabs.Visual", "SauceLa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SauceLabs.Visual.Tests", "SauceLabs.Visual.Tests\SauceLabs.Visual.Tests.csproj", "{6D07FB36-1E01-4D6A-853F-E1548DBE234C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SauceLabs.Visual.IntegrationTests", "SauceLabs.Visual.IntegrationTests\SauceLabs.Visual.IntegrationTests.csproj", "{77A9E41F-CBB4-4E52-A3CE-8131E59B379D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{6D07FB36-1E01-4D6A-853F-E1548DBE234C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D07FB36-1E01-4D6A-853F-E1548DBE234C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D07FB36-1E01-4D6A-853F-E1548DBE234C}.Release|Any CPU.Build.0 = Release|Any CPU
{77A9E41F-CBB4-4E52-A3CE-8131E59B379D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77A9E41F-CBB4-4E52-A3CE-8131E59B379D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77A9E41F-CBB4-4E52-A3CE-8131E59B379D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77A9E41F-CBB4-4E52-A3CE-8131E59B379D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 9db1380

Please sign in to comment.