Skip to content

Commit 9ca5658

Browse files
Added AcmeBankTest
1 parent a57c715 commit 9ca5658

File tree

3 files changed

+148
-5
lines changed

3 files changed

+148
-5
lines changed
Lines changed: 147 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,158 @@
1+
using Applitools;
2+
using Applitools.Selenium;
3+
using Applitools.VisualGrid;
4+
using NUnit.Framework;
5+
using OpenQA.Selenium;
6+
using OpenQA.Selenium.Chrome;
7+
using System.Drawing;
8+
using System.Runtime;
9+
using ScreenOrientation = Applitools.VisualGrid.ScreenOrientation;
10+
111
namespace Applitools.Example.Tests;
212

3-
public class Tests
13+
public class AcmeBankTest
414
{
15+
// Test control inputs to read once and share for all tests
16+
private static string? ApplitoolsApiKey;
17+
private static bool Headless;
18+
19+
// Applitools objects to share for all tests
20+
private static BatchInfo Batch;
21+
private static Configuration Config;
22+
private static VisualGridRunner Runner;
23+
24+
// Test-specific objects
25+
private WebDriver Driver;
26+
private Eyes Eyes;
27+
28+
[OneTimeSetUp]
29+
public static void SetUpConfigAndRunner()
30+
{
31+
// This method sets up the configuration for running visual tests in the Ultrafast Grid.
32+
// The configuration is shared by all tests in a test suite, so it belongs in a `OneTimeSetUp` method.
33+
// If you have more than one test class, then you should abstract this configuration to avoid duplication.
34+
35+
// Read the Applitools API key from an environment variable.
36+
ApplitoolsApiKey = Environment.GetEnvironmentVariable("APPLITOOLS_API_KEY");
37+
38+
// Read the headless mode setting from an environment variable.
39+
// Use headless mode for Continuous Integration (CI) execution.
40+
// Use headed mode for local development.
41+
Headless = Environment.GetEnvironmentVariable("HEADLESS")?.ToLower() == "true";
42+
43+
// Create the runner for the Ultrafast Grid.
44+
// Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
45+
// Warning: If you have a free account, then concurrency will be limited to 1.
46+
Runner = new VisualGridRunner(new RunnerOptions().TestConcurrency(5));
47+
48+
// Create a new batch for tests.
49+
// A batch is the collection of visual checkpoints for a test suite.
50+
// Batches are displayed in the dashboard, so use meaningful names.
51+
Batch = new BatchInfo("Example: Selenium C# NUnit with the Ultrafast Grid");
52+
53+
// Create a configuration for Applitools Eyes.
54+
Config = new Configuration();
55+
56+
// Set the Applitools API key so test results are uploaded to your account.
57+
// If you don't explicitly set the API key with this call,
58+
// then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
59+
Config.SetApiKey(ApplitoolsApiKey);
60+
61+
// Set the batch for the config.
62+
Config.SetBatch(Batch);
63+
64+
// Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
65+
// Other browsers are also available, like Edge and IE.
66+
Config.AddBrowser(800, 600, BrowserType.CHROME);
67+
Config.AddBrowser(1600, 1200, BrowserType.FIREFOX);
68+
Config.AddBrowser(1024, 768, BrowserType.SAFARI);
69+
70+
// Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
71+
// Other mobile devices are available, including iOS.
72+
Config.AddDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.Portrait);
73+
Config.AddDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.Landscape);
74+
}
75+
576
[SetUp]
6-
public void Setup()
77+
public void OpenBrowserAndEyes()
778
{
79+
// This method sets up each test with its own ChromeDriver and Applitools Eyes objects.
80+
81+
// Open the browser with the ChromeDriver instance.
82+
// Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
83+
// it still needs to run the test one time locally to capture snapshots.
84+
ChromeOptions options = new ChromeOptions();
85+
if (Headless) options.AddArgument("headless");
86+
Driver = new ChromeDriver(options);
87+
88+
// Set an implicit wait of 10 seconds.
89+
// For larger projects, use explicit waits for better control.
90+
// https://www.selenium.dev/documentation/webdriver/waits/
91+
// The following call works for Selenium 4:
92+
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
93+
94+
// Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
95+
Eyes = new Eyes(Runner);
96+
Eyes.SetConfiguration(Config);
97+
98+
// Open Eyes to start visual testing.
99+
// It is a recommended practice to set all four inputs:
100+
string testName = NUnit.Framework.TestContext.CurrentContext.Test.Name;
101+
Eyes.Open(
102+
Driver, // WebDriver object to "watch"
103+
"ACME Bank Web App", // The name of the app under test
104+
testName, // The name of the test case
105+
new Size(1024, 768)); // The viewport size for the local browser
8106
}
9107

10108
[Test]
11-
public void Test1()
109+
public void LogIntoBankAccount()
12110
{
13-
Assert.Pass();
111+
// This test covers login for the Applitools demo site, which is a dummy banking app.
112+
// The interactions use typical Selenium WebDriver calls,
113+
// but the verifications use one-line snapshot calls with Applitools Eyes.
114+
// If the page ever changes, then Applitools will detect the changes and highlight them in the dashboard.
115+
// Traditional assertions that scrape the page for text values are not needed here.
116+
117+
// Load the login page.
118+
Driver.Navigate().GoToUrl("https://demo.applitools.com");
119+
120+
// Verify the full login page loaded correctly.
121+
Eyes.Check(Target.Window().Fully().WithName("Login page"));
122+
123+
// Perform login.
124+
Driver.FindElement(By.Id("username")).SendKeys("applibot");
125+
Driver.FindElement(By.Id("password")).SendKeys("I<3VisualTests");
126+
Driver.FindElement(By.Id("log-in")).Click();
127+
128+
// Verify the full main page loaded correctly.
129+
// This snapshot uses LAYOUT match level to avoid differences in closing time text.
130+
Eyes.Check(Target.Window().Fully().WithName("Main page").Layout());
131+
}
132+
133+
[TearDown]
134+
public void CleanUpTest() {
135+
136+
// Quit the WebDriver instance.
137+
Driver.Quit();
138+
139+
// Close Eyes to tell the server it should display the results.
140+
Eyes.CloseAsync();
141+
142+
// Warning: `Eyes.CloseAsync()` will NOT wait for visual checkpoints to complete.
143+
// You will need to check the Applitools dashboard for visual results per checkpoint.
144+
// Note that "unresolved" and "failed" visual checkpoints will not cause the NUnit test to fail.
145+
146+
// If you want the NUnit test to wait synchronously for all checkpoints to complete, then use `eyes.close()`.
147+
// If any checkpoints are unresolved or failed, then `eyes.close()` will make the NUnit test fail.
148+
}
149+
150+
[OneTimeTearDown]
151+
public static void PrintResults() {
152+
153+
// Close the batch and report visual differences to the console.
154+
// Note that it forces NUnit to wait synchronously for all visual checkpoints to complete.
155+
TestResultsSummary allTestResults = Runner.GetAllTestResults();
156+
Console.WriteLine(allTestResults);
14157
}
15158
}

Applitools.Example.Tests/Applitools.Example.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="Eyes.Selenium4" Version="3.33.0" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
1314
<PackageReference Include="NUnit" Version="3.13.3" />
1415
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />

Applitools.Example.Tests/Usings.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)