Capturing JavaScript error in Selenium

前端 未结 12 714
盖世英雄少女心
盖世英雄少女心 2020-12-02 08:15

Is there a way to capture errors occurring in the DOM in Selenium and probably flag the same as an error in the page?

To give a brief exam

12条回答
  •  攒了一身酷
    2020-12-02 08:42

    If you're using java, you're welcome to try this library comitted by me which allows to easily collect JS errors received in Chromedriver session, using annotations on test methods. It works on on JUnit5 with extended annotation, and on TestNG with a listener parsing the annotation. The annotation contains boolean values which let you decide whether you want to assert or log the found errors after test execution.

    JUnit5 example:

    @Test
    @JSErrorsCollectorJUnit
    void referenceErrorTest(TestInfo testInfo) throws InterruptedException {
    
        // Create a new instance of ChromeDriver.
        driver = new ChromeDriver();
    
        // Set your test name to point its ChromeDriver session in HashMap.
        JSErrorsDriverHolder.setDriverForTest(testInfo.getDisplayName(), driver);
    
        // Navigate to URL.
        driver.get("http://testjs.site88.net");
    
        // The click on the button in the test site should cause JS reference error.
        driver.findElement(By.name("testClickButton")).click();
        waitBeforeClosingBrowser();
    }
    

提交回复
热议问题