MCQs on Broken Links Handling in Selenium Java
Which HTTP status code typically indicates a broken link (404 Not Found)?
a) 200
b) 301
c) 404
d) 500
Answer: c) 404
Which Selenium WebDriver method can be used to retrieve the URL of a link element?
a) getHref()
b) getAttribute("href")
c) getLink()
d) getURL()
Answer: b) getAttribute("href")
What is the first step to check if a link is broken using Selenium?
a) Retrieve the URL of the link
b) Click on the link
c) Check the page title
d) Validate the link’s text
Answer: a) Retrieve the URL of the link
How can you check the HTTP status code of a URL in Java?
a) Use HttpURLConnection to send a request and get the response code
b) Use Selenium’s WebDriver to get the status code
c) Use URLConnection to read the content
d) Use Apache HttpClient directly in Selenium
Answer: a) Use HttpURLConnection to send a request and get the response code
Which Java class is commonly used to make HTTP requests and check link status codes?
a) HttpURLConnection
b) HttpClient
c) URLConnection
d) HttpRequest
Answer: a) HttpURLConnection
Which of the following is NOT a correct step when verifying if a link is broken using Selenium?
a) Retrieve the URL from the link element
b) Send a request to the URL using HttpURLConnection
c) Validate the response code to ensure it's 200
d) Check the link’s font color
Answer: d) Check the link’s font color
How do you handle multiple links on a web page to check for broken links?
a) Iterate through all link elements, retrieve their URLs, and check their status codes
b) Click on each link and check for any navigation errors
c) Use JavaScript to fetch all links and validate them
d) Check the number of links on the page
Answer: a) Iterate through all link elements, retrieve their URLs, and check their status codes
What is the purpose of the getResponseCode() method in HttpURLConnection?
a) To get the HTTP status code from the server response
b) To set the URL for the connection
c) To send a request to the server
d) To close the connection
Answer: a) To get the HTTP status code from the server response
Which code snippet correctly checks if a link is broken using Selenium and Java?
a)
URL url = new URL(linkElement.getAttribute("href"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
System.out.println("Broken link: " + url);
}
b)
HttpURLConnection connection = (HttpURLConnection) new URL(linkElement.getAttribute("href")).openConnection();
connection.setRequestMethod("POST");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
System.out.println("Valid link");
}
c)
URL url = new URL(linkElement.getAttribute("href"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
System.out.println("Response code: " + connection.getResponseCode());
d)
HttpURLConnection connection = (HttpURLConnection) new URL(linkElement.getAttribute("href")).openConnection();
connection.setRequestMethod("GET");
connection.disconnect();
Answer: a)
URL url = new URL(linkElement.getAttribute("href"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
System.out.println("Broken link: " + url);
}
What should be done if a link returns an HTTP status code of 500?
a) Log the link as broken and investigate server issues
b) Retry the link after a delay
c) Ignore the link as it is valid
d) Mark the link as valid and proceed
Answer: a) Log the link as broken and investigate server issues
Which Java class provides methods for making HTTP requests and handling responses?
a) HttpURLConnection
b) HttpClient
c) HttpRequest
d) URLConnection
Answer: a) HttpURLConnection
How do you verify that a link is active and not broken?
a) Ensure the HTTP response code is 200
b) Check if the link is clickable
c) Verify the link's text
d) Confirm the link's color
Answer: a) Ensure the HTTP response code is 200
Which status code would you expect if a link is temporarily moved to a different location?
a) 301
b) 404
c) 500
d) 503
Answer: a) 301
What is the typical response for a server error when checking a link?
a) 500 Internal Server Error
b) 400 Bad Request
c) 404 Not Found
d) 301 Moved Permanently
Answer: a) 500 Internal Server Error
When using Selenium to check for broken links, what additional information should be logged along with the status code?
a) The URL of the broken link
b) The link text
c) The link's parent element
d) The browser's version
Answer: a) The URL of the broken link
No comments:
Post a Comment