<commit_before>package net.jozefdransfield.uiregression

import com.thoughtworks.selenium.SeleneseTestCase
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import com.thoughtworks.selenium.DefaultSelenium
import grails.util.BuildSettingsHolder
import java.awt.image.RenderedImage
import org.apache.commons.codec.binary.Base64

public class UIRegressionTestCase extends SeleneseTestCase {

  public void setUp(String rootUrl, String browserString) {
    String seleniumServer = BuildSettingsHolder.getSettings().config.uiregression.selenium.host
    int seleniumPort = BuildSettingsHolder.getSettings().config.uiregression.selenium.port

    selenium = new DefaultSelenium(seleniumServer, seleniumPort, browserString, rootUrl)
    selenium.start()
  }

  public void navigateToAssertScreenShot(String screenShotName, Closure closure) {
    initialiseReportDirectory(screenShotName)
    closure.call()
    String screen = selenium.captureEntirePageScreenshotToString("")
    byte[] imageBytes = Base64.decodeBase64(screen.getBytes("UTF-8"))
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes))
    ImageIO.write(image, "png", new File(resultImagePath(screenShotName)))

    if (!loadScreenShotsAndCompare(screenShotName)) {
      fail("Result Image at [result:${resultImagePath(screenShotName)}]\n did not match [reference:${referenceImagePath(screenShotName)}]\n for ID: [${screenShotName}]\n [diff:${diffImagePath(screenShotName)}]")
    }
  }

  private boolean loadScreenShotsAndCompare(String screenShotName) {
    File result = loadResultFile(screenShotName)
    File reference = loadReferenceFile(screenShotName)

    println result<commit_msg>Full path is always outputted in the fail message<commit_after>package net.jozefdransfield.uiregression

import com.thoughtworks.selenium.SeleneseTestCase
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import com.thoughtworks.selenium.DefaultSelenium
import grails.util.BuildSettingsHolder
import java.awt.image.RenderedImage
import org.apache.commons.codec.binary.Base64

public class UIRegressionTestCase extends SeleneseTestCase {

  public void setUp(String rootUrl, String browserString) {
    String seleniumServer = BuildSettingsHolder.getSettings().config.uiregression.selenium.host
    int seleniumPort = BuildSettingsHolder.getSettings().config.uiregression.selenium.port

    selenium = new DefaultSelenium(seleniumServer, seleniumPort, browserString, rootUrl)
    selenium.start()
  }

  public void navigateToAssertScreenShot(String screenShotName, Closure closure) {
    initialiseReportDirectory(screenShotName)
    closure.call()
    String screen = selenium.captureEntirePageScreenshotToString("")
    byte[] imageBytes = Base64.decodeBase64(screen.getBytes("UTF-8"))
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes))
    ImageIO.write(image, "png", new File(resultImagePath(screenShotName)))

    if (!loadScreenShotsAndCompare(screenShotName)) {
      File resultImage = new File(resultImagePath(screenShotName))
      File referenceImage = new File(referenceImagePath(screenShotName))
      File diffImage = new File(diffImagePath(screenShotName))
      fail("Result Image at [result:${resultImage.absolutePath}]\n did not match [reference:${referenceImage.absolutePath}]\n for ID: [${screenShotName}]\n [diff:${diffImage.absolutePath}]")
    }
  }

  private boolean loadScreenShotsAndCompare(String screenShotName) {
    File result = loadResultFile(screenShotName)
    File reference = loadReferenceFile(screenShotName)

    println result
{'fraction_non_alphanumeric': 0.08954393770856507, 'fraction_numerical': 0.00389321468298109, 'mean_word_length': 6.094674556213017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6745244', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1013, 'n_words': 195}