Thong Nguyen
Thong Nguyen

Reputation: 291

Get HTML pape after Javascript loaded with webtest

So I run Pyramid application and have a .pug template with embedded JS which will run after page loaded.

extends base.pug

block subcontent
    .container
        h1 Homepage

block scripts
    script.
        window.onload = function() {
            //- Load some JS and build page
        }

Here is the test code with webtest, so is there any way that I can get resp.html after the code in window.onload ran. Does webtest support or do I have to use another library?

class TestHomeView:
    def test_home(self, view):
        resp = view.testapp.get('/', expect_errors=True, xhr=True)

        # Validate content
        assert 'd3-chart line-chart' in resp.html

Upvotes: 1

Views: 38

Answers (1)

Sergey
Sergey

Reputation: 12407

Webtest can not execute JavaScript - you'd need a full browser implementation inside your Python testing library, which is not really feasible.

I think for testing client-side JavaScript you need a separate suite of tests - there are a bunch of competing JS libraries for testing, such as Jest or Mocha. This would get you started: https://2021.stateofjs.com/en-US/libraries/testing/

Upvotes: 1

Related Questions