-
Notifications
You must be signed in to change notification settings - Fork 11
/
acme-bank.test.js
205 lines (164 loc) · 8.91 KB
/
acme-bank.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
const { Builder, By } = require('selenium-webdriver');
const { Eyes,
ClassicRunner,
VisualGridRunner,
RunnerOptions,
Target,
RectangleSize,
Configuration,
BatchInfo,
BrowserType,
ScreenOrientation,
DeviceName } = require('@applitools/eyes-selenium');
describe('ACME Bank', () => {
// This Mocha test case class contains everything needed to run a full visual test against the ACME bank site.
// It runs the test once locally.
// If you use the Ultrafast Grid, then it performs cross-browser testing against multiple unique browsers.
// Settings to control how tests are run.
// These could be set by environment variables or other input mechanisms.
// They are hard-coded here to keep the example project simple.
const USE_ULTRAFAST_GRID = true;
const USE_EXECUTION_CLOUD = false;
// Test control inputs to read once and share for all tests
var applitoolsApiKey;
var headless;
// Applitools objects to share for all tests
let batch;
let config;
let runner;
// Test-specific objects
let driver;
let eyes;
before(async () => {
// This method sets up the configuration for running visual tests.
// The configuration is shared by all tests in a test suite, so it belongs in a `before` method.
// If you have more than one test class, then you should abstract this configuration to avoid duplication.
// Read the Applitools API key from an environment variable.
// To find your Applitools API key:
// https://applitools.com/tutorials/getting-started/setting-up-your-environment.html
applitoolsApiKey = process.env.APPLITOOLS_API_KEY;
// Read the headless mode setting from an environment variable.
// Use headless mode for Continuous Integration (CI) execution.
// Use headed mode for local development.
headless = process.env.HEADLESS? ['headless'] : []
if (USE_ULTRAFAST_GRID) {
// Create the runner for the Ultrafast Grid.
// Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
// Warning: If you have a free account, then concurrency will be limited to 1.
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
}
else {
// Create the classic runner.
runner = new ClassicRunner();
}
// Create a new batch for tests.
// A batch is the collection of visual checkpoints for a test suite.
// Batches are displayed in the Eyes Test Manager, so use meaningful names.
const runnerName = (USE_ULTRAFAST_GRID) ? 'Ultrafast Grid' : 'Classic runner';
batch = new BatchInfo(`Example: Selenium JavaScript Mocha with the ${runnerName}`);
// Create a configuration for Applitools Eyes.
config = new Configuration();
// Set the Applitools API key so test results are uploaded to your account.
// If you don't explicitly set the API key with this call,
// then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
config.setApiKey(applitoolsApiKey);
// Set the batch for the config.
config.setBatch(batch);
if (USE_ULTRAFAST_GRID) {
// Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
// Other browsers are also available, like Edge and IE.
config.addBrowser(800, 600, BrowserType.CHROME);
config.addBrowser(1600, 1200, BrowserType.FIREFOX);
config.addBrowser(1024, 768, BrowserType.SAFARI);
// Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
// Other mobile devices are available, including iOS.
config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);
config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE);
}
});
beforeEach(async function() {
//This method sets up each test with its own ChromeDriver and Applitools Eyes objects.
// Open the browser with the ChromeDriver instance.
// Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
// it still needs to run the test one time locally to capture snapshots.
var capabilities = {
browserName: 'chrome',
'goog:chromeOptions': {
args: headless,
},
};
if (USE_EXECUTION_CLOUD) {
// Open the browser remotely in the Execution Cloud.
let url = await Eyes.getExecutionCloudUrl();
driver = new Builder().usingServer(url).withCapabilities(capabilities).build();
}
else {
// Create a local WebDriver.
driver = new Builder().withCapabilities(capabilities).build();
}
// Set an implicit wait of 10 seconds.
// For larger projects, use explicit waits for better control.
// https://www.selenium.dev/documentation/webdriver/waits/
// The following call works for Selenium 4:
await driver.manage().setTimeouts( { implicit: 10000 } );
// If you are using Selenium 3, use the following call instead:
// await driver.manage().timeouts().implicitlyWait(10000);
// Create the Applitools Eyes object connected to the runner and set its configuration.
eyes = new Eyes(runner);
eyes.setConfiguration(config);
// Open Eyes to start visual testing.
// It is a recommended practice to set all four inputs:
await eyes.open(
// WebDriver object to "watch".
driver,
// The name of the application under test.
// All tests for the same app should share the same app name.
// Set this name wisely: Applitools features rely on a shared app name across tests.
'ACME Bank',
// The name of the test case for the given application.
// Additional unique characteristics of the test may also be specified as part of the test name,
// such as localization information ("Home Page - EN") or different user permissions ("Login by admin").
this.currentTest.fullTitle(),
// The viewport size for the local browser.
// Eyes will resize the web browser to match the requested viewport size.
// This parameter is optional but encouraged in order to produce consistent results.
new RectangleSize(1200, 600)
);
})
it('should log into a bank account', async () => {
// This test covers login for the Applitools demo site, which is a dummy banking app.
// The interactions use typical Selenium calls,
// but the verifications use one-line snapshot calls with Applitools Eyes.
// If the page ever changes, then Applitools will detect the changes and highlight them in the Eyes Test Manager.
// Traditional assertions that scrape the page for text values are not needed here.
// Load the login page.
await driver.get("https://demo.applitools.com");
// Verify the full login page loaded correctly.
await eyes.check(Target.window().fully().withName("Login page"));
// Perform login.
await driver.findElement(By.css("#username")).sendKeys("andy");
await driver.findElement(By.css("#password")).sendKeys("i<3pandas");
await driver.findElement(By.id("log-in")).click();
// Verify the full main page loaded correctly.
// This snapshot uses LAYOUT match level to avoid differences in closing time text.
await eyes.check(Target.window().fully().withName("Main page").layout());
});
afterEach(async function() {
// Close Eyes to tell the server it should display the results.
await eyes.closeAsync();
// Quit the WebDriver instance.
await driver.quit();
// Warning: `eyes.closeAsync()` will NOT wait for visual checkpoints to complete.
// You will need to check the Eyes Test Manager for visual results per checkpoint.
// Note that "unresolved" and "failed" visual checkpoints will not cause the Mocha test to fail.
// If you want the ACME demo app test to wait synchronously for all checkpoints to complete, then use `eyes.close()`.
// If any checkpoints are unresolved or failed, then `eyes.close()` will make the ACME demo app test fail.
});
after(async () => {
// Close the batch and report visual differences to the console.
// Note that it forces Mocha to wait synchronously for all visual checkpoints to complete.
const allTestResults = await runner.getAllTestResults();
console.log(allTestResults);
});
})