Skip to content

Commit

Permalink
issue #467: find error cause when flag is set to true
Browse files Browse the repository at this point in the history
  • Loading branch information
bhecquet committed Dec 15, 2021
1 parent 29263ad commit 2389d47
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class SeleniumTestsContext {
public static final String ADVANCED_ELEMENT_SEARCH = "advancedElementSearch"; // 'false' (default), 'full', 'dom'. if 'dom', store and possibly use found element information to adapt to changes in DOM, using only DOM information. If element is not found, it will try to use other element information to find it
// if 'full', search will also be done using element picture, if available

public static final String FIND_ERROR_CAUSE = "findErrorCause"; // if 'true', try to find why the test failed

public static final String IMAGE_FIELD_DETECTOR_SERVER_URL = "imageFieldDetectorServerUrl"; // URL of the server that can find fields in an image

Expand Down Expand Up @@ -261,6 +262,7 @@ public class SeleniumTestsContext {
public static final boolean DEFAULT_MANUAL_TEST_STEPS = false;
public static final boolean DEFAULT_HEADLESS_BROWSER = false;
public static final boolean DEFAULT_MASK_PASSWORD = true;
public static final boolean DEFAULT_FIND_ERROR_CAUSE = false;
public static final String DEFAULT_RUN_MODE = "LOCAL";
public static final boolean DEFAULT_OVERRIDE_SELENIUM_NATIVE_ACTION = false;
public static final boolean DEFAULT_SELENIUMROBOTSERVER_RECORD_RESULTS = false;
Expand Down Expand Up @@ -389,6 +391,8 @@ private void buildContextFromConfig() {
setSeleniumRobotServerRecordResults(getBoolValueForTest(SELENIUMROBOTSERVER_RECORD_RESULTS, System.getProperty(SELENIUMROBOTSERVER_RECORD_RESULTS)));
setSeleniumRobotServerVariableOlderThan(getIntValueForTest(SELENIUMROBOTSERVER_VARIABLES_OLDER_THAN, System.getProperty(SELENIUMROBOTSERVER_VARIABLES_OLDER_THAN)));

setFindErrorCause(getBoolValueForTest(FIND_ERROR_CAUSE, System.getProperty(FIND_ERROR_CAUSE)));

setTmsType(getValueForTest(TMS_TYPE, System.getProperty(TMS_TYPE)));
setTmsUrl(getValueForTest(TMS_URL, System.getProperty(TMS_URL)));
setTmsUser(getValueForTest(TMS_USER, System.getProperty(TMS_USER)));
Expand Down Expand Up @@ -1624,6 +1628,10 @@ public boolean isHeadlessBrowser() {
public boolean isManualTestSteps() {
return (Boolean) getAttribute(MANUAL_TEST_STEPS);
}

public boolean isFindErrorCause() {
return (Boolean) getAttribute(FIND_ERROR_CAUSE);
}

public Map<String, String> getDeviceList() {
HashMap<String, String> deviceList = new HashMap<>();
Expand Down Expand Up @@ -1927,6 +1935,14 @@ public void setAdvancedElementSearch(String advanceElementSearchMode) {
}
}

public void setFindErrorCause(Boolean active) {
if (active != null) {
setAttribute(FIND_ERROR_CAUSE, active);
} else {
setAttribute(FIND_ERROR_CAUSE, DEFAULT_FIND_ERROR_CAUSE);
}
}


public void setReportPortalActive(Boolean active) {
if (active != null) {
Expand Down Expand Up @@ -2011,9 +2027,9 @@ public void setSeleniumRobotServerCompareSnapshotTtl(Integer timeToLive) {
}
}

public void setSeleniumRobotServerRecordResults(Boolean record) {
if (record != null) {
setAttribute(SELENIUMROBOTSERVER_RECORD_RESULTS, record);
public void setSeleniumRobotServerRecordResults(Boolean recordResult) {
if (recordResult != null) {
setAttribute(SELENIUMROBOTSERVER_RECORD_RESULTS, recordResult);
} else {
setAttribute(SELENIUMROBOTSERVER_RECORD_RESULTS, DEFAULT_SELENIUMROBOTSERVER_RECORD_RESULTS);
}
Expand All @@ -2023,9 +2039,9 @@ public void setVariableAlreadyRequestedFromServer(Map<String, TestVariable> vari
this.variableAlreadyRequestedFromServer = variableAlreadyRequestedFromServer;
}

public void setOverrideSeleniumNativeAction(Boolean record) {
if (record != null) {
setAttribute(OVERRIDE_SELENIUM_NATIVE_ACTION, record);
public void setOverrideSeleniumNativeAction(Boolean override) {
if (override != null) {
setAttribute(OVERRIDE_SELENIUM_NATIVE_ACTION, override);
} else {
setAttribute(OVERRIDE_SELENIUM_NATIVE_ACTION, DEFAULT_OVERRIDE_SELENIUM_NATIVE_ACTION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String
}
}


if (!suiteFinished) {
// find error causes once tests are finished
if (suiteFinished && SeleniumTestsContextManager.getGlobalContext().isFindErrorCause()) {
findErrorCauses(resultSet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,33 @@
*/
package com.seleniumtests.it.reporter;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite.ParallelMode;

import com.seleniumtests.core.SeleniumTestsContext;
import com.seleniumtests.core.SeleniumTestsContextManager;
import com.seleniumtests.core.testanalysis.ErrorCauseFinder;
import com.seleniumtests.reporter.reporters.ReporterControler;

@PrepareForTest({ErrorCauseFinder.class, ReporterControler.class, SeleniumTestsContext.class})
public class TestReporterControler extends ReporterTest {


@Mock
private ErrorCauseFinder errorCauseFinder;

/**
* Check that files created by robot but not integrated to tests are deleted
*
Expand Down Expand Up @@ -240,4 +252,103 @@ public void testReportDetailsOnlyTestConfigurationSteps() throws Exception {
Assert.assertTrue(detailedReportContent3.contains("<div class=\"message-info\">before count: 2</div>"));
Assert.assertTrue(detailedReportContent3.contains("<div class=\"message-info\">after count: 3</div>"));
}

/**
* Check we try to find error cause when
* - findErrorCause=true
* - seleniumServer is active
* - result recording is active (meaning we also record step references)
* @throws Exception
*/
@Test(groups={"it"})
public void testErrorCauseSearched() throws Exception {

try {
System.setProperty(SeleniumTestsContext.FIND_ERROR_CAUSE, "true");
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE, "true");
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL, SERVER_URL);
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS, "true");

PowerMockito.whenNew(ErrorCauseFinder.class).withAnyArguments().thenReturn(errorCauseFinder);
configureMockedSnapshotServerConnection();

executeSubTest(1, new String[] {"com.seleniumtests.it.stubclasses.StubTestClassForDriverTest"}, ParallelMode.METHODS, new String[] {"testDriverNativeActions"});

// we search only once for each test result, at the end of test suite
verify(errorCauseFinder).findErrorCause();

} finally {
System.clearProperty(SeleniumTestsContext.FIND_ERROR_CAUSE);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS);
}

}

/**
* Check we do not try to find error cause when
* - findErrorCause=false
* - seleniumServer is active
* - result recording is active (meaning we also record step references)
* @throws Exception
*/
@Test(groups={"it"})
public void testErrorCauseNotSearchedFlagFalse() throws Exception {

try {
System.setProperty(SeleniumTestsContext.FIND_ERROR_CAUSE, "false");
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE, "true");
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL, SERVER_URL);
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS, "true");

PowerMockito.whenNew(ErrorCauseFinder.class).withAnyArguments().thenReturn(errorCauseFinder);
configureMockedSnapshotServerConnection();

executeSubTest(1, new String[] {"com.seleniumtests.it.stubclasses.StubTestClassForDriverTest"}, ParallelMode.METHODS, new String[] {"testDriverNativeActions"});

// we search only once for each test result, at the end of test suite
verify(errorCauseFinder, never()).findErrorCause();

} finally {
System.clearProperty(SeleniumTestsContext.FIND_ERROR_CAUSE);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS);
}

}

/**
* Check we do not try to find error cause when
* - findErrorCause=true
* - seleniumServer is active
* - result recording is inactive
* @throws Exception
*/
@Test(groups={"it"})
public void testErrorCauseNotSearchedNoRecordResult() throws Exception {

try {
System.setProperty(SeleniumTestsContext.FIND_ERROR_CAUSE, "false");
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE, "true");
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL, SERVER_URL);
System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS, "false");

PowerMockito.whenNew(ErrorCauseFinder.class).withAnyArguments().thenReturn(errorCauseFinder);
configureMockedSnapshotServerConnection();

executeSubTest(1, new String[] {"com.seleniumtests.it.stubclasses.StubTestClassForDriverTest"}, ParallelMode.METHODS, new String[] {"testDriverNativeActions"});

// we search only once for each test result, at the end of test suite
verify(errorCauseFinder, never()).findErrorCause();

} finally {
System.clearProperty(SeleniumTestsContext.FIND_ERROR_CAUSE);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL);
System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,19 @@ public void testManualStepsNull(final ITestContext testNGCtx, final XmlTest xmlT
Assert.assertFalse(SeleniumTestsContextManager.getThreadContext().isManualTestSteps());
}

@Test(groups="ut context")
public void testFindErrorCause(final ITestContext testNGCtx, final XmlTest xmlTest) {
initThreadContext(testNGCtx);
SeleniumTestsContextManager.getThreadContext().setFindErrorCause(true);
Assert.assertTrue(SeleniumTestsContextManager.getThreadContext().isFindErrorCause());
}
@Test(groups="ut context")
public void testFindErrorCauseNull(final ITestContext testNGCtx, final XmlTest xmlTest) {
initThreadContext(testNGCtx);
SeleniumTestsContextManager.getThreadContext().setFindErrorCause(null);
Assert.assertFalse(SeleniumTestsContextManager.getThreadContext().isFindErrorCause());
}

@Test(groups="ut context")
public void testCustomTestsReports(final ITestContext testNGCtx, final XmlTest xmlTest) {
initThreadContext(testNGCtx);
Expand Down

0 comments on commit 2389d47

Please sign in to comment.