Skip to content

Commit

Permalink
Merge pull request #590 from hyunil-shin/print_html_path
Browse files Browse the repository at this point in the history
Print path to html in error message if savePageSource is true.
  • Loading branch information
BorisOsipov committed Sep 18, 2017
2 parents 3767b9c + a3e84f3 commit f25a2b3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/codeborne/selenide/ex/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ public static String screenshot(String screenshotPath) {
if (!Configuration.screenshots) {
return "";
}
return "\nScreenshot: " + screenshotPath;

if (screenshotPath == null || screenshotPath.isEmpty()) {
return "\nScreenshot: " + screenshotPath;
}

if (Configuration.savePageSource) {
String htmlFilePath = getHtmlFilePath(screenshotPath);
return "\nScreenshot: " + screenshotPath + "\nPage source: " + htmlFilePath;
} else {
return "\nScreenshot: " + screenshotPath;
}
}

public static String causedBy(Throwable cause) {
Expand All @@ -58,4 +68,8 @@ public static String jsErrors(List<String> jsErrors) {
}
return "\nJavascript Errors: " + jsErrors;
}

private static String getHtmlFilePath(String screenshotPath) {
return screenshotPath.substring(0, screenshotPath.lastIndexOf('.')) + ".html";
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/codeborne/selenide/ex/ErrorMessagesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void rememberOldValues() {
@AfterClass
public static void restoreOldValues() {
Configuration.screenshots = true;
Configuration.savePageSource = true;
Configuration.reportsUrl = reportsUrl;
screenshots = new ScreenShotLaboratory();
}
Expand All @@ -35,6 +36,7 @@ public void setUp() {
Configuration.screenshots = true;
screenshots = mock(ScreenShotLaboratory.class);
doCallRealMethod().when(screenshots).formatScreenShotPath();
Configuration.savePageSource = false;
}

@Test
Expand Down Expand Up @@ -81,4 +83,16 @@ public void doesNotAddScreenshot_if_screenshotsAreDisabled() {
assertEquals("", screenshot);
verify(screenshots, never()).takeScreenShot();
}

@Test
public void printHtmlPath_if_savePageSourceIsEnabled() {
Configuration.savePageSource = true;
Configuration.reportsUrl = "http://ci.mycompany.com/job/666/artifact/";
String currentDir = System.getProperty("user.dir");
doReturn(currentDir + "/test-result/12345.png").when(screenshots).takeScreenShot();

String screenshot = ErrorMessages.screenshot();
assertEquals("\nScreenshot: http://ci.mycompany.com/job/666/artifact/test-result/12345.png"
+ "\nPage source: http://ci.mycompany.com/job/666/artifact/test-result/12345.html", screenshot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void elementNotFound() {
assertStartsWith("Element not found {h9}\n" +
"Expected: text 'expected text'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.\n" +
"Caused by: NoSuchElementException:", expected);
assertEquals("http://ci.org/build/reports/tests/" + browser + "/1.jpg", expected.getScreenshot());
Expand All @@ -69,6 +70,7 @@ public void elementTextDoesNotMatch() {
assertEquals("Element should have text 'expected text' {h2}\n" +
"Element: '<h2>Dropdown list</h2>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", expected.toString());
assertEquals("http://ci.org/build/reports/tests/" + browser + "/1.jpg", expected.getScreenshot());
}
Expand All @@ -83,6 +85,7 @@ public void elementAttributeDoesNotMatch() {
assertEquals("Element should have attribute name=header {h2}\n" +
"Element: '<h2>Dropdown list</h2>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", expected.toString());
}
}
Expand All @@ -97,6 +100,7 @@ public void wrapperTextDoesNotMatch() {
assertEquals("Element should have text 'expected text' {By.tagName: h2}\n" +
"Element: '<h2>Dropdown list</h2>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", expected.toString());
}
}
Expand All @@ -110,6 +114,7 @@ public void clickHiddenElement() {
assertEquals("Element should be visible {#theHiddenElement}\n" +
"Element: '<div id=\"theHiddenElement\" displayed:false></div>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", elementShouldExist.toString());
assertEquals("http://ci.org/build/reports/tests/" + browser + "/1.jpg", elementShouldExist.getScreenshot());
}
Expand All @@ -124,6 +129,7 @@ public void pageObjectElementTextDoesNotMatch() {
assertEquals("Element should have text 'expected text' {By.tagName: h2}\n" +
"Element: '<h2>Dropdown list</h2>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", expected.toString());
}
}
Expand All @@ -137,6 +143,7 @@ public void pageObjectWrapperTextDoesNotMatch() {
assertEquals("Element should have text 'expected text' {By.tagName: h2}\n" +
"Element: '<h2>Dropdown list</h2>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", expected.toString());
}
}
Expand All @@ -159,6 +166,7 @@ public void clickUnexistingWrappedElement() {
assertStartsWith("Element not found {By.id: invalid_id}\n" +
"Expected: visible\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.\n" +
"Caused by: NoSuchElementException:", e);
}
Expand All @@ -178,6 +186,7 @@ public void existingElementShouldNotExist() {
assertEquals("Element should not exist {h2}\n" +
"Element: '<h2>Dropdown list</h2>'\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.", e.toString());
}
}
Expand All @@ -191,6 +200,7 @@ public void nonExistingElementShouldNotBeHidden() {
assertStartsWith("Element not found {h14}\n" +
"Expected: not hidden\n" +
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n" +
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n" +
"Timeout: 1.500 s.\n" +
"Caused by: NoSuchElementException:", e);
}
Expand All @@ -206,7 +216,8 @@ public void clickingNonClickableElement() {
} catch (UIAssertionError e) {
assertContains(e, "is not clickable at point",
"Other element would receive the click",
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg");
"Screenshot: http://ci.org/build/reports/tests/" + browser + "/1.jpg\n",
"Page source: http://ci.org/build/reports/tests/" + browser + "/1.html\n");
}
}

Expand Down

0 comments on commit f25a2b3

Please sign in to comment.