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 localEnv = true;
106 private static OnboardCSVReport csvReport;
107 private static Map<?, ?> credentials;
109 protected static ITestContext myContext;
112 /**************** METHODS ****************/
113 public static ExtentTest getExtendTest() {
114 SomeInterface testManager = new ExtentTestManager();
115 return testManager.getTest();
117 public static WindowTest getWindowTest(){
118 return WindowTestManager.getWindowMap();
121 public OnboardCSVReport getCsvReport() {
125 public static String getReportFolder() {
126 return REPORT_FOLDER;
129 public static String getScreenshotFolder() {
130 return SCREENSHOT_FOLDER;
133 public static String getHarFilesFolder() {
134 return HAR_FILES_FOLDER;
138 protected abstract UserRoleEnum getRole();
140 /**************** BEFORE ****************/
142 @BeforeSuite(alwaysRun = true)
143 public void setupBeforeSuite(ITestContext context) throws Exception {
144 RestCDUtils.deleteOnDemand();
146 setErrorConfigurationFile();
148 ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
149 csvReport = new OnboardCSVReport(getReportFolder(), SHORT_CSV_REPORT_FILE_NAME);
152 private static void setErrorConfigurationFile() {
153 if (!System.getProperty("os.name").contains("Windows")){
154 String errorConfigurationFilename = getConfig().getErrorConfigurationFile();
155 errorConfigurationFilename = errorConfigurationFilename.substring(errorConfigurationFilename.lastIndexOf("/") + 1, errorConfigurationFilename.length());
156 getConfig().setErrorConfigurationFile(FileHandling.getBasePath() + File.separator + "conf" + File.separator + errorConfigurationFilename);
157 if (new File(getConfig().getErrorConfigurationFile()).exists()){
158 System.out.println("Found error-configuration.yaml in : " + getConfig().getErrorConfigurationFile());
163 @BeforeMethod(alwaysRun = true )
164 public void setBrowserBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
166 boolean emptyDataProvider = method.getAnnotation(Test.class).dataProvider().isEmpty();
167 String className = method.getDeclaringClass().getName();
168 if (emptyDataProvider && !className.contains("ToscaValidationTest") ) {
169 System.out.println("ExtentReport instance started from BeforeMethod...");
170 String suiteName = ExtentManager.getSuiteName(context);
171 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
172 ExtentTestManager.startTest(RE_RUN + method.getName());
174 ExtentTestManager.startTest(method.getName());
177 ExtentTestManager.assignCategory(this.getClass());
178 setBrowserBeforeTest(getRole());
180 System.out.println("ExtentReport instance started from Test...");
183 getConfig().setWindowsDownloadDirectory(getWindowTest().getDownloadDirectory());
185 if (getConfig().getCaptureTraffic()){
187 MobProxy.getPoxyServer().newHar(method.getName() + ".har");
188 } catch (Throwable e) {
194 /**************** AFTER ****************/
195 @AfterMethod(alwaysRun = true)
196 public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {
199 ReportAfterTestManager.report(result, context);
200 GeneralUIUtils.closeErrorMessage();
204 if (getConfig().getCaptureTraffic()){
205 addTrafficFileToReport(result);
208 if (result.getInstanceName().equals(OnboardingFlowsUI.class.getName()) && result.getStatus() == ITestResult.FAILURE){
209 System.out.println("Onboarding test failed, closign browser....");
210 getExtendTest().log(Status.INFO, "Onboarding test failed, closing browser....");
213 else if (!getUser().getRole().toLowerCase().equals(UserRoleEnum.ADMIN.name().toLowerCase())){
214 boolean navigateToHomePageSuccess = HomePage.navigateToHomePage();
215 if (!navigateToHomePageSuccess){
216 System.out.println("Navigating to homepage failed, reopening driver....");
217 getExtendTest().log(Status.INFO, "Navigating to homepage failed, reopening driver....");
222 } catch (Exception e) {
224 getExtendTest().log(Status.ERROR, "Exception:"+ e.toString());
229 ExtentTestManager.endTest();
230 String suiteName = ExtentManager.getSuiteName(context);
231 // write result to csv file
232 if((!suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) && (result.getStatus() == ITestResult.SKIP)) {
233 addResultToCSV(result, context);
235 if(suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue()) && !(result.getStatus() == ITestResult.SUCCESS)) {
236 addResultToCSV(result, context);
238 // ExtentManager.closeReporter();
239 FileHandling.cleanCurrentDownloadDir();
243 public void addResultToCSV(ITestResult result, ITestContext context) {
244 // String suiteName = ExtentManager.getSuiteName(context);
245 ExtentTest test = getExtendTest();
246 com.aventstack.extentreports.model.Test model = test.getModel();
247 String name = model.getName();
248 String status = model.getStatus().toString();
249 // if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue()) && !(result.getStatus() == ITestResult.SUCCESS)) {
250 getCsvReport().writeRow(result.getInstanceName(), name.replace(RE_RUN,""), status);
254 public void generateReport4Jenkins(ITestContext context) {
255 String suiteName = ExtentManager.getSuiteName(context);
256 // String outputDirectory = context.getOutputDirectory();
257 JSONObject obj = new JSONObject();
258 String success = Integer.toString(context.getPassedTests().size());
259 String failed = Integer.toString(context.getFailedTests().size());
260 String total = Integer.toString(context.getFailedTests().size()+context.getPassedTests().size());
261 obj.put("projectName", "SDC-ONAP-UI-Automation-"+suiteName);
262 obj.put("projectVersion", AutomationUtils.getOSVersion());
263 obj.put("platform", "Linux");
264 obj.put("total", total);
265 obj.put("success", success);
266 obj.put("failed", failed);
268 try (FileWriter file = new FileWriter(getReportFolder() + "jenkinsResults.json")) {
270 file.write(obj.toJSONString());
273 } catch (IOException e) {
277 System.out.print(obj);
283 @Parameters({ "eraseAfterTests" })
284 @AfterSuite(alwaysRun = true)
285 public void afterSuite2(@Optional("true") String eraseAfterTestsReadValue) throws Exception {
286 // public void afterSuite() throws Exception {
288 csvReport.closeFile();
289 generateReport4Jenkins(myContext);
291 if (Boolean.parseBoolean(eraseAfterTestsReadValue)){
292 RestCDUtils.deleteOnDemand();
294 System.out.println("Resources will not be deleted according to suite configuration ...");
297 if (getConfig().getUseBrowserMobProxy()){
298 MobProxy.getPoxyServer().stop();
302 protected static String setUrl() {
303 url = getConfig().getUrl();
305 String message = "no URL found";
306 System.out.println(message);
307 Assert.fail(message);
308 } else if (!url.contains("localhost") && !url.contains("192.168.33.10") && !url.contains("127.0.0.1")) {
314 public static void loadCredentialsFile() throws Exception {
315 if (credentials != null){
318 File credentialsFileRemote = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + CREDENTIALS_FILE);
319 // File credentialsFileLocal = new File(FileHandling.getConfFilesPath() + CREDENTIALS_FILE);
320 File credentialsFileLocal = new File(FileHandling.getSdcVnfsPath() + File.separator + "conf"
321 + File.separator + CREDENTIALS_FILE);
322 File[] credentialFiles = {credentialsFileRemote, credentialsFileLocal};
323 for (File credentialsFile : credentialFiles){
324 if (credentialsFile.exists()){
325 credentials = FileHandling.parseYamlFile(credentialsFile.getAbsolutePath());
331 private UserCredentials getUserCredentialsFromFile(String userRole) throws Exception {
332 @SuppressWarnings("unchecked")
333 Map<String, String> credentialsMap = (Map<String, String>) credentials.get(userRole);
334 String user = (String) credentialsMap.get("username");
335 String password = (String) credentialsMap.get("password");
336 String firstname = (String) credentialsMap.get("firstname");
337 String lastname = (String) credentialsMap.get("lastname");
339 return new UserCredentials(user, password, firstname, lastname, userRole);
343 public static void navigateToUrl(String url) throws Exception {
345 System.out.println("Deleting cookies...");
348 System.out.println("Navigating to URL : " + url);
349 getDriver().navigate().to(url);
350 GeneralUIUtils.waitForLoader();
352 System.out.println("Zooming out...");
353 GeneralUIUtils.windowZoomOutUltimate();
356 catch (Exception e) {
357 String msg = "Browser is unreachable";
358 System.out.println(msg);
359 getExtendTest().log(Status.ERROR, msg);
363 private static void deleteCookies() throws Exception {
364 getDriver().manage().deleteAllCookies();
368 final int max_attempts = 3;
370 while (!getDriver().manage().getCookies().isEmpty() && attempts < max_attempts){
371 getExtendTest().log(Status.INFO, "Trying to delete cookies one more time - " + (attempts + 1) + "/" + max_attempts + "attempts");
372 String deleteCookiesJS = "document.cookie.split(';').forEach(function(c) { document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); });";
373 ((JavascriptExecutor) getDriver()).executeScript(deleteCookiesJS);
376 if (attempts == max_attempts){
377 String msg = "Did not delete cookies, can't login as user " + WindowTestManager.getWindowMap().getUser().getRole();
378 System.out.println(msg);
379 getExtendTest().log(Status.ERROR, msg);
385 protected void loginToSystem(UserRoleEnum role) throws Exception {
386 UserCredentials credentials;
388 loginToSimulator(role);
389 credentials = new UserCredentials(role.getUserId(), WEB_SEAL_PASSWORD, role.getFirstName(), role.getLastName(), role.name());
392 credentials = getUserFromFileByRole(role);
393 sendUserAndPasswordKeys(credentials);
394 WebElement submitButton = GeneralUIUtils.getWebElementBy(By.name("btnSubmit"), 30);
395 submitButton.click();
396 WebElement buttonOK = GeneralUIUtils.getWebElementBy(By.name("successOK"), 30);
397 Assert.assertTrue(buttonOK.isDisplayed(), "OK button is not displayed.");
400 GeneralUIUtils.ultimateWait();
401 getWindowTest().setUser(credentials);
403 protected UserCredentials getUserFromFileByRole(UserRoleEnum role) throws Exception {
404 loadCredentialsFile();
405 return getUserCredentialsFromFile(role.name().toLowerCase());
407 private void goToHomePage(UserRoleEnum role) throws Exception {
409 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() == 0 ? NUM_OF_ATTEMPTS_TO_REFTRESH : getWindowTest().getRefreshAttempts());
410 if (!role.equals(UserRoleEnum.ADMIN)) {
412 WebElement closeButton = GeneralUIUtils.getClickableButtonBy(By.className("sdc-welcome-close"), 10);
413 if (closeButton != null){
417 if (!GeneralUIUtils.isElementVisibleByTestId(DataTestIdEnum.MainMenuButtons.HOME_BUTTON.getValue()))
419 restartBrowser(role);
423 catch (Exception e) {
424 restartBrowser(role);
427 private void restartBrowser(UserRoleEnum role) throws Exception {
428 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() - 1);
429 if (getWindowTest().getRefreshAttempts() <= 0) {
430 System.out.println("ERR : Something is wrong with browser!");
431 Assert.fail("ERR : Something is wrong with browser!");
433 System.out.println("Trying again...");
434 getExtendTest().log(Status.INFO, "Trying again...");
435 getExtendTest().log(Status.INFO, String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts() ));
436 System.out.println(String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts() ));
438 reloginWithNewRole(role);
441 public void loginToSimulator(UserRoleEnum role){
442 WebDriver driver = GeneralUIUtils.getDriver();
443 WebDriverWait wait = new WebDriverWait(driver, 30);
445 wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@method='" + "post" + "']"))));
447 WebElement userIdTextbox = GeneralUIUtils.getWebElementBy(By.name("userId"));
448 userIdTextbox.sendKeys(role.getUserId());
449 WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
450 passwordTextbox.sendKeys(WEB_SEAL_PASSWORD);
452 wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//*[@value='" + "Login" + "']")))).click();
455 private void sendUserAndPasswordKeys(UserCredentials userId) {
456 System.out.println("Login as user : " + userId.getUserId());
457 WebElement userNameTextbox = GeneralUIUtils.getWebElementBy(By.name("userid"));
458 userNameTextbox.sendKeys(userId.getUserId());
459 WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
460 passwordTextbox.sendKeys(userId.getPassword());
463 public void loginWithUser(UserRoleEnum role) {
465 getExtendTest().log(Status.INFO, String.format("Login as user %s", role.name().toUpperCase()));
468 } catch (Exception e) {
469 throw new RuntimeException(e);
472 getWindowTest().setPreviousRole(getWindowTest().getUser().getRole());
476 private void setUser(UserRoleEnum role) {
477 User user = new User();
478 user.setUserId(role.getUserId());
479 user.setFirstName(role.getFirstName());
480 user.setRole(role.name());
481 user.setLastName(role.getLastName());
483 getWindowTest().setUser(user);
486 public User getUser() {
487 return getWindowTest().getUser();
490 private void setBrowserBeforeTest(UserRoleEnum role) {
491 System.out.println(String.format("Setup before test as %s.", role.toString().toUpperCase() ));
493 System.out.println("Previous role is : " + getWindowTest().getPreviousRole() + " ; Current role is : " + role.name());
494 if (!getWindowTest().getPreviousRole().toLowerCase().equals(role.name().toLowerCase())){
495 System.out.println("Roles are different, navigate and login");
496 navigateAndLogin(role);
498 } catch (Exception e) {
503 public void navigateAndLogin(UserRoleEnum role) throws Exception {
504 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() != 0 ? getWindowTest().getRefreshAttempts() : 0);
508 GeneralUIUtils.ultimateWait();
511 public User getUser(UserRoleEnum role) {
512 User user = new User();
513 user.setUserId(role.getUserId());
514 user.setFirstName(role.getFirstName());
515 user.setLastName(role.getLastName());
516 user.setRole(role.name());
520 protected void reloginWithNewRole(UserRoleEnum role) throws Exception {
521 System.out.println(String.format("Setup before relogin as %s", role.toString().toUpperCase()));
522 navigateAndLogin(role);
525 /*public static void setLocalUrl(UserRoleEnum role) {
528 url = "http://localhost:8181/sdc1/proxy-admin1#/dashboard";
532 url = "http://localhost:8181/sdc1/proxy-designer1#/dashboard";
536 url = "http://localhost:8181/sdc1/proxy-governor1#/dashboard";
540 url = "http://localhost:8181/sdc1/proxy-ops1#/dashboard";
544 url = "http://localhost:8181/sdc1/proxy-tester1#/dashboard";
547 case PRODUCT_MANAGER1: {
548 url = "http://localhost:8181/sdc1/proxy-pm1#/dashboard";
551 case PRODUCT_MANAGER2: {
552 url = "http://localhost:8181/sdc1/proxy-pm2#/dashboard";
555 case PRODUCT_STRATEGIST1: {
556 url = "http://localhost:8181/sdc1/proxy-ps1#/dashboard";
559 case PRODUCT_STRATEGIST2: {
560 url = "http://localhost:8181/sdc1/proxy-ps2#/dashboard";
569 public void addTrafficFileToReport(ITestResult result) {
572 Har har = MobProxy.getPoxyServer().getHar();
573 String shortUUID = UUID.randomUUID().toString().split("-")[0];
574 File harFile = new File(getHarFilesFolder() + result.getName() + shortUUID + ".har");
575 new File(getHarFilesFolder()).mkdirs();
577 har.writeTo(harFile);
579 String pathToFileFromReportDirectory = HAR_FILES_FOLDER_NAME + File.separator + harFile.getName();
580 ExtentTestActions.addFileToReportAsLink(harFile, pathToFileFromReportDirectory, "File with captured traffic");
581 } catch (IOException ioe) {
582 ioe.printStackTrace();
587 * * Start section of test in ExtentReport with DataProvider parameters,
588 * should be started from test method, see example in onboardVNFTest
590 public void setLog(String fromDataProvider) {
592 String suiteName = ExtentManager.getSuiteName(myContext);
593 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
594 ExtentTestManager.startTest(RE_RUN +Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
596 ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
601 getWindowTest().setAddedValueFromDataProvider(fromDataProvider);
602 ExtentTestManager.assignCategory(this.getClass());
603 setBrowserBeforeTest(getRole());
608 /**************** MAIN ****************/
609 public static void main(String[] args) {
610 System.out.println("---------------------");
611 System.out.println("running test from CLI");
612 System.out.println("---------------------");
614 String attsdcFilePath = FileHandling.getBasePath() + File.separator + "conf" + File.separator + "attsdc.yaml";
615 System.setProperty("config.resource", attsdcFilePath);
616 System.out.println("attsdc.yaml file path is : " + attsdcFilePath);
618 String filepath = FileHandling.getBasePath() + File.separator + "Files" + File.separator;
619 System.setProperty("filePath", filepath);
620 System.out.println("filePath is : " + System.getProperty("filePath"));
622 Object[] testSuitsList = FileHandling.filterFileNamesFromFolder(FileHandling.getBasePath() + File.separator + "testSuites", ".xml");
623 if (testSuitsList != null) {
624 System.out.println(String.format("Found %s testSuite(s)", testSuitsList.length));
625 args = Arrays.copyOf(testSuitsList, testSuitsList.length, String[].class);
626 StartTest.main(args);