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.config.UserCredentialsFromFile;
39 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestManager;
40 import org.openecomp.sdc.ci.tests.api.SomeInterface;
41 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
42 import org.openecomp.sdc.ci.tests.datatypes.UserCredentials;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.openecomp.sdc.ci.tests.execute.sanity.OnboardingFlowsUI;
45 import org.openecomp.sdc.ci.tests.execute.setup.ExtentManager.suiteNameXml;
46 import org.openecomp.sdc.ci.tests.pages.HomePage;
47 import org.openecomp.sdc.ci.tests.run.StartTest;
48 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
49 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
50 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
51 import org.openecomp.sdc.ci.tests.utils.rest.AutomationUtils;
52 import org.openqa.selenium.By;
53 import org.openqa.selenium.JavascriptExecutor;
54 import org.openqa.selenium.WebDriver;
55 import org.openqa.selenium.WebElement;
56 import org.openqa.selenium.support.ui.ExpectedConditions;
57 import org.openqa.selenium.support.ui.WebDriverWait;
58 import org.slf4j.LoggerFactory;
59 import org.testng.Assert;
60 import org.testng.ITestContext;
61 import org.testng.ITestResult;
62 import org.testng.annotations.AfterMethod;
63 import org.testng.annotations.AfterSuite;
64 import org.testng.annotations.BeforeMethod;
65 import org.testng.annotations.BeforeSuite;
66 import org.testng.annotations.Optional;
67 import org.testng.annotations.Parameters;
68 import org.testng.annotations.Test;
70 import com.aventstack.extentreports.ExtentReports;
71 import com.aventstack.extentreports.ExtentTest;
72 import com.aventstack.extentreports.Status;
74 import ch.qos.logback.classic.Level;
75 import ch.qos.logback.classic.LoggerContext;
76 import groovyjarjarantlr.Utils;
77 import net.lightbody.bmp.BrowserMobProxyServer;
78 import net.lightbody.bmp.core.har.Har;
80 public abstract class SetupCDTest extends DriverFactory {
82 // private static final String RE_RUN = "ReRun - ";
83 private static final String RE_RUN = "<html><font color=\"red\">ReRun - </font></html>";
84 private static final String WEB_SEAL_PASSWORD = "123123a";
86 public SetupCDTest() {
87 LoggerContext lc = (LoggerContext) LoggerFactory. getILoggerFactory();
88 lc.getLogger("org.apache").setLevel(Level.INFO);
91 /**************** CONSTANTS ****************/
92 private static final String CREDENTIALS_FILE = "credentials.yaml";
93 private static final String REPORT_FILE_NAME = "SDC_UI_Extent_Report.html";
94 protected static final String REPORT_FOLDER = "." + File.separator + "ExtentReport" + File.separator;
95 protected static final String SCREENSHOT_FOLDER = REPORT_FOLDER + "screenshots" + File.separator;
96 protected static final String HAR_FILES_FOLDER_NAME = "har_files";
97 protected static final String HAR_FILES_FOLDER = REPORT_FOLDER + HAR_FILES_FOLDER_NAME + File.separator;
100 private static final String SHORT_CSV_REPORT_FILE_NAME = "ShortReport.csv";
101 private static final int NUM_OF_ATTEMPTS_TO_REFTRESH = 2;
104 /**************** PRIVATES ****************/
105 private static String url;
106 private static boolean uiSimulator;
107 private static boolean localEnv = true;
108 private static OnboardCSVReport csvReport;
109 private final UserCredentialsFromFile credentialsIns = UserCredentialsFromFile.getInstance();
111 protected static ITestContext myContext;
114 /**************** METHODS ****************/
115 public static ExtentTest getExtendTest() {
116 SomeInterface testManager = new ExtentTestManager();
117 return testManager.getTest();
119 public static WindowTest getWindowTest(){
120 return WindowTestManager.getWindowMap();
123 public OnboardCSVReport getCsvReport() {
127 public static String getReportFolder() {
128 return REPORT_FOLDER;
131 public static String getScreenshotFolder() {
132 return SCREENSHOT_FOLDER;
135 public static String getHarFilesFolder() {
136 return HAR_FILES_FOLDER;
140 protected abstract UserRoleEnum getRole();
142 /**************** BEFORE ****************/
144 @BeforeSuite(alwaysRun = true)
145 public void setupBeforeSuite(ITestContext context) throws Exception {
146 RestCDUtils.deleteOnDemand();
148 setErrorConfigurationFile();
150 ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
151 csvReport = new OnboardCSVReport(getReportFolder(), SHORT_CSV_REPORT_FILE_NAME);
154 private static void setErrorConfigurationFile() {
155 if (!System.getProperty("os.name").contains("Windows")){
156 String errorConfigurationFilename = getConfig().getErrorConfigurationFile();
157 errorConfigurationFilename = errorConfigurationFilename.substring(errorConfigurationFilename.lastIndexOf("/") + 1, errorConfigurationFilename.length());
158 getConfig().setErrorConfigurationFile(FileHandling.getBasePath() + File.separator + "conf" + File.separator + errorConfigurationFilename);
159 if (new File(getConfig().getErrorConfigurationFile()).exists()){
160 System.out.println("Found error-configuration.yaml in : " + getConfig().getErrorConfigurationFile());
165 @BeforeMethod(alwaysRun = true )
166 public void setBrowserBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
168 boolean emptyDataProvider = method.getAnnotation(Test.class).dataProvider().isEmpty();
169 String className = method.getDeclaringClass().getName();
170 if (emptyDataProvider && !className.contains("ToscaValidationTest") ) {
171 System.out.println("ExtentReport instance started from BeforeMethod...");
172 String suiteName = ExtentManager.getSuiteName(context);
173 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
174 ExtentTestManager.startTest(RE_RUN + method.getName());
176 ExtentTestManager.startTest(method.getName());
179 ExtentTestManager.assignCategory(this.getClass());
180 setBrowserBeforeTest(getRole());
182 System.out.println("ExtentReport instance started from Test...");
185 getConfig().setWindowsDownloadDirectory(getWindowTest().getDownloadDirectory());
187 if (getConfig().getCaptureTraffic()){
189 MobProxy.getPoxyServer().newHar(method.getName() + ".har");
190 } catch (Throwable e) {
196 /**************** AFTER ****************/
197 @AfterMethod(alwaysRun = true)
198 public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {
201 ReportAfterTestManager.report(result, context);
202 GeneralUIUtils.closeErrorMessage();
206 if (getConfig().getCaptureTraffic()){
207 addTrafficFileToReport(result);
210 if (result.getInstanceName().equals(OnboardingFlowsUI.class.getName()) && result.getStatus() == ITestResult.FAILURE){
211 System.out.println("Onboarding test failed, closign browser....");
212 getExtendTest().log(Status.INFO, "Onboarding test failed, closing browser....");
215 else if (!getUser().getRole().toLowerCase().equals(UserRoleEnum.ADMIN.name().toLowerCase())){
216 boolean navigateToHomePageSuccess = HomePage.navigateToHomePage();
217 if (!navigateToHomePageSuccess){
218 System.out.println("Navigating to homepage failed, reopening driver....");
219 getExtendTest().log(Status.INFO, "Navigating to homepage failed, reopening driver....");
224 } catch (Exception e) {
226 getExtendTest().log(Status.ERROR, "Exception:"+ e.toString());
231 ExtentTestManager.endTest();
232 String suiteName = ExtentManager.getSuiteName(context);
233 // write result to csv file
234 if((!suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) && (result.getStatus() == ITestResult.SKIP)) {
235 addResultToCSV(result, context);
237 if(suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue()) && !(result.getStatus() == ITestResult.SUCCESS)) {
238 addResultToCSV(result, context);
240 // ExtentManager.closeReporter();
241 FileHandling.cleanCurrentDownloadDir();
245 public void addResultToCSV(ITestResult result, ITestContext context) {
246 // String suiteName = ExtentManager.getSuiteName(context);
247 ExtentTest test = getExtendTest();
248 com.aventstack.extentreports.model.Test model = test.getModel();
249 String name = model.getName();
250 String status = model.getStatus().toString();
251 // if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue()) && !(result.getStatus() == ITestResult.SUCCESS)) {
252 getCsvReport().writeRow(result.getInstanceName(), name.replace(RE_RUN,""), status);
256 public void generateReport4Jenkins(ITestContext context) {
257 String suiteName = ExtentManager.getSuiteName(context);
258 // String outputDirectory = context.getOutputDirectory();
259 JSONObject obj = new JSONObject();
260 String success = Integer.toString(context.getPassedTests().size());
261 String failed = Integer.toString(context.getFailedTests().size());
262 String total = Integer.toString(context.getFailedTests().size()+context.getPassedTests().size());
263 obj.put("projectName", "SDC-ONAP-UI-Automation-"+suiteName);
264 obj.put("projectVersion", AutomationUtils.getOSVersion());
265 obj.put("platform", "Linux");
266 obj.put("total", total);
267 obj.put("success", success);
268 obj.put("failed", failed);
270 try (FileWriter file = new FileWriter(getReportFolder() + "jenkinsResults.json")) {
272 file.write(obj.toJSONString());
275 } catch (IOException e) {
279 System.out.print(obj);
285 @Parameters({ "eraseAfterTests" })
286 @AfterSuite(alwaysRun = true)
287 public void afterSuite2(@Optional("true") String eraseAfterTestsReadValue) throws Exception {
288 // public void afterSuite() throws Exception {
290 csvReport.closeFile();
291 generateReport4Jenkins(myContext);
293 if (Boolean.parseBoolean(eraseAfterTestsReadValue)){
294 RestCDUtils.deleteOnDemand();
296 System.out.println("Resources will not be deleted according to suite configuration ...");
299 if (getConfig().getUseBrowserMobProxy()){
300 MobProxy.getPoxyServer().stop();
304 protected static String setUrl() {
305 url = getConfig().getUrl();
306 uiSimulator = getConfig().isUiSimulator();
308 String message = "no URL found";
309 System.out.println(message);
310 Assert.fail(message);
311 } 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) {
319 public static void navigateToUrl(String url) throws Exception {
321 System.out.println("Deleting cookies...");
324 System.out.println("Navigating to URL : " + url);
325 getDriver().navigate().to(url);
326 GeneralUIUtils.waitForLoader();
328 System.out.println("Zooming out...");
329 GeneralUIUtils.windowZoomOutUltimate();
332 catch (Exception e) {
333 String msg = "Browser is unreachable";
334 System.out.println(msg);
335 getExtendTest().log(Status.ERROR, msg);
339 private static void deleteCookies() throws Exception {
340 getDriver().manage().deleteAllCookies();
344 final int max_attempts = 3;
346 while (!getDriver().manage().getCookies().isEmpty() && attempts < max_attempts){
347 getExtendTest().log(Status.INFO, "Trying to delete cookies one more time - " + (attempts + 1) + "/" + max_attempts + "attempts");
348 String deleteCookiesJS = "document.cookie.split(';').forEach(function(c) { document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); });";
349 ((JavascriptExecutor) getDriver()).executeScript(deleteCookiesJS);
352 if (attempts == max_attempts){
353 String msg = "Did not delete cookies, can't login as user " + WindowTestManager.getWindowMap().getUser().getRole();
354 System.out.println(msg);
355 getExtendTest().log(Status.ERROR, msg);
361 protected void loginToSystem(UserRoleEnum role) throws Exception {
362 UserCredentials credentials = new UserCredentials(role.getUserId(), role.getPassword(), role.getFirstName(), role.getLastName(), role.name());
364 loginToSimulator(role);
367 sendUserAndPasswordKeys(credentials);
368 WebElement submitButton = GeneralUIUtils.getWebElementBy(By.name("btnSubmit"), 30);
369 submitButton.click();
370 WebElement buttonOK = GeneralUIUtils.getWebElementBy(By.name("successOK"), 30);
371 Assert.assertTrue(buttonOK.isDisplayed(), "OK button is not displayed.");
374 GeneralUIUtils.ultimateWait();
375 getWindowTest().setUser(credentials);
378 private void goToHomePage(UserRoleEnum role) throws Exception {
380 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() == 0 ? NUM_OF_ATTEMPTS_TO_REFTRESH : getWindowTest().getRefreshAttempts());
381 if (!role.equals(UserRoleEnum.ADMIN)) {
383 WebElement closeButton = GeneralUIUtils.getClickableButtonBy(By.className("sdc-welcome-close"), 10);
384 if (closeButton != null){
388 if (!GeneralUIUtils.isElementVisibleByTestId(DataTestIdEnum.MainMenuButtons.HOME_BUTTON.getValue()))
390 restartBrowser(role);
394 catch (Exception e) {
395 restartBrowser(role);
398 private void restartBrowser(UserRoleEnum role) throws Exception {
399 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() - 1);
400 if (getWindowTest().getRefreshAttempts() <= 0) {
401 System.out.println("ERR : Something is wrong with browser!");
402 Assert.fail("ERR : Something is wrong with browser!");
404 System.out.println("Trying again...");
405 getExtendTest().log(Status.INFO, "Trying again...");
406 getExtendTest().log(Status.INFO, String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts() ));
407 System.out.println(String.format("%s attempt(s) left", getWindowTest().getRefreshAttempts() ));
409 reloginWithNewRole(role);
412 public void loginToSimulator(UserRoleEnum role){
413 WebDriver driver = GeneralUIUtils.getDriver();
414 WebDriverWait wait = new WebDriverWait(driver, 30);
416 wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@method='" + "post" + "']"))));
418 WebElement userIdTextbox = GeneralUIUtils.getWebElementBy(By.name("userId"));
419 userIdTextbox.sendKeys(role.getUserId());
420 WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
421 passwordTextbox.sendKeys(WEB_SEAL_PASSWORD);
423 wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//*[@value='" + "Login" + "']")))).click();
426 private void sendUserAndPasswordKeys(UserCredentials userId) {
427 System.out.println("Login as user : " + userId.getUserId());
428 WebElement userNameTextbox = GeneralUIUtils.getWebElementBy(By.name("userid"));
429 userNameTextbox.sendKeys(userId.getUserId());
430 WebElement passwordTextbox = GeneralUIUtils.getWebElementBy(By.name("password"));
431 passwordTextbox.sendKeys(userId.getPassword());
434 public void loginWithUser(UserRoleEnum role) {
436 getExtendTest().log(Status.INFO, String.format("Login as user %s", role.name().toUpperCase()));
439 } catch (Exception e) {
440 throw new RuntimeException(e);
443 getWindowTest().setPreviousRole(getWindowTest().getUser().getRole());
447 private void setUser(UserRoleEnum role) {
448 User user = new User();
449 user.setUserId(role.getUserId());
450 user.setFirstName(role.getFirstName());
451 user.setRole(role.name());
452 user.setLastName(role.getLastName());
454 getWindowTest().setUser(user);
457 public User getUser() {
458 return getWindowTest().getUser();
461 private void setBrowserBeforeTest(UserRoleEnum role) {
462 System.out.println(String.format("Setup before test as %s.", role.toString().toUpperCase() ));
464 System.out.println("Previous role is : " + getWindowTest().getPreviousRole() + " ; Current role is : " + role.name());
465 if (!getWindowTest().getPreviousRole().toLowerCase().equals(role.name().toLowerCase())){
466 System.out.println("Roles are different, navigate and login");
467 navigateAndLogin(role);
469 } catch (Exception e) {
474 public void navigateAndLogin(UserRoleEnum role) throws Exception {
475 getWindowTest().setRefreshAttempts(getWindowTest().getRefreshAttempts() != 0 ? getWindowTest().getRefreshAttempts() : 0);
479 GeneralUIUtils.ultimateWait();
482 public User getUser(UserRoleEnum role) {
483 User user = new User();
484 user.setUserId(role.getUserId());
485 user.setFirstName(role.getFirstName());
486 user.setLastName(role.getLastName());
487 user.setRole(role.name());
491 protected void reloginWithNewRole(UserRoleEnum role) throws Exception {
492 System.out.println(String.format("Setup before relogin as %s", role.toString().toUpperCase()));
493 navigateAndLogin(role);
496 /*public static void setLocalUrl(UserRoleEnum role) {
499 url = "http://localhost:8181/sdc1/proxy-admin1#/dashboard";
503 url = "http://localhost:8181/sdc1/proxy-designer1#/dashboard";
507 url = "http://localhost:8181/sdc1/proxy-governor1#/dashboard";
511 url = "http://localhost:8181/sdc1/proxy-ops1#/dashboard";
515 url = "http://localhost:8181/sdc1/proxy-tester1#/dashboard";
518 case PRODUCT_MANAGER1: {
519 url = "http://localhost:8181/sdc1/proxy-pm1#/dashboard";
522 case PRODUCT_MANAGER2: {
523 url = "http://localhost:8181/sdc1/proxy-pm2#/dashboard";
526 case PRODUCT_STRATEGIST1: {
527 url = "http://localhost:8181/sdc1/proxy-ps1#/dashboard";
530 case PRODUCT_STRATEGIST2: {
531 url = "http://localhost:8181/sdc1/proxy-ps2#/dashboard";
540 public void addTrafficFileToReport(ITestResult result) {
543 Har har = MobProxy.getPoxyServer().getHar();
544 String shortUUID = UUID.randomUUID().toString().split("-")[0];
545 File harFile = new File(getHarFilesFolder() + result.getName() + shortUUID + ".har");
546 new File(getHarFilesFolder()).mkdirs();
548 har.writeTo(harFile);
550 String pathToFileFromReportDirectory = HAR_FILES_FOLDER_NAME + File.separator + harFile.getName();
551 ExtentTestActions.addFileToReportAsLink(harFile, pathToFileFromReportDirectory, "File with captured traffic");
552 } catch (IOException ioe) {
553 ioe.printStackTrace();
558 * * Start section of test in ExtentReport with DataProvider parameters,
559 * should be started from test method, see example in onboardVNFTest
561 public void setLog(String fromDataProvider) {
563 String suiteName = ExtentManager.getSuiteName(myContext);
564 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
565 ExtentTestManager.startTest(RE_RUN +Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
567 ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
572 getWindowTest().setAddedValueFromDataProvider(fromDataProvider);
573 ExtentTestManager.assignCategory(this.getClass());
574 setBrowserBeforeTest(getRole());
579 /**************** MAIN ****************/
580 public static void main(String[] args) {
581 System.out.println("---------------------");
582 System.out.println("running test from CLI");
583 System.out.println("---------------------");
585 String attsdcFilePath = FileHandling.getBasePath() + File.separator + "conf" + File.separator + "attsdc.yaml";
586 System.setProperty("config.resource", attsdcFilePath);
587 System.out.println("attsdc.yaml file path is : " + attsdcFilePath);
589 String filepath = FileHandling.getBasePath() + File.separator + "Files" + File.separator;
590 System.setProperty("filePath", filepath);
591 System.out.println("filePath is : " + System.getProperty("filePath"));
593 Object[] testSuitsList = FileHandling.filterFileNamesFromFolder(FileHandling.getBasePath() + File.separator + "testSuites", ".xml");
594 if (testSuitsList != null) {
595 System.out.println(String.format("Found %s testSuite(s)", testSuitsList.length));
596 args = Arrays.copyOf(testSuitsList, testSuitsList.length, String[].class);
597 StartTest.main(args);