Search in sources :

Example 6 with SakuliExceptionWithScreenshot

use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.

the class ScreenshotDivConverter method extractScreenshotAsBase64.

protected String extractScreenshotAsBase64(Throwable exception) {
    if (exception instanceof SakuliExceptionWithScreenshot) {
        Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot();
        if (screenshotPath != null) {
            try {
                byte[] binaryScreenshot = Files.readAllBytes(screenshotPath);
                String base64String = new BASE64Encoder().encode(binaryScreenshot);
                for (String newLine : Arrays.asList("\n", "\r")) {
                    base64String = StringUtils.remove(base64String, newLine);
                }
                return base64String;
            } catch (IOException e) {
                exceptionHandler.handleException(new SakuliForwarderException(e, String.format("error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString())));
            }
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) BASE64Encoder(sun.misc.BASE64Encoder) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) IOException(java.io.IOException)

Example 7 with SakuliExceptionWithScreenshot

use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.

the class DaoTestCaseImplTest method testGetScreenshotAsSqlLobValue.

@Test
public void testGetScreenshotAsSqlLobValue() throws Exception {
    Path screenshotPath = Paths.get(this.getClass().getResource("computer.png").toURI());
    TestCase testCase = new TestCaseExampleBuilder().withException(new SakuliExceptionWithScreenshot("test-exception", screenshotPath)).buildExample();
    SqlLobValue result = testling.getScreenshotAsSqlLobValue(testCase);
    assertNotNull(result);
}
Also used : Path(java.nio.file.Path) SqlLobValue(org.springframework.jdbc.core.support.SqlLobValue) TestCase(org.sakuli.datamodel.TestCase) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) Test(org.testng.annotations.Test)

Example 8 with SakuliExceptionWithScreenshot

use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.

the class NagiosOutputBuilderTest method testFormatTestCaseTableStateMessageWithScreenshot.

@Test
public void testFormatTestCaseTableStateMessageWithScreenshot() throws Exception {
    Path screenshotPath = Paths.get(NagiosOutputBuilder.class.getResource("computer.png").toURI());
    String htmlTemplate = "<tr valign=\"top\"><td class=\"%s\">%s<\\/td><\\/tr>";
    GearmanProperties properties = MonitoringPropertiesTestHelper.initMock(mock(GearmanProperties.class));
    TestCase testCase = new TestCaseExampleBuilder().withState(TestCaseState.ERRORS).withId("case-error").withException(new SakuliExceptionWithScreenshot("EXCEPTION-MESSAGE", screenshotPath)).buildExample();
    String regex = String.format(htmlTemplate, "serviceCRITICAL", "\\[CRIT\\] case \"case-error\" EXCEPTION: EXCEPTION-MESSAGE" + "<div.* src=\"data:image\\/png;base64,.*><\\/div>");
    BaseTest.assertRegExMatch(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), regex);
}
Also used : Path(java.nio.file.Path) GearmanProperties(org.sakuli.services.forwarder.gearman.GearmanProperties) TestCase(org.sakuli.datamodel.TestCase) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) TestCaseExampleBuilder(org.sakuli.builder.TestCaseExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Example 9 with SakuliExceptionWithScreenshot

use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.

the class NagiosOutputBuilderTest method testFormatTestSuiteTableExceptionWithScreenshot.

@Test
public void testFormatTestSuiteTableExceptionWithScreenshot() throws Exception {
    Path screenshotPath = Paths.get(NagiosOutputBuilder.class.getResource("computer.png").toURI());
    Date startDate = new Date();
    TestSuite testSuiteExample = new TestSuiteExampleBuilder().withId("sakuli-123").withState(TestSuiteState.ERRORS).withStartDate(startDate).withException(new SakuliExceptionWithScreenshot("TEST-ERROR", screenshotPath)).withStopDate(DateUtils.addSeconds(startDate, 120)).buildExample();
    String result = testling.formatTestSuiteTableStateMessage(testSuiteExample, gearmanProperties.getTemplateSuiteTable());
    String lastRun = AbstractOutputBuilder.dateFormat.format(testSuiteExample.getStopDate());
    final String separator = "<div";
    assertEquals(result.substring(0, result.indexOf(separator)), "<tr valign=\"top\"><td class=\"serviceCRITICAL\">[CRIT] Sakuli suite \"sakuli-123\"" + " (120.00s) EXCEPTION: 'TEST-ERROR'. (Last suite run: " + lastRun + ")");
    String screenshotHash = result.substring(result.indexOf(ScreenshotDiv.DEFAULT_SAKULI_SCREENSHOT_DIV_ID) + ScreenshotDiv.DEFAULT_SAKULI_SCREENSHOT_DIV_ID.length());
    screenshotHash = screenshotHash.substring(0, screenshotHash.indexOf("\">"));
    String start_1 = "<div id=\"sakuli_screenshot" + screenshotHash + "\">" + "<div id=\"openModal_sakuli_screenshot" + screenshotHash + "\" class=\"modalDialog\">" + "<a href=\"#close\" title=\"Close\" class=\"close\">Close X</a>" + "<a href=\"#openModal_sakuli_screenshot" + screenshotHash + "\"><img class=\"screenshot\" src=\"";
    String start = start_1 + "data:image/png;base64,";
    String end = "</a></div></div></td></tr>";
    String substring = result.substring(result.indexOf(separator));
    assertEquals(substring.substring(0, start.length()), start);
    assertEquals(substring.substring(substring.length() - end.length()), end);
    //now check the remove function
    String resultWithOutBase64Data = ScreenshotDivConverter.removeBase64ImageDataString(substring);
    assertEquals(resultWithOutBase64Data, start_1 + "\" >" + end);
}
Also used : Path(java.nio.file.Path) TestSuite(org.sakuli.datamodel.TestSuite) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) Date(java.util.Date) TestSuiteExampleBuilder(org.sakuli.builder.TestSuiteExampleBuilder) Test(org.testng.annotations.Test) BaseTest(org.sakuli.BaseTest)

Aggregations

Path (java.nio.file.Path)9 SakuliExceptionWithScreenshot (org.sakuli.exceptions.SakuliExceptionWithScreenshot)9 Test (org.testng.annotations.Test)8 TestCaseExampleBuilder (org.sakuli.builder.TestCaseExampleBuilder)5 TestCase (org.sakuli.datamodel.TestCase)5 BaseTest (org.sakuli.BaseTest)4 TestCaseStepExampleBuilder (org.sakuli.builder.TestCaseStepExampleBuilder)3 SakuliForwarderException (org.sakuli.exceptions.SakuliForwarderException)3 GearmanProperties (org.sakuli.services.forwarder.gearman.GearmanProperties)3 NoSuchFileException (java.nio.file.NoSuchFileException)2 ScreenshotDiv (org.sakuli.services.forwarder.gearman.model.ScreenshotDiv)2 SqlLobValue (org.springframework.jdbc.core.support.SqlLobValue)2 IOException (java.io.IOException)1 Date (java.util.Date)1 TestSuiteExampleBuilder (org.sakuli.builder.TestSuiteExampleBuilder)1 TestSuite (org.sakuli.datamodel.TestSuite)1 BASE64Encoder (sun.misc.BASE64Encoder)1