Skip to content

Commit

Permalink
issue #467: add a way to deactivate random name in snapshot
Browse files Browse the repository at this point in the history
to ease tests
  • Loading branch information
bhecquet committed Jan 11, 2022
1 parent 5780b67 commit d11fbee
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,18 @@ public class SeleniumTestsContext {
public static final String PLATFORM = "platform"; // platform on which test should execute. Ex: Windows 7, Android, iOS, Linux, OS X 10.10.
public static final String CLOUD_API_KEY = "cloudApiKey"; // clé d'accès (dépend des services)

public static final String TEST_NAME = "testName";
public static final String RELATIVE_OUTPUT_DIR = "relativeOutputDir";

// Neoload specific properties
public static final String NEOLOAD_USER_PATH = "neoloadUserPath"; // name of the neoload "user path" that will be created in Design mode

public static final String REPORTPORTAL_ACTIVE = "reportPortalActive"; // whether report portal is activated

// internal storage
// internal use
public static final String TEST_VARIABLES = "testVariables"; // configuration (aka variables, get via 'param()' method) used for the current test. It is not updated via XML file

public static final String TEST_NAME = "testName";
public static final String RELATIVE_OUTPUT_DIR = "relativeOutputDir";
public static final String RANDOM_IN_ATTACHMENT_NAME = "randomInAttachmentName"; // by default, snapshots are renamed with a random part so that if several steps have the same name, their snapshot do not overwrite.
// this option disables the behaviour FOR TEST PURPOSE

// default values
protected static final List<ReportInfo> DEFAULT_CUSTOM_TEST_REPORTS = Arrays.asList(new ReportInfo("PERF::xml::reporter/templates/report.perf.vm"));
protected static final List<ReportInfo> DEFAULT_CUSTOM_SUMMARY_REPORTS = Arrays.asList(new ReportInfo("results::json::reporter/templates/report.summary.json.vm"));
Expand Down Expand Up @@ -292,6 +293,7 @@ public class SeleniumTestsContext {
public static final String DEFAULT_BUGTRACKER_TYPE = null;
public static final String DEFAULT_STARTED_BY = null;
public static final boolean DEFAULT_REPORTPORTAL_ACTIVE = false;
public static final boolean DEFAULT_RANDOM_IN_ATTACHMENT_NAME = true;
public static final ElementInfo.Mode DEFAULT_ADVANCED_ELEMENT_SEARCH = ElementInfo.Mode.FALSE;
public static final String DEFAULT_IMAGE_FIELD_DETECTOR_SERVER_URL = null;
public static final boolean DEFAULT_EDGE_IE_MODE = false;
Expand Down Expand Up @@ -506,6 +508,8 @@ private void buildContextFromConfig() {

setNeoloadUserPath(getValueForTest(NEOLOAD_USER_PATH, System.getProperty(NEOLOAD_USER_PATH)));

setRandomInAttachmentNames(getBoolValueForTest(RANDOM_IN_ATTACHMENT_NAME, System.getProperty(RANDOM_IN_ATTACHMENT_NAME)));

//setReportPortalActive(getBoolValueForTest(REPORTPORTAL_ACTIVE, System.getProperty(REPORTPORTAL_ACTIVE)));

if (testNGContext != null) {
Expand Down Expand Up @@ -1446,6 +1450,10 @@ public Boolean getMaskedPassword() {
return (Boolean) getAttribute(MASK_PASSWORD);
}

public Boolean getRandomInAttachments() {
return (Boolean) getAttribute(RANDOM_IN_ATTACHMENT_NAME);
}

public ITestResult getTestNGResult() {
return testNGResult;
}
Expand Down Expand Up @@ -2170,6 +2178,14 @@ public void setMaskPassword(Boolean maskPassword) {
}
}

public void setRandomInAttachmentNames(Boolean random) {
if (random != null) {
setAttribute(RANDOM_IN_ATTACHMENT_NAME, random);
} else {
setAttribute(RANDOM_IN_ATTACHMENT_NAME, DEFAULT_RANDOM_IN_ATTACHMENT_NAME);
}
}

/**
* Record DEBUG
* also store an INTERNAL_DEBUG System property to be used internally with SeleniumRobotLogger class
Expand Down
25 changes: 23 additions & 2 deletions core/src/main/java/com/seleniumtests/reporter/logger/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class Snapshot extends TestAction {

private ScreenShot screenshot;
private SnapshotCheckType checkSnapshot; // whether this snapshot will be sent to Snapshot server to check if it conforms to baseline
private boolean displayInReport = true;

public static final String SNAPSHOT_PATTERN = "Application Snapshot";
public static final String OUTPUT_PATTERN = "Output '%s' browser: ";
Expand Down Expand Up @@ -145,8 +146,14 @@ private void renameImageFile(String newBaseName) {
// build file name with <base name>-<part of UUID>.html
// this way, even when test is repeated multiple times, snapshots will have different names
// (usefull for comments in bugtrackers where reference to new file should be different from previous ones)
String newName = String.format("%s-%s", newBaseName.substring(0, Math.min(50, newBaseName.length())),
String newName;
if (Boolean.TRUE.equals(SeleniumTestsContextManager.getThreadContext().getRandomInAttachments())) {
newName = String.format("%s-%s", newBaseName.substring(0, Math.min(50, newBaseName.length())),
oldFileName.substring(oldFileName.length() - 10));
} else {
newName = String.format("%s.%s", newBaseName.substring(0, Math.min(50, newBaseName.length())),
FilenameUtils.getExtension(oldFileName));
}

screenshot.setImagePath(folderName + newName);

Expand Down Expand Up @@ -174,8 +181,14 @@ private void renameHtmlSourceFile(String newBaseName) {
// build file name with <base name>-<part of UUID>.html
// this way, even when test is repeated multiple times, snapshots will have different names
// (usefull for comments in bugtrackers where reference to new file should be different from previous ones)
String newName = String.format("%s-%s", newBaseName.substring(0, Math.min(50, newBaseName.length())),
String newName;
if (Boolean.TRUE.equals(SeleniumTestsContextManager.getThreadContext().getRandomInAttachments())) {
newName = String.format("%s-%s", newBaseName.substring(0, Math.min(50, newBaseName.length())),
oldFileName.substring(oldFileName.length() - 10));
} else {
newName = String.format("%s.%s", newBaseName.substring(0, Math.min(50, newBaseName.length())),
FilenameUtils.getExtension(oldFileName));
}


// if file cannot be moved, go back to old name
Expand Down Expand Up @@ -230,4 +243,12 @@ public void setCheckSnapshot(SnapshotCheckType checkSnapshot) {
this.checkSnapshot = checkSnapshot;
}

public boolean isDisplayInReport() {
return displayInReport;
}

public void setDisplayInReport(boolean displayInReport) {
this.displayInReport = displayInReport;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,20 @@ public void testMaskPasswordNull(final ITestContext testNGCtx, final XmlTest xml
Assert.assertTrue(SeleniumTestsContextManager.getThreadContext().getMaskedPassword());
}

@Test(groups="ut context")
public void testRandmoInAttachment(final ITestContext testNGCtx, final XmlTest xmlTest) {
initThreadContext(testNGCtx);
SeleniumTestsContextManager.getThreadContext().setRandomInAttachmentNames(false);
Assert.assertFalse(SeleniumTestsContextManager.getThreadContext().getRandomInAttachments());
}
// by default, devMode is true if tests are launched from IDE
@Test(groups="ut context")
public void testRandmoInAttachmentNull(final ITestContext testNGCtx, final XmlTest xmlTest) {
initThreadContext(testNGCtx);
SeleniumTestsContextManager.getThreadContext().setRandomInAttachmentNames(null);
Assert.assertTrue(SeleniumTestsContextManager.getThreadContext().getRandomInAttachments());
}

@Test(groups="ut context")
public void testDebugCore(final ITestContext testNGCtx, final XmlTest xmlTest) {
try {
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/com/seleniumtests/ut/reporter/TestTestStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.testng.annotations.Test;

import com.seleniumtests.GenericTest;
import com.seleniumtests.core.SeleniumTestsContextManager;
import com.seleniumtests.core.Step.RootCause;
import com.seleniumtests.customexception.CustomSeleniumTestsException;
import com.seleniumtests.driver.screenshots.ScreenShot;
Expand Down Expand Up @@ -148,6 +149,36 @@ public void testSnapshotRenaming() throws IOException {
tmpImgFile.deleteOnExit();
tmpHtmlFile.deleteOnExit();
}

/**
* Check case where random part of attachment is removed
* @throws IOException
*/
@Test(groups = { "ut" })
public void testSnapshotRenamingNoRandom() throws IOException {
SeleniumTestsContextManager.getThreadContext().setRandomInAttachmentNames(false);
TestStep step = new TestStep("step1", null, new ArrayList<>(), true);
ScreenShot screenshot = new ScreenShot();

File tmpImgFile = File.createTempFile("img", ".png");
File tmpHtmlFile = File.createTempFile("html", ".html");

screenshot.setOutputDirectory(tmpImgFile.getParent());
screenshot.setLocation("http://mysite.com");
screenshot.setTitle("mysite");
screenshot.setImagePath(tmpImgFile.getName());
screenshot.setHtmlSourcePath(tmpHtmlFile.getName());

step.addSnapshot(new Snapshot(screenshot, "main", SnapshotCheckType.TRUE), 0, null);

Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getImagePath(),
"N-A_0-1_step1-.png");
Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getHtmlSourcePath(),
"N-A_0-1_step1-.html");

tmpImgFile.deleteOnExit();
tmpHtmlFile.deleteOnExit();
}

/**
* Test that when adding a snapshot to a test step, with a custom name, it's
Expand Down

0 comments on commit d11fbee

Please sign in to comment.