Automation Testing using JSRerport



  • Hi Jsreport Team,

    We are working on POC for testing the generated templates, our end goal is to test the pdf generated by the JSReport template. We did see some examples in the node-test module to perform testing on the jsreport server. As it seems that there is no documentation provided on the internet to write the custom report test, we initiated to write it on our own.

    In the following example we are trying to connect the invoice template in the testing framework, and test that weather the template has been loaded or not, but we faced that the port number it was connecting to was generated randomly when we pass the param "loadConfig" to true.
    If we set the param "loadConfig" to false it will give us the permission error.
    the test file is located inside /test folder. and the example repository can be found at the following link : https://gitlab.com/simondshealth/public/jsreport-automation-testing-poc

    Can you please guide us based on the POC, it will really help us to perform detailed automation tests on the jsreport. If possible we would also like to have a help-guide to write different kind of test case examples.

    Thank you so much



  • With loadConfig:false the jsreport initializes with defaults and use just memory store. It doesn't load your templates at all.
    You don't want that. Remove the loadConfig.

    You've misread the error about the port. It was not jsreport http server port but port the templating engines use for internal load balancing of execution.
    It wouldn't normally crash, but your test is wrongly implemented. You use promise, but don't return one from the test. This means the test framework-mocha exits the test immediately and afterEach hook kills the execution in the middle. This is causing the actual port error.
    To fix it just add return at the top

    it('should have template', () => {
        return jsreport.render({
    

    Then the test will work properly. You will now probably need some asserts to test the output.
    You could use resp.content pdf buffer and pdf-parse module to convert it into text and then assert if it contains expected strings.



  • @jan_blaha Thank you so much, It worked, Can you suggest me a document or an artical which will be helpful to use to test jsreport outputs ?

    Now I have working solution where i can print the text from the specific template using combining jsreport with pdf render and pdf parse tools.

    For instance it prints all the information of the pdf but if i want to nerrow it down to the specific page, is it possible to do it using jsreport.render function ?

    So what i am trying to do here to achieve the total hands on parising of the generated pdf which will allow me to write the specific conditions on the code to match the text, if it matches then i can pass the result otherwise i can fail the result.

    In our jsreport example using handlebar js we have written some conditions which will print some extra pages / extra contents based on input data, this is the main reason why i am trying to achieve this.

    thank you again for your help and a quick support . :)



  • Also i would like to ask your suggestions on the procedure, as you know that there is much less community support available for testing jsreport with mocha pdf.

    1) Is it possible to get the entire pdf in html

    • The reason i am asking that i guess using the html version of the pdf will allow the java-script to find the elements in text easily, so the test-cases could be written seamlessly and accurately.
      2) Is the approach i am following is correct approach to test the pdf generated through the jsreport ?
      3) Do you have any suggestions regarding the workflow ?

    Here is the basic code that i have used to render data from the jsreport :

    it('should have template', () => {
    return jsreport.render({
    template: { pageRanges : 1-3, name: 'invoice' },
    data: { }
    }).then((resp) => {
    pdf(resp.content).then(function (data) {
    console.log("===========================================================")
    console.log("Number of Pages : "+data.numpages)
    console.log("===========================================================")
    console.log("Rendering Page : "+data.numrender)
    console.log("===========================================================")
    console.log("Basic Information : "+data.info)
    console.log("===========================================================")
    console.log("Metadata : "+data.metadata)
    console.log("===========================================================")
    console.log("Version : "+data.version)
    console.log("===========================================================")
    console.log("Text : "+data.text)
    });
    }).catch((e) => {
    console.error(e)
    })
    })



  • You should be able to parse the pdf and get back text on specific pages.
    We do it in the pdf-utils unit tests. See it here
    https://github.com/jsreport/jsreport-pdf-utils/blob/master/lib/utils/parsePdf.js
    https://github.com/jsreport/jsreport-pdf-utils/blob/master/test/test.js#L55


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.