2

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

2
  • check the link by using http request and responce so that we come to know that link is active or not 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! May 20, 2016 at 7:11

1 Answer 1

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;
        }


}
4
  • 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 issue
    – Ramya
    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 403
    – Ramya
    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 explain
    – Ramya
    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? May 25, 2016 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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