11using Applitools ;
22using Applitools . Selenium ;
33using Applitools . VisualGrid ;
4+ using NUnit . Framework ;
45using OpenQA . Selenium ;
56using OpenQA . Selenium . Chrome ;
67using System ;
1011
1112namespace ApplitoolsTutorial
1213{
14+ [ TestFixture ]
1315 public class UFGDemo
1416 {
15-
16- public static void Main ( string [ ] args )
17- {
18- // Create a new chrome web driver
19- IWebDriver webDriver = new ChromeDriver ( ) ;
20-
21- // Create a runner with concurrency of 1
22- VisualGridRunner runner = new VisualGridRunner ( new RunnerOptions ( ) . TestConcurrency ( 1 ) ) ;
23-
17+ private EyesRunner runner ;
18+ private Eyes eyes ;
19+ private IWebDriver driver ;
20+
21+ [ SetUp ]
22+ public void BeforeEach ( ) {
23+ var CI = Environment . GetEnvironmentVariable ( "CI" ) ;
24+ var options = new ChromeOptions ( ) ;
25+ if ( CI != null ) {
26+ options . AddArguments ( "headless" ) ;
27+ }
28+ // Use Chrome browser
29+ driver = new ChromeDriver ( options ) ;
30+
31+ //Initialize the Runner for your test with concurrency of 5.
2432 // Create Eyes object with the runner, meaning it'll be a Visual Grid eyes.
25- Eyes eyes = new Eyes ( runner ) ;
26-
27- SetUp ( eyes ) ;
33+ runner = new VisualGridRunner ( new RunnerOptions ( ) . TestConcurrency ( 5 ) ) ;
2834
29- try
30- {
31- // ⭐️ Note to see visual bugs, run the test using the above URL for the 1st run.
32- // but then change the above URL to https://demo.applitools.com/index_v2.html
33- // (for the 2nd run)
34- UltraFastTest ( webDriver , eyes ) ;
35-
36- }
37- finally
38- {
39- TearDown ( webDriver , runner ) ;
40- }
41-
42- }
43-
44- public static void SetUp ( Eyes eyes )
45- {
35+ // Initialize the eyes SDK (IMPORTANT: make sure your API key is set in the APPLITOOLS_API_KEY env variable).
36+ eyes = new Eyes ( runner ) ;
4637
4738 // Initialize eyes Configuration
4839 Configuration config = new Configuration ( ) ;
4940
5041 // You can get your api key from the Applitools dashboard
51- config . SetApiKey ( "APPLITOOLS_API_KEY" ) ;
42+ config . SetApiKey ( Environment . GetEnvironmentVariable ( "APPLITOOLS_API_KEY" ) ) ;
5243
5344 // create a new batch info instance and set it to the configuration
5445 config . SetBatch ( new BatchInfo ( "Ultrafast Batch" ) ) ;
@@ -69,47 +60,44 @@ public static void SetUp(Eyes eyes)
6960
7061 }
7162
72- public static void UltraFastTest ( IWebDriver webDriver , Eyes eyes )
73- {
74-
75- try
76- {
77-
78- // Navigate to the url we want to test
79- webDriver . Url = "https://demo.applitools.com" ;
63+ [ Test ]
64+ public void UFGTest ( ) {
65+ // Start the test by setting AUT's name, window or the page name that's being tested, viewport width and height
66+ eyes . Open ( driver , "Demo App - csharp ufg" , "Smoke Test" , new Size ( 800 , 600 ) ) ;
8067
81- // Call Open on eyes to initialize a test session
82- eyes . Open ( webDriver , "Demo App - csharp" , "Ultrafast grid demo" , new Size ( 800 , 600 ) ) ;
68+ // Navigate the browser to the "ACME" demo app. To see visual bugs after the first run, use the commented line below instead.
69+ driver . Url = "https://demo.applitools.com/" ;
70+ //driver.Url = "https://demo.applitools.com/index_v2.html";
8371
84- // check the login page with fluent api, see more info here
85- // https://applitools.com/docs/topics/sdk/the-eyes-sdk-check-fluent-api.html
86- eyes . Check ( Target . Window ( ) . Fully ( ) . WithName ( "Login page " ) ) ;
72+ // Visual checkpoint #1 - Check the login page. using the fluent API
73+ // https://applitools.com/docs/topics/sdk/the-eyes-sdk-check-fluent-api.html?Highlight=fluent%20api
74+ eyes . Check ( Target . Window ( ) . Fully ( ) . WithName ( "Login Window " ) ) ;
8775
88- webDriver . FindElement ( By . Id ( "log-in" ) ) . Click ( ) ;
76+ // This will create a test with two test steps.
77+ driver . FindElement ( By . Id ( "log-in" ) ) . Click ( ) ;
78+
79+ // Visual checkpoint #2 - Check the app page.
80+ eyes . Check ( Target . Window ( ) . Fully ( ) . WithName ( "App Window" ) ) ;
8981
90- // Check the app page
91- eyes . Check ( Target . Window ( ) . Fully ( ) . WithName ( "App page" ) ) ;
92-
93- // Call Close on eyes to let the server know it should display the results
94- eyes . CloseAsync ( ) ;
82+ // End the test.
83+ eyes . CloseAsync ( ) ;
84+ }
9585
96- }
97- catch ( Exception e )
98- {
99- eyes . AbortAsync ( ) ;
100- }
86+ [ TearDown ]
87+ public void AfrerEach ( ) {
88+ // Close the browser.
89+ driver . Quit ( ) ;
10190
102- }
91+ // If the test was aborted before eyes.close was called, ends the test as aborted.
92+ eyes . AbortIfNotClosed ( ) ;
10393
104- private static void TearDown ( IWebDriver webDriver , VisualGridRunner runner )
105- {
106- // Close the browser
107- webDriver . Quit ( ) ;
94+ //Wait and collect all test results
95+ // we pass false to this method to suppress the exception that is thrown if we
96+ // find visual differences
97+ TestResultsSummary allTestResults = runner . GetAllTestResults ( ) ;
10898
109- // we pass false to this method to suppress the exception that is thrown if we
110- // find visual differences
111- TestResultsSummary allTestResults = runner . GetAllTestResults ( ) ;
112- System . Console . WriteLine ( allTestResults ) ;
99+ // Print results
100+ Console . WriteLine ( allTestResults ) ;
113101 }
114102
115103 }
0 commit comments