Multiple ways to get background data through the rest service

Source: Internet
Author: User

When developing a Web page with a rest service to get JSON data and then organizing the data to be displayed on a Web page, we have the following 2 points to verify when doing automated testing:

1. Service data can be retrieved as normal

2. The data are correctly displayed and displayed in the correct location.

So to do this 2-point verification, the specific steps of automated testing are as follows

1. Obtain data from this service by method (Json,xml)

2. Sorting and extracting data by parsing JSON or XML

3. Verify that the collated data is displayed in the right place

After 2 points, for the 1th method I summarized the following:

1. Selenium execute JavaScript get to get

Public string Retrieveservice (String svcurl) throws exception{string Js_getresponsdata= "var request = new XMLHttpRequest ();" + "var ansytype = false;" + " (' GET ', '" +svcurl+ "', Ansytype);" + "Request.setrequestheader (' X-requested-with ', ' XMLHttpRequest ');" + "request.send (null);"                 + "if (request.readystate = = 4) {" + "if (request.status = = 200) {" + " return request.responsetext;} " +                "}    "; ("Javascript ready to execute:" +js_getresponsdata); Pause (2000); String ResponseData=(String) Jsreturner (js_getresponsdata); returnResponseData; }

The above code for can be directly obtained, does not require authentication of the address, if the need for authentication, then when the selenium open the Web page, with selenium to log in and then run the code, Or you can run the JavaScript post with selenium, but you need to choose a different authentication method for different authentication methods (BASIC,NTLM).

2. With, the advantage of this approach is that it does not require a jar package, runs directly, and does not specifically require the authentication method, where I post a validation of NTLM:

     Publicstring GETDATAFROMNTLM (string url,string user,string password) {string str=NULL; Try{Cookiehandler.setdefault (NewCookiemanager (NULL, Cookiepolicy.accept_all)); Authenticator.setdefault (Newmyauthenticator (user, password)); URL URL=Newurl (URL); URLConnection Connection=url.openconnection (); InputStream is=Connection.getinputstream (); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR);  while(true) {str=Br.readline (); if(str = =NULL)                {                     Break; }Else{                    returnstr; }             }        } Catch(Exception ex) {ex.printstacktrace (); }        returnstr; }

3. Use HttpClient to get the data, this need to get the jar package, directly paste the code

Defaulthttpclient hclient =Newdefaulthttpclient (); Ntcredentials creds=NewNtcredentials ("[Email protected]", "tenp1108", "Basicregistry", "");          System.out.println (Creds.getuserprincipal (). GetName ());        System.out.println (Creds.getpassword ());         ((abstracthttpclient) hclient). Getcredentialsprovider (). SetCredentials (Authscope.any, creds); Httphost Target=NewHttphost ("", "http",); HttpContext Localcontext=NewBasichttpcontext (); HttpGet HttpGet=NewHttpGet ("/ntlm-protected/info"); HttpResponse Response=Hclient.execute (Target, HttpGet, Localcontext); Httpentity entity1=response.getentity ();

The above code is used to pass authentication NTLM to get the data.

Of course, when none of the above methods work, there is also the most primitive way to directly selenium open the service URL and get the data through XPath.

Multiple ways to get background data through the rest service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.