Capturing JavaScript error in Selenium

前端 未结 12 692
盖世英雄少女心
盖世英雄少女心 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:29

    You try including windows.onerror event in your page or enable the show error dialog box in IE options. If you choose the later in Se1 will hang. PS: This has been discussed here. Do a search.

    0 讨论(0)
  • 2020-12-02 08:30

    JSErrorCollector does the job.

    Once configured, it's a matter of:

    List<JavaScriptError> jsErrorList = JavaScriptError.readErrors(driver);
    
    0 讨论(0)
  • 2020-12-02 08:33

    Put this script on your page and then check in Selenium for the JSError:

    <script type="text/javascript">
        window.onerror=function(msg){
            $("body").attr("JSError",msg);
        }
    </script>
    
    0 讨论(0)
  • 2020-12-02 08:37

    I would like to iterate on the answer of jhanifen. Here is a javascript solution that does not depend on jQuery. It creates an invisible HTML list on the bottom of the page, which contians the errors.

    (function () {
        var ul = null;
        function createErrorList() {
            ul = document.createElement('ul');
            ul.setAttribute('id', 'js_error_list');
            ul.style.display = 'none';
            document.body.appendChild(ul);
        }
        window.onerror = function(msg){
            if (ul === null)
                createErrorList();
            var li = document.createElement("li");
            li.appendChild(document.createTextNode(msg));
            ul.appendChild(li);
        };
    })();
    
    0 讨论(0)
  • 2020-12-02 08:38

    I'm doing this to capture JavaScript errors:

    [TestCleanup]
    public void TestCleanup()
    {
        var errorStrings = new List<string> 
        { 
            "SyntaxError", 
            "EvalError", 
            "ReferenceError", 
            "RangeError", 
            "TypeError", 
            "URIError" 
        };
    
        var jsErrors = Driver.Manage().Logs.GetLog(LogType.Browser).Where(x => errorStrings.Any(e => x.Message.Contains(e)));
    
        if (jsErrors.Any())
        {
            Assert.Fail("JavaScript error(s):" + Environment.NewLine + jsErrors.Aggregate("", (s, entry) => s + entry.Message + Environment.NewLine));
        }
    }
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题