Prev |  | Next

## 6.9. HTML2PDF - Does the Rendering Tool Work Correct?

### Initial Situation

A web application creates dynamic web pages and additionally it provides the possibility to download the current web page as a PDF document. The PDF generation is done by rendering the HTML page with an appropriate tool.

### Problem

How do you know that the complete content of the HTML page is also present in the generated PDF? Do you know the boundary conditions required by the rendering tool? Does your HTML meets these requirements?

### Solution Approach

The test starts by reading the HTML page using Selenium. Text is extracted with Selenium and stored in local variables.

Then the PDF generation is started through the web page and the resulting document is selected again with Selenium. Finally the stored text can be used in PDFUnit test methods.

### Solution

    /**
     * This sample shows how to test an HTML page with Selenium, then let it be rendered
     * by the server to PDF and verify that content also appears in PDF.
     *
     * @author Carsten Siedentop, February 2013
     */
    public class Html2PDFTest {

      private WebDriver driver;

      @Test
      public void testHtml2PDFRenderer_WikipediaSeleniumEnglish() throws Exception {
        String urlWikipediaSelenium = "http://en.wikipedia.org/wiki/Selenium_%28software%29";
        driver.get(urlWikipediaSelenium);

        String section1  = "History";
        String section2  = "Components";
        String section3  = "The Selenium ecosystem";
        String section4  = "References";
        String section5  = "External links";

        assertLinkPresent(section1);
        assertLinkPresent(section2);
        assertLinkPresent(section3);
        assertLinkPresent(section4);
        assertLinkPresent(section5);

        String linkName = "Download as PDF";
        URL url = loadPDF(linkName);

        AssertThat.document(url)
                  .restrictedTo(ANY_PAGE)
                  .hasText()
                  .containing(section1,  WhitespaceProcessing.IGNORE)
                  .containing(section2,  WhitespaceProcessing.IGNORE)
                  .containing(section3,  WhitespaceProcessing.IGNORE)
                  .containing(section4,  WhitespaceProcessing.IGNORE)
                  .containing(section5,  WhitespaceProcessing.IGNORE)
        ;
      }

      private void assertLinkPresent(String partOfLinkText) {
        driver.findElement(By.xpath("//a[./span = '" + partOfLinkText + "']"));
      }

      private URL loadPDF(String linkName_LoadAsPDF) throws Exception {
        driver.findElement(By.linkText(linkName_LoadAsPDF)).click();
        String title = "Rendering finished - Wikipedia, the free encyclopedia";
        assertEquals(title, driver.getTitle());
        WebElement element = driver.findElement(By.linkText("Download the file"));
        String hrefValue = element.getAttribute("href");
        URL url = new URL(hrefValue);
        return url;
      }

      @After
      public void tearDown() throws Exception {
        driver.quit();
      }

      @Before
      public void createDriver() throws Exception {
        driver = new HtmlUnitDriver();
      }

    }

Prev | Up | Next
6.8. PDF Documents on Web Sites | Home | 6.10. Validate PDF as Mail Attachment