in Education by
I have written tests with Selenium2/WebDriver and want to test if HTTP Request returns an HTTP 403 Forbidden. Is it possible to get the HTTP response status code with Selenium WebDriver? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
It is doable to get the response code of a HTTP protocol request using Selenium and Chrome or Firefox. All you've got to try is begin either Chrome or Firefox in work mode. I will show you some examples below. java + Se + Chrome Here is AN example of java + Se + Chrome, but I guess that it can be done in any language (python, c#, ...). All you need to do is tell ChromeDriver to do "Network.enable". This can be done by enabling Performance logging. LoggingPreferences logPrefs = new LoggingPreferences(); logPrefs.enable(LogType.PERFORMANCE, Level.ALL); cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs); After the request is done, all you have to do is get and iterate the perfomance logs and find "Network.responseReceived" for the requested URL: LogEntries logs = driver.manage().logs().get("performance"); Here is the code: import java.util.Iterator; import java.util.logging.Level; import org.json.JSONException; import org.json.JSONObject; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.logging.LogEntries; import org.openqa.selenium.logging.LogEntry; import org.openqa.selenium.logging.LogType; import org.openqa.selenium.logging.LoggingPreferences; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class TestResponseCode { public static void main(String[] args) { // simple page (without many resources so that the output is // easy to understand String url = "https://intellipaat.com/"; DownloadPage(url); } private static void DownloadPage(String url) { ChromeDriver driver = null; try { ChromeOptions options = new ChromeOptions(); // add whatever extensions you need // for example I needed one of adding proxy, and one for blocking // images // options.addExtensions(new File(file, "proxy.zip")); // options.addExtensions(new File("extensions", // "Block-image_v1.1.crx")); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(ChromeOptions.CAPABILITY, options); // set performance logger // this sends Network.enable to chromedriver LoggingPreferences logPrefs = new LoggingPreferences(); logPrefs.enable(LogType.PERFORMANCE, Level.ALL); cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs); driver = new ChromeDriver(cap); // navigate to the page System.out.println("Navigate to " + url); driver.navigate().to(url); // and capture the last recorded url (it may be a redirect, or the // original url) String currentURL = driver.getCurrentUrl(); // then ask for all the performance logs from this request // one of them will contain the Network.responseReceived method // and we shall find the "last recorded url" response LogEntries logs = driver.manage().logs().get("performance"); int status = -1; System.out.println("\nList of log entries:\n"); for (Iterator it = logs.iterator(); it.hasNext();) { LogEntry entry = it.next(); try { JSONObject json = new JSONObject(entry.getMessage()); System.out.println(json.toString()); JSONObject message = json.getJSONObject("message"); String method = message.getString("method"); if (method != null && "Network.responseReceived".equals(method)) { JSONObject params = message.getJSONObject("params"); JSONObject response = params.getJSONObject("response"); String messageUrl = response.getString("url"); if (currentURL.equals(messageUrl)) { status = response.getInt("status"); System.out.println( "---------- returned response for " + messageUrl + ": " + status); System.out.println( "---------- headers: " + response.get("headers")); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("\nstatus code: " + status); } finally { if (driver != null) { driver.quit(); } } } Hope this helps! Also, you can refer to our Selenium tutorial page for other related queries To learn in-depth about Selenium, sign up for an industry based Selenium course.

Related questions

0 votes
    I'm working on a Java Selenium-WebDriver. I added driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); ... a better way? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How to open a new tab in the existing Firefox browser using Selenium WebDriver (a.k.a. Selenium 2)? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am converting my selenium 1 code to selenium 2 and can't find any easy way to select a label in a ... ("//path_to_drop_down"); Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    In the HTML of a web app, there is the following code What is actually shown on the page is a string displaying ... . Any help? Thanks. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I want to put value using webdriver, This is the command I am using driver.findElement(By.name("name")).sendKeys(" ... way to do so ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I use Selenium RC, for which the scroll command is selenium.getEval("scrollBy(0, 250)"); Now, I have started ... the command for it? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    We are using Selenium to automate our testing. Recently we have seen the majority of our users using Chrome. ... over Selenium? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a certain element that I can select with Selenium 1. Unfortunately, I need to click the parent element ... upwards with XPath? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am new in software testing and looking to enhance my knowledge. I want to know in detail about: What is ... answer here. Thanks! Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Could someone tell me whether Selenium is a good career option? Select the correct answer from above options...
asked Jan 6, 2022 in Education by JackTerrance
0 votes
    Which of the following code is used to get an attribute in a HTTP Session object in servlets? (a) session. ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I want to log in to instagram using selenium, but I can't seem to enter values into the fields. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I use Selenium RC, for which the scroll command is selenium.getEval("scrollBy(0, 250)"); Now, I ... questions and answers pdf, Verbal Reasoning interview questions for beginners...
asked Oct 30, 2021 in Education by JackTerrance
0 votes
    Page scroll up or down in Selenium WebDriver (Selenium 2) using java I use Selenium RC, for which the scroll command ... using Selenium WebDriver , what will be the command for it?...
asked Nov 20, 2020 in Education by Editorial Staff
0 votes
    I see this only in Chrome. The full error message reads: "org.openqa.selenium.WebDriverException: Element is not ... for scrolling. Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
...