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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.sdc.frontend.ci.tests.execute.setup;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;
import net.lightbody.bmp.core.har.Har;
import org.json.simple.JSONObject;
import org.onap.sdc.backend.ci.tests.config.UserCredentialsFromFile;
import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum;
import org.onap.sdc.frontend.ci.tests.datatypes.UserCredentials;
import org.onap.sdc.frontend.ci.tests.execute.sanity.OnboardingFlowsUi;
import org.onap.sdc.frontend.ci.tests.pages.HomePage;
import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
import org.onap.sdc.frontend.ci.tests.utilities.GeneralUIUtils;
import org.onap.sdc.frontend.ci.tests.utilities.RestCDUtils;
import org.onap.sdc.backend.ci.tests.utils.rest.AutomationUtils;
import org.openecomp.sdc.be.model.User;
import org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public abstract class SetupCDTest extends DriverFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(SetupCDTest.class);
private static final String RE_RUN = "<html><font color=\"red\">ReRun - </font></html>";
private static final String WEB_SEAL_PASSWORD = "123123a";
protected static final String HEAT_FILE_YAML_NAME_PREFIX = "Heat-File";
protected static final String HEAT_FILE_YAML_NAME_SUFFIX = ".yaml";
private static final int BASIC_SLEEP_DURATION = 1000;
/**************** CONSTANTS ****************/
private static final String CREDENTIALS_FILE = "credentials.yaml";
private static final String REPORT_FILE_NAME = "SDC_UI_Extent_Report.html";
private static final String REPORT_FOLDER = "target" + File.separator + "ExtentReport" + File.separator + "UI" + File.separator;
private static final String SCREENSHOT_FOLDER = REPORT_FOLDER + "screenshots" + File.separator;
private static final String HAR_FILES_FOLDER_NAME = "har_files";
private static final String HAR_FILES_FOLDER = REPORT_FOLDER + HAR_FILES_FOLDER_NAME + File.separator;
private static final String SHORT_CSV_REPORT_FILE_NAME = "ShortReport.csv";
private static final int NUM_OF_ATTEMPTS_TO_REFTRESH = 2;
/**************** PRIVATES ****************/
private static String url;
private static boolean uiSimulator;
private static boolean localEnv = true;
private static OnboardCSVReport csvReport;
private final UserCredentialsFromFile credentialsIns = UserCredentialsFromFile.getInstance();
private static ITestContext myContext;
public SetupCDTest() {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.getLogger("org.apache").setLevel(Level.INFO);
}
public static ExtentTest getExtendTest() {
return ExtentTestManager.getInstance().getTest();
}
public static WindowTest getWindowTest() {
return WindowTestManager.getWindowMap();
}
private OnboardCSVReport getCsvReport() {
return csvReport;
}
public static String getReportFolder() {
return REPORT_FOLDER;
}
public static String getScreenshotFolder() {
return SCREENSHOT_FOLDER;
}
private static String getHarFilesFolder() {
return HAR_FILES_FOLDER;
}
protected UserRoleEnum getRole() {
return UserRoleEnum.DESIGNER;
}
/**************** BEFORE ****************/
@BeforeSuite(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) throws Exception {
RestCDUtils.deleteOnDemand();
myContext = context;
setUrl();
ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
csvReport = new OnboardCSVReport(getReportFolder(), SHORT_CSV_REPORT_FILE_NAME);
}
@BeforeMethod(alwaysRun = true)
public void setBrowserBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
boolean emptyDataProvider = method.getAnnotation(Test.class).dataProvider().isEmpty();
String className = method.getDeclaringClass().getName();
if (emptyDataProvider && !className.contains("ToscaValidationTest")) {
System.out.println("ExtentReport instance started from BeforeMethod...");
String suiteName = ExtentManager.getSuiteName(context);
if (suiteName.equals(ExtentManager.suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
ExtentTestManager.getInstance().startTest(RE_RUN + method.getName());
} else {
ExtentTestManager.getInstance().startTest(method.getName());
}
ExtentTestManager.getInstance().assignCategory(this.getClass());
setBrowserBeforeTest(getRole());
} else {
System.out.println("ExtentReport instance started from Test...");
}
if (getConfig().getCaptureTraffic()) {
try {
MobProxy.getPoxyServer().newHar(method.getName() + ".har");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/**************** AFTER ****************/
@AfterMethod(alwaysRun = true)
public void quitAfterTest(final ITestResult result, final ITestContext context) throws Exception {
try {
ReportAfterTestManager.report(result, context);
GeneralUIUtils.closeErrorMessage();
} finally {
try {
if (getConfig().getCaptureTraffic()) {
addTrafficFileToReport(result);
}
if (OnboardingFlowsUi.class.getName().equals(result.getInstanceName()) && result.getStatus() == ITestResult.FAILURE) {
final String msg = "Onboarding test failed, closing browser";
LOGGER.info(msg);
getExtendTest().log(Status.INFO, msg);
quitDriver();
} else if (!getUser().getRole().toLowerCase().equals(UserRoleEnum.ADMIN.name().toLowerCase())) {
boolean navigateToHomePageSuccess = HomePage.navigateToHomePage();
if (!navigateToHomePageSuccess) {
final String msg = "Navigating to homepage failed, reopening driver";
LOGGER.info(msg);
getExtendTest().log(Status.INFO, msg);
quitDriver();
}
}
} catch (final Exception e) {
LOGGER.error("An unexpected error has occurred", e);
getExtendTest().log(Status.ERROR, "Exception:" + e.toString());
}
ExtentTestManager.getInstance().endTest();
final String suiteName = ExtentManager.getSuiteName(context);
// write result to csv file
if ((!ExtentManager.suiteNameXml.TESTNG_FAILED_XML_NAME.getValue().equals(suiteName)) && (result.getStatus() == ITestResult.SKIP)) {
addResultToCSV(result, context);
}
if (ExtentManager.suiteNameXml.TESTNG_FAILED_XML_NAME.getValue().equals(suiteName) && !(result.getStatus() == ITestResult.SUCCESS)) {
addResultToCSV(result, context);
}
FileHandling.cleanCurrentDownloadDir();
}
}
private void addResultToCSV(ITestResult result, ITestContext context) {
ExtentTest test = getExtendTest();
com.aventstack.extentreports.model.Test model = test.getModel();
String name = model.getName();
String status = model.getStatus().toString();
getCsvReport().writeRow(result.getInstanceName(), name.replace(RE_RUN, ""), status);
}
private void generateReport4Jenkins(ITestContext context) {
final String suiteName = ExtentManager.getSuiteName(context);
final JSONObject obj = new JSONObject();
final String success = Integer.toString(context.getPassedTests().size());
final String failed = Integer.toString(context.getFailedTests().size());
final String total = Integer.toString(context.getFailedTests().size() + context.getPassedTests().size());
obj.put("projectName", "SDC-ONAP-UI-Automation-" + suiteName);
obj.put("projectVersion", AutomationUtils.getOSVersion());
obj.put("platform", "Linux");
obj.put("total", total);
obj.put("success", success);
obj.put("failed", failed);
try (FileWriter file = new FileWriter(getReportFolder() + "jenkinsResults.json")) {
file.write(obj.toJSONString());
file.flush();
} catch (final IOException e) {
LOGGER.debug("An error has occurred while writing 'jenkinsResults.json' file", e);
}
LOGGER.debug(obj.toJSONString());
}
@Parameters({"eraseAfterTests"})
@AfterSuite(alwaysRun = true)
public void afterSuite2(@Optional("true") String eraseAfterTestsReadValue) throws Exception {
csvReport.closeFile();
generateReport4Jenkins(myContext);
if (Boolean.parseBoolean(eraseAfterTestsReadValue)) {
RestCDUtils.deleteOnDemand();
} else {
System.out.println("Resources will not be deleted according to suite configuration ...");
}
if (getConfig().getUseBrowserMobProxy()) {
MobProxy.getPoxyServer().stop();
}
}
private static String setUrl() {
url = getConfig().getUrl();
uiSimulator = getConfig().isUiSimulator();
if (url == null) {
String message = "no URL found";
System.out.println(message);
Assert.fail(message);
} else if (!url.contains("localhost") && !url.contains("192.168.33.10") && !url.contains("127.0.0.1") && !url.contains("192.168.50.5") && !uiSimulator) {
localEnv = false;
}
return url;
}
private static void navigateToUrl(final String url) {
try {
LOGGER.info("Deleting cookies");
deleteCookies();
LOGGER.info("Navigating to URL : " + url);
getDriver().navigate().to(url);
GeneralUIUtils.waitForLoader();
LOGGER.info("Zooming out");
GeneralUIUtils.windowZoomOutUltimate();
} catch (Exception e) {
final String errorMsg = String.format("Could not navigate to '%s'", url);
LOGGER.error(errorMsg, e);
getExtendTest().log(Status.ERROR, errorMsg);
Assert.fail(errorMsg);
}
}
private static void deleteCookies() throws Exception {
getDriver().manage().deleteAllCookies();
Thread.sleep(BASIC_SLEEP_DURATION);
int attempts = 0;
final int max_attempts = 3;
while (!getDriver().manage().getCookies().isEmpty() && attempts < max_attempts) {
getExtendTest().log(Status.INFO, "Trying to delete cookies one more time - " + (attempts + 1) + "/" + max_attempts + "attempts");
String deleteCookiesJS = "document.cookie.split(';').forEach(function(c) { document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); });";
((JavascriptExecutor) getDriver()).executeScript(deleteCookiesJS);
attempts++;
if (attempts == max_attempts) {
String msg = "Did not delete cookies, can't login as user " + WindowTestManager.getWindowMap().getUser().getRole();
System.out.println(msg);
getExtendTest().log(Status.ERROR, msg);
Assert.fail(msg);
}
}
}
private void loginToSystem(UserRoleEnum role) throws Exception {
final int gettingWebElementTimeOut = 30;
UserCredentials credentials = new UserCredentials(role.getUserId(), role.getPassword(), role.getFirstName(), role.getLastName(), role.name());
if (localEnv) {
loginToSimulator(role);
} else {
sendUserAndPasswordKeys(credentials);
WebElement submitButton = GeneralUIUtils.getWebElementBy(By.name("btnSubmit"), gettingWebElementTimeOut);
submitButton.click();
WebElement buttonOK = GeneralUIUtils.getWebElementBy(By.name("successOK"), gettingWebElementTimeOut);
Assert.assertTrue(buttonOK.isDisplayed(), "OK button is not displayed.");
buttonOK.click();
}
GeneralUIUtils.ultimateWait();
getWindowTest().setUser(credentials);
}
private void goToHomePage(UserRoleEnum role) throws Exception {
final int gettingButtonTimeOut = 10;
try {
getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() == 0 ? NUM_OF_ATTEMPTS_TO_REFTRESH : getWindowTest().getRefreshAttempts());
if (!role.equals(UserRoleEnum.ADMIN)) {
WebElement closeButton = GeneralUIUtils.getClickableButtonBy(By.className("sdc-welcome-close"), gettingButtonTimeOut);
if (closeButton != null) {
closeButton.click();
}
if (!GeneralUIUtils.isElementVisibleByTestId(DataTestIdEnum.MainMenuButtons.HOME_BUTTON.getValue())) {
restartBrowser(role);
}
}
} catch (Exception e) {
restartBrowser(role);
}
}
private void restartBrowser(UserRoleEnum role) throws Exception {
getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() - 1);
if (getWindowTest().getRefreshAttempts() <= 0) {
System.out.println("ERR : Something is wrong with browser!");
Assert.fail("ERR : Something is wrong with browser!");
}
System.out.println("Trying again...");
getExtendTest().log(Status.INFO, "Trying again...");
getExtendTest().log(Status.INFO, String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts()));
System.out.println(String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts()));
reloginWithNewRole(role);
}
private void loginToSimulator(UserRoleEnum role) {
final int gettingWebElementTimeOut = 30;
WebDriver driver = GeneralUIUtils.getDriver();
WebDriverWait wait = new WebDriverWait(driver, gettingWebElementTimeOut);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@method='" + "post" + "']"))));
WebElement userIdTextbox = GeneralUIUtils.getWebElementBy(By.name("userId"));
userIdTextbox.sendKeys(role.getUserId());
WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
passwordTextbox.sendKeys(WEB_SEAL_PASSWORD);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//*[@value='" + "Login" + "']")))).click();
}
private void sendUserAndPasswordKeys(UserCredentials userId) {
System.out.println("Login as user : " + userId.getUserId());
WebElement userNameTextbox = GeneralUIUtils.getWebElementBy(By.name("userid"));
userNameTextbox.sendKeys(userId.getUserId());
WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
passwordTextbox.sendKeys(userId.getPassword());
}
private void loginWithUser(final UserRoleEnum role) {
try {
final String msg = String
.format("Login as user '%s', role '%s'", role.getUserId(), role.getUserRole());
getExtendTest().log(Status.INFO, msg);
LOGGER.info(msg);
loginToSystem(role);
LOGGER.debug("Going to home page");
goToHomePage(role);
} catch (final Exception e) {
throw new RuntimeException(e);
} finally {
getWindowTest().setPreviousRole(getWindowTest().getUser().getRole());
}
}
private void setUser(UserRoleEnum role) {
User user = new User();
user.setUserId(role.getUserId());
user.setFirstName(role.getFirstName());
user.setRole(role.name());
user.setLastName(role.getLastName());
getWindowTest().setUser(user);
}
public User getUser() {
return getWindowTest().getUser();
}
private void setBrowserBeforeTest(final UserRoleEnum role) {
LOGGER.info(String.format("Setup before test for role '%s'", role.name()));
if (!getWindowTest().getPreviousRole().equalsIgnoreCase(role.name())) {
LOGGER.info(String.format("Logging in with new role '%s'. Previous role was: '%s'.", role.name(),
getWindowTest().getPreviousRole()));
navigateAndLogin(role);
}
}
private void navigateAndLogin(final UserRoleEnum role) {
getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts());
setUser(role);
navigateToUrl(url);
loginWithUser(role);
GeneralUIUtils.ultimateWait();
}
public User getUser(UserRoleEnum role) {
User user = new User();
user.setUserId(role.getUserId());
user.setFirstName(role.getFirstName());
user.setLastName(role.getLastName());
user.setRole(role.name());
return user;
}
protected void reloginWithNewRole(final UserRoleEnum role) {
navigateAndLogin(role);
}
private void addTrafficFileToReport(ITestResult result) {
try {
// Get the HAR data
Har har = MobProxy.getPoxyServer().getHar();
String shortUUID = UUID.randomUUID().toString().split("-")[0];
File harFile = new File(getHarFilesFolder() + result.getName() + shortUUID + ".har");
new File(getHarFilesFolder()).mkdirs();
har.writeTo(harFile);
String pathToFileFromReportDirectory = HAR_FILES_FOLDER_NAME + File.separator + harFile.getName();
ExtentTestActions.addFileToReportAsLink(harFile, pathToFileFromReportDirectory, "File with captured traffic");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/*
* * Start section of test in ExtentReport with DataProvider parameters,
* should be started from test method, see example in onboardVNFTest
*/
public void setLog(String fromDataProvider) {
String suiteName = ExtentManager.getSuiteName(myContext);
if (ExtentManager.suiteNameXml.TESTNG_FAILED_XML_NAME.getValue().equals(suiteName)) {
ExtentTestManager.getInstance().startTest(RE_RUN + Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
} else {
ExtentTestManager.getInstance().startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
}
getWindowTest().setAddedValueFromDataProvider(fromDataProvider);
ExtentTestManager.getInstance().assignCategory(this.getClass());
setBrowserBeforeTest(getRole());
}
}
|