2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.execute.setup;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.util.Arrays;
28 import java.util.UUID;
29 import java.util.logging.FileHandler;
30 import java.util.logging.Handler;
31 import java.util.logging.LogManager;
32 import java.util.logging.Logger;
34 import org.json.simple.JSONObject;
35 import org.littleshoot.proxy.impl.ClientToProxyConnection;
36 import org.littleshoot.proxy.impl.ProxyToServerConnection;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestManager;
39 import org.openecomp.sdc.ci.tests.api.SomeInterface;
40 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
41 import org.openecomp.sdc.ci.tests.datatypes.UserCredentials;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
43 import org.openecomp.sdc.ci.tests.execute.sanity.OnboardingFlowsUI;
44 import org.openecomp.sdc.ci.tests.execute.setup.ExtentManager.suiteNameXml;
45 import org.openecomp.sdc.ci.tests.pages.HomePage;
46 import org.openecomp.sdc.ci.tests.run.StartTest;
47 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
48 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
49 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
50 import org.openecomp.sdc.ci.tests.utils.rest.AutomationUtils;
51 import org.openqa.selenium.By;
52 import org.openqa.selenium.JavascriptExecutor;
53 import org.openqa.selenium.WebDriver;
54 import org.openqa.selenium.WebElement;
55 import org.openqa.selenium.support.ui.ExpectedConditions;
56 import org.openqa.selenium.support.ui.WebDriverWait;
57 import org.slf4j.LoggerFactory;
58 import org.testng.Assert;
59 import org.testng.ITestContext;
60 import org.testng.ITestResult;
61 import org.testng.annotations.AfterMethod;
62 import org.testng.annotations.AfterSuite;
63 import org.testng.annotations.BeforeMethod;
64 import org.testng.annotations.BeforeSuite;
65 import org.testng.annotations.Optional;
66 import org.testng.annotations.Parameters;
67 import org.testng.annotations.Test;
69 import com.aventstack.extentreports.ExtentReports;
70 import com.aventstack.extentreports.ExtentTest;
71 import com.aventstack.extentreports.Status;
73 import ch.qos.logback.classic.Level;
74 import ch.qos.logback.classic.LoggerContext;
75 import groovyjarjarantlr.Utils;
76 import net.lightbody.bmp.BrowserMobProxyServer;
77 import net.lightbody.bmp.core.har.Har;
79 public abstract class SetupCDTest extends DriverFactory {
81 // private static final String RE_RUN = "ReRun - ";
82 private static final String RE_RUN = "<html><font color=\"red\">ReRun - </font></html>";
83 private static final String WEB_SEAL_PASSWORD = "123123a";
85 public SetupCDTest() {
86 LoggerContext lc = (LoggerContext) LoggerFactory. getILoggerFactory();
87 lc.getLogger("org.apache").setLevel(Level.INFO);
90 /**************** CONSTANTS ****************/
91 private static final String CREDENTIALS_FILE = "credentials.yaml";
92 private static final String REPORT_FILE_NAME = "SDC_UI_Extent_Report.html";
93 protected static final String REPORT_FOLDER = "." + File.separator + "ExtentReport" + File.separator;
94 protected static final String SCREENSHOT_FOLDER = REPORT_FOLDER + "screenshots" + File.separator;
95 protected static final String HAR_FILES_FOLDER_NAME = "har_files";
96 protected static final String HAR_FILES_FOLDER = REPORT_FOLDER + HAR_FILES_FOLDER_NAME + File.separator;
99 private static final String SHORT_CSV_REPORT_FILE_NAME = "ShortReport.csv";
100 private static final int NUM_OF_ATTEMPTS_TO_REFTRESH = 2;
103 /**************** PRIVATES ****************/
104 private static String url;
105 private static boolean uiSimulator;
106 private static boolean localEnv = true;
107 private static OnboardCSVReport csvReport;
108 private static Map<?, ?> credentials;
110 protected static ITestContext myContext;
113 /**************** METHODS ****************/
114 public static ExtentTest getExtendTest() {
115 SomeInterface testManager = new ExtentTestManager();
116 return testManager.getTest();
118 public static WindowTest getWindowTest(){
119 return WindowTestManager.getWindowMap();
122 public OnboardCSVReport getCsvReport() {
126 public static String getReportFolder() {
127 return REPORT_FOLDER;
130 public static String getScreenshotFolder() {
131 return SCREENSHOT_FOLDER;
134 public static String getHarFilesFolder() {
135 return HAR_FILES_FOLDER;
139 protected abstract UserRoleEnum getRole();
141 /**************** BEFORE ****************/
143 @BeforeSuite(alwaysRun = true)
144 public void setupBeforeSuite(ITestContext context) throws Exception {
145 RestCDUtils.deleteOnDemand();
147 setErrorConfigurationFile();
149 ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
150 csvReport = new OnboardCSVReport(getReportFolder(), SHORT_CSV_REPORT_FILE_NAME);
153 private static void setErrorConfigurationFile() {
154 if (!System.getProperty("os.name").contains("Windows")){
155 String errorConfigurationFilename = getConfig().getErrorConfigurationFile();
156 errorConfigurationFilename = errorConfigurationFilename.substring(errorConfigurationFilename.lastIndexOf("/") + 1, errorConfigurationFilename.length());
157 getConfig().setErrorConfigurationFile(FileHandling.getBasePath() + File.separator + "conf" + File.separator + errorConfigurationFilename);
158 if (new File(getConfig().getErrorConfigurationFile()).exists()){
159 System.out.println("Found error-configuration.yaml in : " + getConfig().getErrorConfigurationFile());
164 @BeforeMethod(alwaysRun = true )
165 public void setBrowserBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
167 boolean emptyDataProvider = method.getAnnotation(Test.class).dataProvider().isEmpty();
168 String className = method.getDeclaringClass().getName();
169 if (emptyDataProvider && !className.contains("ToscaValidationTest") ) {
170 System.out.println("ExtentReport instance started from BeforeMethod...");
171 String suiteName = ExtentManager.getSuiteName(context);
172 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
173 ExtentTestManager.startTest(RE_RUN + method.getName());
175 ExtentTestManager.startTest(method.getName());
178 ExtentTestManager.assignCategory(this.getClass());
179 setBrowserBeforeTest(getRole());
181 System.out.println("ExtentReport instance started from Test...");
184 getConfig().setWindowsDownloadDirectory(getWindowTest().getDownloadDirectory());
186 if (getConfig().getCaptureTraffic()){
188 MobProxy.getPoxyServer().newHar(method.getName() + ".har");
189 } catch (Throwable e) {
195 /**************** AFTER ****************/
196 @AfterMethod(alwaysRun = true)
197 public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {
200 ReportAfterTestManager.report(result, context);
201 GeneralUIUtils.closeErrorMessage();
205 if (getConfig().getCaptureTraffic()){
206 addTrafficFileToReport(result);
209 if (result.getInstanceName().equals(OnboardingFlowsUI.class.getName()) && result.getStatus() == ITestResult.FAILURE){
210 System.out.println("Onboarding test failed, closign browser....");
211 getExtendTest().log(Status.INFO, "Onboarding test failed, closing browser....");
214 else if (!getUser().getRole().toLowerCase().equals(UserRoleEnum.ADMIN.name().toLowerCase())){
215 boolean navigateToHomePageSuccess = HomePage.navigateToHomePage();
216 if (!navigateToHomePageSuccess){
217 System.out.println("Navigating to homepage failed, reopening driver....");
218 getExtendTest().log(Status.INFO, "Navigating to homepage failed, reopening driver....");
223 } catch (Exception e) {
225 getExtendTest().log(Status.ERROR, "Exception:"+ e.toString());
230 ExtentTestManager.endTest();
231 String suiteName = ExtentManager.getSuiteName(context);
232 // write result to csv file
233 if((!suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) && (result.getStatus() == ITestResult.SKIP)) {
234 addResultToCSV(result, context);
236 if(suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue()) && !(result.getStatus() == ITestResult.SUCCESS)) {
237 addResultToCSV(result, context);
239 // ExtentManager.closeReporter();
240 FileHandling.cleanCurrentDownloadDir();
244 public void addResultToCSV(ITestResult result, ITestContext context) {
245 // String suiteName = ExtentManager.getSuiteName(context);
246 ExtentTest test = getExtendTest();
247 com.aventstack.extentreports.model.Test model = test.getModel();
248 String name = model.getName();
249 String status = model.getStatus().toString();
250 // if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue()) && !(result.getStatus() == ITestResult.SUCCESS)) {
251 getCsvReport().writeRow(result.getInstanceName(), name.replace(RE_RUN,""), status);
255 public void generateReport4Jenkins(ITestContext context) {
256 String suiteName = ExtentManager.getSuiteName(context);
257 // String outputDirectory = context.getOutputDirectory();
258 JSONObject obj = new JSONObject();
259 String success = Integer.toString(context.getPassedTests().size());
260 String failed = Integer.toString(context.getFailedTests().size());
261 String total = Integer.toString(context.getFailedTests().size()+context.getPassedTests().size());
262 obj.put("projectName", "SDC-ONAP-UI-Automation-"+suiteName);
263 obj.put("projectVersion", AutomationUtils.getOSVersion());
264 obj.put("platform", "Linux");
265 obj.put("total", total);
266 obj.put("success", success);
267 obj.put("failed", failed);
269 try (FileWriter file = new FileWriter(getReportFolder() + "jenkinsResults.json")) {
271 file.write(obj.toJSONString());
274 } catch (IOException e) {
278 System.out.print(obj);
284 @Parameters({ "eraseAfterTests" })
285 @AfterSuite(alwaysRun = true)
286 public void afterSuite2(@Optional("true") String eraseAfterTestsReadValue) throws Exception {
287 // public void afterSuite() throws Exception {
289 csvReport.closeFile();
290 generateReport4Jenkins(myContext);
292 if (Boolean.parseBoolean(eraseAfterTestsReadValue)){
293 RestCDUtils.deleteOnDemand();
295 System.out.println("Resources will not be deleted according to suite configuration ...");
298 if (getConfig().getUseBrowserMobProxy()){
299 MobProxy.getPoxyServer().stop();
303 protected static String setUrl() {
304 url = getConfig().getUrl();
305 uiSimulator = getConfig().isUiSimulator();
307 String message = "no URL found";
308 System.out.println(message);
309 Assert.fail(message);
310 } else if (!url.contains("localhost") && !url.contains("192.168.33.10") && !url.contains("127.0.0.1") && !url.contains("192.168.50.5") && !uiSimulator==true) {
316 public static void loadCredentialsFile() throws Exception {
317 if (credentials != null){
320 File credentialsFileRemote = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + CREDENTIALS_FILE);
321 // File credentialsFileLocal = new File(FileHandling.getConfFilesPath() + CREDENTIALS_FILE);
322 File credentialsFileLocal = new File(FileHandling.getSdcVnfsPath() + File.separator + "conf"
323 + File.separator + CREDENTIALS_FILE);
324 File[] credentialFiles = {credentialsFileRemote, credentialsFileLocal};
325 for (File credentialsFile : credentialFiles){
326 if (credentialsFile.exists()){
327 credentials = FileHandling.parseYamlFile(credentialsFile.getAbsolutePath());
333 private UserCredentials getUserCredentialsFromFile(String userRole) throws Exception {
334 @SuppressWarnings("unchecked")
335 Map<String, String> credentialsMap = (Map<String, String>) credentials.get(userRole);
336 String user = (String) credentialsMap.get("username");
337 String password = (String) credentialsMap.get("password");
338 String firstname = (String) credentialsMap.get("firstname");
339 String lastname = (String) credentialsMap.get("lastname");
341 return new UserCredentials(user, password, firstname, lastname, userRole);
345 public static void navigateToUrl(String url) throws Exception {
347 System.out.println("Deleting cookies...");
350 System.out.println("Navigating to URL : " + url);
351 getDriver().navigate().to(url);
352 GeneralUIUtils.waitForLoader();
354 System.out.println("Zooming out...");
355 GeneralUIUtils.windowZoomOutUltimate();
358 catch (Exception e) {
359 String msg = "Browser is unreachable";
360 System.out.println(msg);
361 getExtendTest().log(Status.ERROR, msg);
365 private static void deleteCookies() throws Exception {
366 getDriver().manage().deleteAllCookies();
370 final int max_attempts = 3;
372 while (!getDriver().manage().getCookies().isEmpty() && attempts < max_attempts){
373 getExtendTest().log(Status.INFO, "Trying to delete cookies one more time - " + (attempts + 1) + "/" + max_attempts + "attempts");
374 String deleteCookiesJS = "document.cookie.split(';').forEach(function(c) { document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); });";
375 ((JavascriptExecutor) getDriver()).executeScript(deleteCookiesJS);
378 if (attempts == max_attempts){
379 String msg = "Did not delete cookies, can't login as user " + WindowTestManager.getWindowMap().getUser().getRole();
380 System.out.println(msg);
381 getExtendTest().log(Status.ERROR, msg);
387 protected void loginToSystem(UserRoleEnum role) throws Exception {
388 UserCredentials credentials;
390 loginToSimulator(role);
391 credentials = new UserCredentials(role.getUserId(), WEB_SEAL_PASSWORD, role.getFirstName(), role.getLastName(), role.name());
394 credentials = getUserFromFileByRole(role);
395 sendUserAndPasswordKeys(credentials);
396 WebElement submitButton = GeneralUIUtils.getWebElementBy(By.name("btnSubmit"), 30);
397 submitButton.click();
398 WebElement buttonOK = GeneralUIUtils.getWebElementBy(By.name("successOK"), 30);
399 Assert.assertTrue(buttonOK.isDisplayed(), "OK button is not displayed.");
402 GeneralUIUtils.ultimateWait();
403 getWindowTest().setUser(credentials);
405 protected UserCredentials getUserFromFileByRole(UserRoleEnum role) throws Exception {
406 loadCredentialsFile();
407 return getUserCredentialsFromFile(role.name().toLowerCase());
409 private void goToHomePage(UserRoleEnum role) throws Exception {
411 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() == 0 ? NUM_OF_ATTEMPTS_TO_REFTRESH : getWindowTest().getRefreshAttempts());
412 if (!role.equals(UserRoleEnum.ADMIN)) {
414 WebElement closeButton = GeneralUIUtils.getClickableButtonBy(By.className("sdc-welcome-close"), 10);
415 if (closeButton != null){
419 if (!GeneralUIUtils.isElementVisibleByTestId(DataTestIdEnum.MainMenuButtons.HOME_BUTTON.getValue()))
421 restartBrowser(role);
425 catch (Exception e) {
426 restartBrowser(role);
429 private void restartBrowser(UserRoleEnum role) throws Exception {
430 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() - 1);
431 if (getWindowTest().getRefreshAttempts() <= 0) {
432 System.out.println("ERR : Something is wrong with browser!");
433 Assert.fail("ERR : Something is wrong with browser!");
435 System.out.println("Trying again...");
436 getExtendTest().log(Status.INFO, "Trying again...");
437 getExtendTest().log(Status.INFO, String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts() ));
438 System.out.println(String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts() ));
440 reloginWithNewRole(role);
443 public void loginToSimulator(UserRoleEnum role){
444 WebDriver driver = GeneralUIUtils.getDriver();
445 WebDriverWait wait = new WebDriverWait(driver, 30);
447 wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@method='" + "post" + "']"))));
449 WebElement userIdTextbox = GeneralUIUtils.getWebElementBy(By.name("userId"));
450 userIdTextbox.sendKeys(role.getUserId());
451 WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
452 passwordTextbox.sendKeys(WEB_SEAL_PASSWORD);
454 wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//*[@value='" + "Login" + "']")))).click();
457 private void sendUserAndPasswordKeys(UserCredentials userId) {
458 System.out.println("Login as user : " + userId.getUserId());
459 WebElement userNameTextbox = GeneralUIUtils.getWebElementBy(By.name("userid"));
460 userNameTextbox.sendKeys(userId.getUserId());
461 WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
462 passwordTextbox.sendKeys(userId.getPassword());
465 public void loginWithUser(UserRoleEnum role) {
467 getExtendTest().log(Status.INFO, String.format("Login as user %s", role.name().toUpperCase()));
470 } catch (Exception e) {
471 throw new RuntimeException(e);
474 getWindowTest().setPreviousRole(getWindowTest().getUser().getRole());
478 private void setUser(UserRoleEnum role) {
479 User user = new User();
480 user.setUserId(role.getUserId());
481 user.setFirstName(role.getFirstName());
482 user.setRole(role.name());
483 user.setLastName(role.getLastName());
485 getWindowTest().setUser(user);
488 public User getUser() {
489 return getWindowTest().getUser();
492 private void setBrowserBeforeTest(UserRoleEnum role) {
493 System.out.println(String.format("Setup before test as %s.", role.toString().toUpperCase() ));
495 System.out.println("Previous role is : " + getWindowTest().getPreviousRole() + " ; Current role is : " + role.name());
496 if (!getWindowTest().getPreviousRole().toLowerCase().equals(role.name().toLowerCase())){
497 System.out.println("Roles are different, navigate and login");
498 navigateAndLogin(role);
500 } catch (Exception e) {
505 public void navigateAndLogin(UserRoleEnum role) throws Exception {
506 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() != 0 ? getWindowTest().getRefreshAttempts() : 0);
510 GeneralUIUtils.ultimateWait();
513 public User getUser(UserRoleEnum role) {
514 User user = new User();
515 user.setUserId(role.getUserId());
516 user.setFirstName(role.getFirstName());
517 user.setLastName(role.getLastName());
518 user.setRole(role.name());
522 protected void reloginWithNewRole(UserRoleEnum role) throws Exception {
523 System.out.println(String.format("Setup before relogin as %s", role.toString().toUpperCase()));
524 navigateAndLogin(role);
527 /*public static void setLocalUrl(UserRoleEnum role) {
530 url = "http://localhost:8181/sdc1/proxy-admin1#/dashboard";
534 url = "http://localhost:8181/sdc1/proxy-designer1#/dashboard";
538 url = "http://localhost:8181/sdc1/proxy-governor1#/dashboard";
542 url = "http://localhost:8181/sdc1/proxy-ops1#/dashboard";
546 url = "http://localhost:8181/sdc1/proxy-tester1#/dashboard";
549 case PRODUCT_MANAGER1: {
550 url = "http://localhost:8181/sdc1/proxy-pm1#/dashboard";
553 case PRODUCT_MANAGER2: {
554 url = "http://localhost:8181/sdc1/proxy-pm2#/dashboard";
557 case PRODUCT_STRATEGIST1: {
558 url = "http://localhost:8181/sdc1/proxy-ps1#/dashboard";
561 case PRODUCT_STRATEGIST2: {
562 url = "http://localhost:8181/sdc1/proxy-ps2#/dashboard";
571 public void addTrafficFileToReport(ITestResult result) {
574 Har har = MobProxy.getPoxyServer().getHar();
575 String shortUUID = UUID.randomUUID().toString().split("-")[0];
576 File harFile = new File(getHarFilesFolder() + result.getName() + shortUUID + ".har");
577 new File(getHarFilesFolder()).mkdirs();
579 har.writeTo(harFile);
581 String pathToFileFromReportDirectory = HAR_FILES_FOLDER_NAME + File.separator + harFile.getName();
582 ExtentTestActions.addFileToReportAsLink(harFile, pathToFileFromReportDirectory, "File with captured traffic");
583 } catch (IOException ioe) {
584 ioe.printStackTrace();
589 * * Start section of test in ExtentReport with DataProvider parameters,
590 * should be started from test method, see example in onboardVNFTest
592 public void setLog(String fromDataProvider) {
594 String suiteName = ExtentManager.getSuiteName(myContext);
595 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
596 ExtentTestManager.startTest(RE_RUN +Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
598 ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
603 getWindowTest().setAddedValueFromDataProvider(fromDataProvider);
604 ExtentTestManager.assignCategory(this.getClass());
605 setBrowserBeforeTest(getRole());
610 /**************** MAIN ****************/
611 public static void main(String[] args) {
612 System.out.println("---------------------");
613 System.out.println("running test from CLI");
614 System.out.println("---------------------");
616 String attsdcFilePath = FileHandling.getBasePath() + File.separator + "conf" + File.separator + "attsdc.yaml";
617 System.setProperty("config.resource", attsdcFilePath);
618 System.out.println("attsdc.yaml file path is : " + attsdcFilePath);
620 String filepath = FileHandling.getBasePath() + File.separator + "Files" + File.separator;
621 System.setProperty("filePath", filepath);
622 System.out.println("filePath is : " + System.getProperty("filePath"));
624 Object[] testSuitsList = FileHandling.filterFileNamesFromFolder(FileHandling.getBasePath() + File.separator + "testSuites", ".xml");
625 if (testSuitsList != null) {
626 System.out.println(String.format("Found %s testSuite(s)", testSuitsList.length));
627 args = Arrays.copyOf(testSuitsList, testSuitsList.length, String[].class);
628 StartTest.main(args);