Search code examples
javascriptseleniumselenium-webdrivermonitoringsynthetic

Fetch console logs error code selenium js


the following is my code in selenium JS, at the last step, I want to fetch the error code from the browser console, I need to check, there shouldn't be any 504 error code in the browser console for the current page.

driver.get(M_URL)
    .then(() => {
        return driver.findElement(By.xpath('//input[@id="UserName"]'))
            .then(el => el.sendKeys(USERNAME));
    })
    .then(() => {
        return driver.findElement(By.xpath('//input[@id="Password"]'))
            .then(el => el.sendKeys(PASSWORD));
    })
    .then(() => {
        return driver.findElement(By.xpath('//button[text()="Login"]'))
            .then(el => el.click());
    })
    .then(() => {
    return driver.findElement(By.xpath('//h3[text()[contains(.,"Publisher")]]')).click()
        .then(() => log('Publisher page is rendered'));
    })
    .then(() => log('check 504 error code in browser console'));

Thanks in Advance!


Solution

  • Here is how can you do it

    require('chromedriver');
    
    const path = require('path');
    const wd = require('selenium-webdriver');
    const chrome = require('selenium-webdriver/chrome');
    
    var builder = new wd.Builder();
    var options = new chrome.Options();
    var prefs = new wd.logging.Preferences();
    var driver;
    
    prefs.setLevel(wd.logging.Type.BROWSER, wd.logging.Level.ALL);
    options.setLoggingPrefs(prefs);
    
    driver = builder
        .forBrowser(wd.Browser.CHROME)
        .setChromeOptions(options)
        .build();
    
    driver
        .get(`file://${path.resolve(__dirname, './page.html')}`)
        .then(() => driver.manage().logs().get(wd.logging.Type.BROWSER))
        .then((logs) => {
            console.log(logs);
        })
        .then(() => driver.quit());
    

    more about it here