gfk-ba / senbot

Cucumber / Selenium framework in Java
MIT License
10 stars 11 forks source link

Save full HTML body on error #1

Closed nreijmersdal closed 11 years ago

nreijmersdal commented 11 years ago
Feature: Save HTML during fail
  In order to investigate failing tests cases
  As a Selenium Test Developer
  I want to see the full HTML body from the time the failing test case happened

  Scenario: Error in HTML document
    Given I have a open a HTML file with the text "error:" in it
     When I assert the text "error" is not in the document
     Then a screenshot including the HTML is saved to the test results directory
nreijmersdal commented 11 years ago

To analyse a flickering test I wrote a small log function to log the pageSource instead of fully implementing it in the framework. Lowering priority for now.


    @Then("^I should not see the text \"([^\"]*)\"$")
    public void I_should_not_see_the_text(String expectedText) throws Throwable {
        String pageSource = getWebDriver().getPageSource();
        if(pageSource.contains(expectedText)) {
            log(pageSource);
            fail("Found text I should not see: " + expectedText);
        }
    }

    public void log(String text) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
        String date = dateFormat.format( new Date() );
        File file = new File("target/test-results/error_" + date + ".log");

        try {
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.append(text);
            output.close();
        } catch (IOException e) {
        }
    }
joostschouten commented 11 years ago

We have SLF4J loggers using the log4j flavour available as well. Have a look at SeleniumManager how the private static Logger log is used.

nreijmersdal commented 11 years ago

Good idea.

Example of using the logger to log to a file: http://stackoverflow.com/questions/3967202/storing-log-into-log-file-using-slf4j-log4j We need to log to a file, because sometimes we want to zip and email the results, this is mainly if we use senbot as a monitoring tool in other cases the log can be viewed from the CI.