Road to verify 200 response code of web application in java webdriver

This post will help to verify HTTP response code 200 of web application using java webdriver. As webdriver does not support direct any function to verify page response code. But using "WebClient" of HtmlUnit API we can achieve this.

Html unit API is GUI less browser for java developer, using WebClent class we can send request  to application server and verify response header status.

Below code, I used in my webdriver script to verify response 200 of web application

 String url = "http://www.google.com/";
 WebClient webClient = new WebClient();
 HtmlPage htmlPage = webClient.getPage(url);

 //verify response
 Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCode());
 Assert.assertEquals("OK",htmlPage.getWebResponse().getStatusMessage());

If HTTP authentication is required in web application use below code.


 String url = "Application Url";
           
 WebClient webClient = new WebClient();
 DefaultCredentialsProvider credential = new DefaultCredentialsProvider();
           
 //Set some example credentials
 credential.addCredentials("UserName", "Passeord");          
 webClient.setCredentialsProvider(credential);
           
 HtmlPage htmlPage = webClient.getPage(url);

 //verify response
 Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCode());
 Assert.assertEquals("OK",htmlPage.getWebResponse().getStatusMessage());

5 comments:

  1. If Assert and verify method failed then how to handle error in Selenium WebDriver?

    ReplyDelete
  2. If assert and verify methods failed for a particular case then how can we handle error in selenium WebDriver?
    It's urg for me if any one have answer pls replay me ASAP

    ReplyDelete
    Replies
    1. User try and catch to handle exception

      Delete
  3. how to configure webclient class file...i am getting error message

    I used html jar file

    import java.io.IOException;
    import java.net.MalformedURLException;

    import org.testng.Assert;

    import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;

    ReplyDelete
  4. how can i get webclient class file

    did we have any separate jar files on that

    I downloaded html jar fiel

    i am using below import files

    import java.io.IOException;
    import java.net.MalformedURLException;

    import org.testng.Assert;

    import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;


    getting below error

    Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/css/sac/ErrorHandler
    at Get_200_response.main(Get_200_response.java:15)
    Caused by: java.lang.ClassNotFoundException: org.w3c.css.sac.ErrorHandler
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more


    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution