6

How to Capture HTTP errors on a web page such as page not found, 500 Internal server errors and customizing the message in webdriver.

2
  • 4
    What message do you want to customize?
    – Alexey R.
    May 29 '18 at 9:48
  • 1
    In relation with HTTP error as you mention in the question, what do you mean by "customizing the message in webdriver"? Mar 26 at 4:52
1

You can capture console Logs using the following code snippets:

For chrome only -

        LogEntries logEntries = _driver.manage().logs().get(LogType.BROWSER);
        List<LogEntry> logs = logEntries.getAll();
        logLength = logs.size();

You will need to import following classes:

import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
1

As alternative to looking at the http code, you could also look at the contents of a page. You would have to implement something where you actively check whether a new page has been opened, by checking for the presence of a specific element that is only shown on the expected page.

You could also define error pages, which have specific content such as text "Internal server error".

If the check for the expected page fails, you can then first check whether you are on one of the error pages. If this is the case, throw a specific exception, otherwise just throw the original exception.

This is a very high level description of how it's done in the framework Geb. More information here: http://www.gebish.org/manual/0.10.0/pages.html#unexpected_pages

You might need to read a little bit more about the so called 'At checking' in the framework in addition to just the item that I linked to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.