@Override
  public void onStart(ITestContext context) {
    CommonUtils.createDirectory(screenDirPath);
    imageCounter = 0;
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();

    StringBuilder builder = new StringBuilder();
    builder.append(
        "<html><style>"
            + "table {margin:0 auto;}td:first-child {width:200px;}td:nth-child(2) {width:660px;}td:nth-child(3) "
            + "{width:100px;}tr.success{color:black;background-color:#CCFFCC;}"
            + "tr.warning{color:black;background-color:#FEE01E;}"
            + "tr.error{color:black;background-color:#FFCCCC;}"
            + "tr.step{color:white;background:grey}"
            + "</style><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"
            + "<style>td { border-top: 1px solid grey; } </style></head><body>"
            + "<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.8.2.min.js\"></script>"
            + "<p>Date: "
            + DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ")
                .print(DateTime.now(DateTimeZone.UTC))
            + "</p>"
            + "<p>Polish Time: "
            + DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss ZZ")
                .print(
                    DateTime.now()
                        .withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Warsaw"))))
            + "</p>"
            + "<p>Browser: "
            + Configuration.getBrowser()
            + "</p>"
            + "<p>OS: "
            + System.getProperty("os.name")
            + "</p>"
            + "<p>Testing environment: "
            + new UrlBuilder().getUrlForWiki(Configuration.getWikiName())
            + "</p>"
            + "<p>Testing environment: "
            + Configuration.getEnv()
            + "</p>"
            + "<p>Tested version: "
            + "TO DO: GET WIKI VERSION HERE"
            + "</p>"
            + "<div id='toc'></div>");
    CommonUtils.appendTextToFile(logPath, builder.toString());
    appendShowHideButtons();
    try {
      FileInputStream input = new FileInputStream("./src/test/resources/script.txt");
      String content = IOUtils.toString(input);
      CommonUtils.appendTextToFile(logPath, content);
    } catch (IOException e) {
      System.out.println("no script.txt file available");
    }
  }
  @Override
  public void onTestStart(ITestResult result) {
    logsResults.clear();
    StringBuilder builder = new StringBuilder();
    String testName = result.getName().toString();
    String className = result.getTestClass().getName().toString();

    Method testMethod = result.getMethod().getConstructorOrMethod().getMethod();

    builder.append("<table>" + "<h1>Class: <em>" + className + "." + testName + " </em></h1>");
    if (testMethod.isAnnotationPresent(RelatedIssue.class)) {
      String issueID = testMethod.getAnnotation(RelatedIssue.class).issueID();
      String jiraUrl = jiraPath + issueID;
      builder.append(
          "<tr class=\"step\"><td>Known failure</td><td><h1><em>"
              + testName
              + " - "
              + "<a href=\""
              + jiraUrl
              + "\">"
              + issueID
              + "</a> "
              + testMethod.getAnnotation(RelatedIssue.class).comment()
              + "</em></h1></td><td> <br/> &nbsp;</td></tr>");
    } else {
      builder.append(
          "<tr class=\"step\"><td>&nbsp</td><td><h1><em>"
              + testName
              + "</em></h1></td><td> <br/> &nbsp;</td></tr>");
    }
    CommonUtils.appendTextToFile(logPath, builder.toString());
    System.out.println(className + " " + testName);
  }
  private static void log(String command, String description, boolean success, boolean ifLowLevel) {
    logsResults.add(success);
    String escapedDescription = escapeHtml(description);

    String className = success ? "success" : "error";
    StringBuilder builder = new StringBuilder();
    if (ifLowLevel) {
      builder.append(
          "<tr class=\""
              + className
              + " lowLevelAction"
              + "\"><td>"
              + command
              + "</td><td>"
              + escapedDescription
              + "</td><td> <br/> &nbsp;</td></tr>");
    } else {
      builder.append(
          "<tr class=\""
              + className
              + "\"><td>"
              + command
              + "</td><td>"
              + escapedDescription
              + "</td><td> <br/> &nbsp;</td></tr>");
    }
    CommonUtils.appendTextToFile(logPath, builder.toString());
    logJSError(NewDriverProvider.getWebDriver());
  }
 private void appendShowHideButtons() {
   String hideButton = "<button id=\"hideLowLevel\">hide low level actions</button>";
   String showButton = "<button id=\"showLowLevel\">show low level actions</button>";
   StringBuilder builder = new StringBuilder();
   builder.append(hideButton);
   builder.append(showButton);
   CommonUtils.appendTextToFile(logPath, builder.toString());
 }
 public String resetForgotPasswordTime(String userName, String apiToken) {
   String[][] apiRequestParameters = {
     {"action", ApiActions.API_ACTION_FORGOT_PASSWORD},
     {"user", userName},
     {"token", apiToken},
     {"format", "json"},
   };
   return CommonUtils.sendPost(URLsContent.API_URL, apiRequestParameters);
 }
 @Override
 public void afterChangeValueOf(WebElement element, WebDriver driver) {
   StringBuilder builder = new StringBuilder();
   builder.append(
       "<tr class=\"success lowLevelAction\"><td>ChangeValueOfField</td><td>"
           + lastFindBy
           + "</td><td> <br/> &nbsp;</td></tr>");
   CommonUtils.appendTextToFile(logPath, builder.toString());
 }
 @Override
 public void beforeNavigateTo(String url, WebDriver driver) {
   StringBuilder builder = new StringBuilder();
   builder.append(
       "<tr class=\"success\"><td>Navigate to</td><td>"
           + url
           + "</td><td> <br/> &nbsp;</td></tr>");
   CommonUtils.appendTextToFile(logPath, builder.toString());
   logJSError(driver);
 }
 @Override
 public void onTestSuccess(ITestResult result) {
   StringBuilder builder = new StringBuilder();
   builder.append(
       "<tr class=\"step\">"
           + "<td>&nbsp</td><td>STOP LOGGING METHOD  "
           + "<div style=\"text-align:center\">"
           + "<a href=\"#toc\" style=\"color:blue\">"
           + "<b>BACK TO MENU</b></a></div> </td><td> <br/> &nbsp;</td></tr>"
           + "</table>");
   CommonUtils.appendTextToFile(logPath, builder.toString());
 }
 /**
  * Selects given files in upload browser.
  *
  * @author Michal Nowierski
  * @author Karol 'kkarolk' Kujawiak ** @param FilesNamesList List of files to be uploaded
  *     <p>Look at folder PageContent.resourcesPath
  */
 public void selectFilesToUpload(String[] filesNamesList) {
   wait.forElementVisible(multipleUploadForm);
   for (int i = 0; i < filesNamesList.length; i++) {
     jsActions.scrollToElement(fileInputs.get(i));
     fileInputs
         .get(i)
         .sendKeys(
             CommonUtils.getAbsolutePathForFile(
                 PageContent.IMAGE_UPLOAD_RESOURCES_PATH + filesNamesList[i]));
   }
   PageObjectLogging.log(
       "typeInFilesToUpload", filesNamesList.length + " files added to upload list", true);
 }
 public static void log(String command, String description, boolean success, WebDriver driver) {
   logsResults.add(success);
   imageCounter += 1;
   new Shooter().savePageScreenshot(screenPath + imageCounter, driver);
   CommonUtils.appendTextToFile(screenPath + imageCounter + ".html", getPageSource(driver));
   String className = success ? "success" : "error";
   StringBuilder builder = new StringBuilder();
   builder.append(
       "<tr class=\""
           + className
           + "\"><td>"
           + command
           + "</td><td>"
           + description
           + "</td><td> <br/><a href='screenshots/screenshot"
           + imageCounter
           + ".png'>Screenshot</a><br/><a href='screenshots/screenshot"
           + imageCounter
           + ".html'>HTML Source</a></td></tr>");
   CommonUtils.appendTextToFile(logPath, builder.toString());
   logJSError(driver);
 }
  @Override
  public void onTestFailure(ITestResult result) {
    driver = NewDriverProvider.getWebDriver();
    if (driver == null) {
      driver = NewDriverProvider.getWebDriver();
    }

    imageCounter += 1;
    if ("true".equals(Configuration.getLogEnabled())) {
      try {
        new Shooter().savePageScreenshot(screenPath + imageCounter, driver);
        CommonUtils.appendTextToFile(screenPath + imageCounter + ".html", getPageSource(driver));
      } catch (Exception e) {
        log(
            "onException",
            "driver has no ability to catch screenshot or html source - driver may died",
            false);
      }

      String exception =
          escapeHtml(
              result.getThrowable().toString()
                  + "\n"
                  + ExceptionUtils.getStackTrace(result.getThrowable()));

      StringBuilder builder = new StringBuilder();
      builder.append(
          "<tr class=\"error\"><td>error</td><td><pre>"
              + exception
              + "</pre></td><td> <br/><a href='screenshots/screenshot"
              + imageCounter
              + ".png'>Screenshot</a><br/><a href='screenshots/screenshot"
              + imageCounter
              + ".html'>HTML Source</a></td></tr>");
      CommonUtils.appendTextToFile(logPath, builder.toString());
      logJSError(driver);
      onTestSuccess(result);
    }
  }
 private static void logJSError(WebDriver driver) {
   if ("true".equals(Configuration.getJSErrorsEnabled())) {
     JavascriptExecutor js = (JavascriptExecutor) driver;
     List<String> error =
         (ArrayList<String>) js.executeScript("return window.JSErrorCollector_errors.pump()");
     if (!error.isEmpty()) {
       StringBuilder builder = new StringBuilder();
       builder.append(
           "<tr class=\"error\"><td>click</td><td>" + error + "</td><td> <br/> &nbsp;</td></tr>");
       CommonUtils.appendTextToFile(logPath, builder.toString());
     }
   }
 }
 public static void logImage(String command, String imageAsBase64, boolean success) {
   imageAsBase64 = "<img src=\"data:image/png;base64," + imageAsBase64 + "\">";
   String className = success ? "success" : "error";
   CommonUtils.appendTextToFile(
       logPath,
       ("<tr class=\""
           + className
           + "\"><td>"
           + command
           + "</td><td>"
           + imageAsBase64
           + "</td><td> <br/> &nbsp;</td></tr>"));
 }
 /** This method will log warning to log file (line in yellow color) */
 public static void logWarning(String command, String description) {
   StringBuilder builder =
       new StringBuilder()
           .append(
               "<tr class=\"warning\">"
                   + "<td>"
                   + command
                   + "</td>"
                   + "<td>"
                   + description
                   + "</td>"
                   + "<td> <br/> &nbsp;</td></tr>");
   CommonUtils.appendTextToFile(logPath, builder.toString());
 }
 @Override
 public void afterNavigateTo(String url, WebDriver driver) {
   StringBuilder builder = new StringBuilder();
   if (!AlertHandler.isAlertPresent(driver)) {
     if (url.equals(driver.getCurrentUrl())) {
       builder.append(
           "<tr class=\"success\"><td>Url after navigation</td><td>"
               + driver.getCurrentUrl()
               + "</td><td> <br/> &nbsp;</td></tr>");
       CommonUtils.appendTextToFile(logPath, builder.toString());
     } else {
       if (driver.getCurrentUrl().contains("data:text/html,chromewebdata ")) {
         driver.get(url);
       }
       logWarning("Url after navigation", driver.getCurrentUrl());
     }
   } else {
     logWarning("Url after navigation", "Unable to check URL after navigation - alert present");
   }
   logJSError(driver);
 }
 @Override
 public void onFinish(ITestContext context) {
   CommonUtils.appendTextToFile(logPath, "</body></html>");
 }