I have a link in Website which will redirect to PDF document after clicking I would like to check if PDF is loaded in the browser or not using selenium
I do not want to compare the text/content in the loaded PDF or checking the pdf extension in the URL.
Please let me know how to automate the above scenario appreciate the response ASAP
-
check the link by using http request and responce so that we come to know that link is active or notsameer joshi– sameer joshi2016年05月20日 07:09:24 +00:00Commented May 20, 2016 at 7:09
-
Well, if you don't want to assert the PDF content or the URL, you can take a screenshot and somehow compare it with an existing screenshot or image of the PDF opened in a browser!IAmMilinPatel– IAmMilinPatel2016年05月20日 07:11:43 +00:00Commented May 20, 2016 at 7:11
1 Answer 1
why don't you check normal http response. That will also work for a normal http page. Here is the code sample that you can use.
@Test
public void sampleTest(){ //test method
//your code
boolean abletoOpenPDF=linkResponse(driver.getCurrentUrl());
}
public static boolean linkResponse(String url){
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
-
Can you please explain me what the above does i am not aware of how this will work,Please explain me so that i will get to know if this will help me in solving my issueRamya– Ramya2016年05月25日 07:20:59 +00:00Commented May 25, 2016 at 7:20
-
i have tried the above code in my script but even the content is loading or not loading the i am getting the response code as 403Ramya– Ramya2016年05月25日 09:59:36 +00:00Commented May 25, 2016 at 9:59
-
How do i use the response code to know whether the url is loading or not i din't get that point please explainRamya– Ramya2016年05月25日 10:32:09 +00:00Commented May 25, 2016 at 10:32
-
Above code tries to open a connection with given URL and tries to check its response. For any successful connection website returns response code 200(HTTP_OK). If website refuses connection then it would return 403 or 404. You must have seen "404-Page Not found" error for some pages. I've just checked this code locally for pdf and it returning successful response. Could you please pass URL manually and see its response? Would you able to share links here so that I can have a look?abhijeet kanade– abhijeet kanade2016年05月25日 13:51:52 +00:00Commented May 25, 2016 at 13:51
Explore related questions
See similar questions with these tags.