<commit_before>package com.frameworkium.listeners;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.frameworkium.capture.ElementHighlighter;
import com.frameworkium.capture.ScreenshotCapture;
import com.frameworkium.capture.model.Command;
import com.frameworkium.config.DriverType;
import com.frameworkium.config.WebDriverWrapper;
import com.frameworkium.tests.internal.BaseTest;

public class CaptureListener implements WebDriverEventListener, ITestListener {

    private static final Logger logger = LogManager.getLogger(CaptureListener.class);

    private void takeScreenshotAndSendToCapture(Command command, WebDriver driver) {
        logger.debug("takeScreenshotAndSendToCapture");
        BaseTest.getCapture().takeAndSendScreenshot(command, driver, null);
    }

    private void takeScreenshotAndSendToCapture(String action, WebDriver driver) {
        Command command = new Command(action, "n/a", "n/a");
        takeScreenshotAndSendToCapture(command, driver);
    }

    private void takeScreenshotAndSendToCaptureWithException(String action, WebDriver driver, Throwable thrw) {
        Command command = new Command(action, "n/a", "n/a");
        BaseTest.getCapture().takeAndSendScreenshot(command, driver, thrw.getMessage() + "\n" + ExceptionUtils.getStackTrace(thrw));
    }

    private void sendFinalScreenshot(ITestResult result, String action) {
        WebDriverWrapper webDriver = BaseTest.getDriver();
        Throwable thrw = result.getThrowable();
        if (null != thrw) {
            takeScreenshotAndSendToCaptureWithException(action, webDriver, thrw);
        } else {
            Command command = new Command(action, "n/a", "n/a");
            takeScreenshotAndSendToCapture(command, webDriver);
        }
    }

    private void highlightElementTakeScreenshotAndSendToCapture(String action, WebDriver driver, WebElement element) {
        logger.debug(String.format("highlightElementTakeScreenshotAndSendToCapture: action=%s", action));
        ElementHighlighter highlighter = null;
        if (!DriverType.isNative()) {
            logger.debug("Not an app, trying to highlight the element");
            highlighter = new ElementHighlighter(driver);
            highlighter.highlightElement(element);
        }
        Command command = new Command(action, element);
        takeScreenshotAndSendToCapture(command, driver);
        if (null != highlighter) {
            highlighter.unhighlightLast();
        }
    }

    /* WebDriver events */
    @Override
    public void beforeClickOn(WebElement element, WebDriver driver) {
        highlightElementTakeScreenshotAndSendToCapture("click", driver, element);
    }

    @Override
    public void afterChangeValueOf(WebElement element, WebDriver driver) {
        highlightElementTakeScreenshotAndSendToCapture("change", driver, element);
    }

    @Override
    public void beforeNavigateBack(WebDriver driver) {
        takeScreenshotAndSendToCapture("nav back", driver);
    }<commit_msg>Added protection against StaleElementReferenceException during highlighting<commit_after>package com.frameworkium.listeners;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.frameworkium.capture.ElementHighlighter;
import com.frameworkium.capture.ScreenshotCapture;
import com.frameworkium.capture.model.Command;
import com.frameworkium.config.DriverType;
import com.frameworkium.config.WebDriverWrapper;
import com.frameworkium.tests.internal.BaseTest;

public class CaptureListener implements WebDriverEventListener, ITestListener {

    private static final Logger logger = LogManager.getLogger(CaptureListener.class);

    private void takeScreenshotAndSendToCapture(Command command, WebDriver driver) {
        logger.debug("takeScreenshotAndSendToCapture");
        BaseTest.getCapture().takeAndSendScreenshot(command, driver, null);
    }

    private void takeScreenshotAndSendToCapture(String action, WebDriver driver) {
        Command command = new Command(action, "n/a", "n/a");
        takeScreenshotAndSendToCapture(command, driver);
    }

    private void takeScreenshotAndSendToCaptureWithException(String action, WebDriver driver, Throwable thrw) {
        Command command = new Command(action, "n/a", "n/a");
        BaseTest.getCapture().takeAndSendScreenshot(command, driver, thrw.getMessage() + "\n" + ExceptionUtils.getStackTrace(thrw));
    }

    private void sendFinalScreenshot(ITestResult result, String action) {
        WebDriverWrapper webDriver = BaseTest.getDriver();
        Throwable thrw = result.getThrowable();
        if (null != thrw) {
            takeScreenshotAndSendToCaptureWithException(action, webDriver, thrw);
        } else {
            Command command = new Command(action, "n/a", "n/a");
            takeScreenshotAndSendToCapture(command, webDriver);
        }
    }

    private void highlightElementTakeScreenshotAndSendToCapture(String action, WebDriver driver, WebElement element) {
        logger.debug(String.format("highlightElementTakeScreenshotAndSendToCapture: action=%s", action));
        ElementHighlighter highlighter = null;
        try {
            if (!DriverType.isNative()) {
                logger.debug("Trying to highlight the element");
                highlighter = new ElementHighlighter(driver);
                highlighter.highlightElement(element);
            }
            Command command = new Command(action, element);
            takeScreenshotAndSendToCapture(command, driver);
            if (null != highlighter) {
                highlighter.unhighlightLast();
            }
        } catch (StaleElementReferenceException serex) {
            logger.debug("Caught StaleElementReferenceException when trying to (un)highlight an element.");
        }
    }

    /* WebDriver events */
    @Override
    public void beforeClickOn(WebElement element, WebDriver driver) {
        highlightElementTakeScreenshotAndSendToCapture("click", driver, element);
    }

    @Override
    public void afterChangeValueOf(WebElement element, WebDriver driver) {
        highlightElementTakeScreenshotAndSendToCapture("change", driver, element);
    }

    @Override
    public void beforeNavigateBack(WebDriver driver) {
        takeScreenshotAndSendToCapture("nav back", driver);
    }
{'fraction_non_alphanumeric': 0.09044513751063227, 'fraction_numerical': 0.0005670541536716756, 'mean_word_length': 3.9543539325842696, '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': '2495268', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1877, 'n_words': 386}