Package: pytest-pyppeteer
pytest-pyppeteerA plugin to runpyppeteerin pytest.InstallationYou can install pytest-pyppeteer viapip:$pipinstallpytest-pyppeteeror install the latest one on Github:pipinstallgit+https://github.com/luizyao/pytest-pyppeteer.gitQuickstartFor example,The Shawshank Redemptiondeserves a 9.0 or higher rating ondouban.com.fromdataclassesimportdataclass@dataclass(init=False)classElements:"""Collect locators of page objects, no matter XPath or CSS Selector."""# query inputquery="#inp-query"# search buttonapply=".inp-btn > input:nth-child(1)"# the first resultfirst_result="#root > div > div > div > div > div:nth-child(1) > div.item-root a.cover-link"# ratingrating="#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong"asyncdeftest_lifetimes(browser):page=awaitbrowser.new_page()awaitpage.goto("https://movie.douban.com/")awaitpage.type(Elements.query,"The Shawshank Redemption")awaitpage.click(Elements.apply)awaitpage.waitfor(Elements.first_result)awaitpage.click(Elements.first_result)awaitpage.waitfor(Elements.rating)rating=awaitpage.get_value(Elements.rating)assertfloat(rating)>=9.0UsageFixturesbrowserfixtureProvide anpyppeteer.browser.Browserinstance with a new methodnew_page(), likepyppeteer.browser.Browser.newPage(),new_page()could create apyppeteer.page.Pageinstance.But thepyppeteer.page.Pageinstance created bynew_page()has some new methods:MethodTypequery_locatorNewwaitforNewclickOverridetypeOverrideget_valueNewFor example, you can query an element by css or xpath in the same methodquery_locatorinstead of originalquerySelectorandxpath.More details check withpage.pyin the source code.browser_factoryfixtureProvide to create anpyppeteer.browser.Browserinstance.For example, query theThe Shawshank Redemption’s movie and book rating ondouban.comat the same time, then compare them.importasynciofromdataclassesimportdataclass@dataclassclassElements:query="#inp-query"apply=".inp-btn > input:nth-child(1)"@dataclassclassBookElements(Elements):url="https://book.douban.com/"result='(//*[@class="item-root"])[1]/a'rating="#interest_sectl > div > div.rating_self.clearfix > strong"@dataclassclassMovieElements(Elements):url="https://movie.douban.com/"result="#root > div > div > div > div > div:nth-child(1) > div.item-root a.cover-link"rating="#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong"asyncdefquery_rating(browser,name:str,elements:"Elements"):page=awaitbrowser.new_page()awaitpage.goto(elements.url)awaitpage.type(elements.query,name)awaitpage.click(elements.apply)awaitpage.waitfor(elements.result)awaitpage.click(elements.result)awaitpage.waitfor(elements.rating)rating=awaitpage.get_value(elements.rating)returnratingasyncdeftest_multiple_browsers(browser_factory):browser1=awaitbrowser_factory()browser2=awaitbrowser_factory()movie_rating,book_rating=awaitasyncio.gather(query_rating(browser1,"The Shawshank Redemption",MovieElements),query_rating(browser2,"The Shawshank Redemption",BookElements),)assertmovie_rating==book_ratingCommand line options--executable-pathYou can specify the Chromium or Chrome executable path. Otherwise I will use the default install path of Chrome in current platform.For other platforms, pyppeteer will downloads the recent version of Chromium when called first time. If you don’t prefer this behavior, you can specify an exact path by override this fixture:@pytest.fixture(scope="session")defexecutable_path(executable_path):returnexecutable_pathor"path/to/Chrome/or/Chromium"--headlessRun browser in headless mode.--argsAdditional args to pass to the browser instance.For example, specify a proxy:$pytest--argsproxy-server"localhost:5555,direct://"--argsproxy-bypass-list"192.0.0.1/8;10.0.0.1/8"Or by override theargsfixture:@pytest.fixture(scope="session")defargs(args)->List[str]:returnargs+["--proxy-server=localhost:5555,direct://","--proxy-bypass-list=192.0.0.1/8;10.0.0.1/8",]--window-sizeThe default browser size is 800*600, you can use this option to change this behavior:$pytest--window-size1200800--window-size 0 0means to starts the browser maximized.--slowSlow down the pyppeteer operate in milliseconds. Defaults to0.0.MarkersoptionsYou can override some command line options in the specified test.For example, auto-open a DevTools panel:importasyncioimportpytest@pytest.mark.options(devtools=True)asyncdeftest_marker(browser):awaitbrowser.new_page()awaitasyncio.sleep(2)LicenseDistributed under the terms of theMITlicense, pytest-pyppeteer is free and open source software.IssuesIf you encounter any problems, pleasefile an issuealong with a detailed description.