All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.seleniumtests.core.testanalysis.ErrorCauseFinder Maven / Gradle / Ivy

There is a newer version: 4.23.18
Show newest version
package com.seleniumtests.core.testanalysis;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.testng.ITestResult;

import com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector;
import com.seleniumtests.connectors.selenium.fielddetector.Field;
import com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector;
import com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector.FieldType;
import com.seleniumtests.connectors.selenium.fielddetector.Label;
import com.seleniumtests.core.TestStepManager;
import com.seleniumtests.core.utils.TestNGResultUtils;
import com.seleniumtests.customexception.ConfigurationException;
import com.seleniumtests.customexception.SeleniumRobotServerException;
import com.seleniumtests.reporter.logger.Snapshot;
import com.seleniumtests.reporter.logger.TestStep;
import com.seleniumtests.util.imaging.ImageProcessor;
import com.seleniumtests.util.imaging.StepReferenceComparator;
import com.seleniumtests.util.logging.SeleniumRobotLogger;

/**
 * Class that may help the tester to know what if the cause of test failure
 * - we were not on the right page when performing the last step
 * - we were on the right page, but an error occured
 * 		- error message is displayed
 * 		- a field is in error
 * - we were on the right page, but this page has slightly changed and a new field may have appeared
 * 
 * Some other errors may be found using network data (probably using selenium 4 features)
 * - some resources took too long to display
 * - error loading resources (HTTP code different from 2xx or 3xx)
 * 
 * @author S047432
 *
 */
public class ErrorCauseFinder {
	
	public static final String CLASS_ERROR_FIELD = "error_field";

	public static final String CLASS_ERROR_MESSAGE = "error_message";

	// TODO: link to RootCause and RootCause details from the Step annotation which can indicate what is the root cause of this error (declarative for a step)

	private static final String[] ERROR_WORDS = new String[] {"error", "erreur", "problem", "problème"};

	private static final Logger logger = SeleniumRobotLogger.getLogger(ErrorCauseFinder.class);
	
	private ITestResult testResult;
	
	
	
	
	public ErrorCauseFinder(ITestResult testResult) {
		this.testResult = testResult;
	}
	
	/**
	 * Try to find what caused the test to fail
	 * @return
	 */
	public List findErrorCause() {
		
		
		
		List causes = new ArrayList<>();
		
		causes.addAll(findErrorInLastStepSnapshots());
		causes.addAll(compareStepInErrorWithReference());
		
		logger.info(String.format("Found %d causes of error", causes.size()));
		
		return causes;
	}
	
	/**
	 * Compare the failed step with it's reference that can be found on seleniumRobot server
	 * If reference cannot be found, skip this step
	 * @return
	 */
	public List compareStepInErrorWithReference() {
		logger.info("Searching causes: comparing with references");
		List causes = new ArrayList<>();
		
		// do not seearch again
		if (TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult)) {
			return causes; 
		}
		
		// don't analyze if result has not been recorded on seleniumRobot server
		TestStepManager testStepManager = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager();
		if (testStepManager.getLastTestStep() == null || testStepManager.getLastTestStep().getStepResultId() == null) {
			return causes;
		}
		
		for (TestStep testStep: testStepManager.getTestSteps()) {

			Integer stepResultId = testStep.getStepResultId(); // stepResultId is set when step recording is done on server
			if (Boolean.TRUE.equals(testStep.getFailed()) && !(testStep.getActionException() instanceof AssertionError) && stepResultId != null) {
				
				try {
					Snapshot stepSnapshot = testStep.getSnapshots()
						.stream()
						.filter(s -> s.getCheckSnapshot().recordSnapshotOnServerForReference())
						.collect(Collectors.toList()).get(0);
					File stepSnapshotFile = new File(stepSnapshot.getScreenshot().getFullImagePath());
					
					File referenceSnapshot = SeleniumRobotSnapshotServerConnector.getInstance().getReferenceSnapshot(stepResultId);
					if (referenceSnapshot == null) {
						continue;
					}
					
					// perform a match between the picture of this step and the reference stored on server
					// We look at presence, position and text of each field
					List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy