Learn to call Webdriver Jsonwireprotocol's RESTful API directly

Source: Internet
Author: User

API Reference:

Https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

https://developer.microsoft.com/en-us/microsoft-edge/platform/documentation/webdriver-commands/

Https://whitewaterhill.com/php-webdriver-bindings-0.9.0/status.html

This article attempts to simulate the implementation of the Webdriver Java binding directly using Webdriver's Rest API, which facilitates a more in-depth understanding of selenium webdriver and its framework, and is the most basic knowledge in future projects where the framework needs to be modified. , and can even customize their binding in their own way, providing additional functionality.

1. Start Selenium-server-standalone as REST API service

    • Start Hub:java-jar selenium-server-standalone-2.53.0.jar-role Hub
    • Start Node:java-jar selenium-server-standalone-2.53.0.jar-role node-hub Http://localhost:4444/grid/register-browser "b Rowsername=firefox,firefx_binary=<path-to-firefox-executable>,maxinstances=6,platform=windows "

2. When hub and node are started, they can send a request to the relevant Service API address, in this case the hub and node are started locally

3. REST API refer to the link at the beginning of this article, request the link format Http://localhost:4444/wd/hub + < requested api>

Such as
The POST Http://localhost:4444/wd/hub/session will return the created session information, and subsequent requests will be based on that session.
 PackageDriverjsonwirepro;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.util.Base64;Importjava.util.Date;ImportJava.util.Base64.Decoder;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.entity.StringEntity;ImportOrg.apache.http.impl.client.HttpClientBuilder;Importorg.apache.http.util.EntityUtils;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONObject; Public classcallrestful {StaticString huburl= "Http://localhost:4444/wd/hub"; StaticString sessionapi= "/session"; StaticString deletesessionapi= "/session/:sessionid"; StaticString geturlapi= "/session/:sessionid/url"; StaticString findelementapi= "/session/:sessionid/element"; StaticString clickelementapi= "/session/:sessionid/element/:id/click"; StaticString sendkeysapi= "/session/sessionid/element/:id/value"; StaticString takescreenapi= "/session/:sessionid/screenshot"; StaticHttpClient client =gethttpclient (); StaticString sessionId;  Public Static voidMain (String args[])throwsexception{sessionId=getsessionid ("Firefox"); Try{NAVIGATEURL ("Www.google.com"); String ElementID=findelementbycss (". Mockclass.span #mockId"); SendKeys (ElementID,"Jsonwireprotocol"); ElementID=findelementbycss (". Mockclass #mockButon");        Clickelement (ElementID); }Catch(Throwable t) {//TODO}finally{deletesession (sessionId); }                }         Public StaticString GetSessionID (String browser)throwsexception{String para= "{\" desiredcapabilities\ ": {\" browsername\ ": \" "+browser+" \ "}}"; String URL=huburl+Sessionapi; HttpPost Post=postrequest (URL, Json.parseobject (para)); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode ()!=200)return NULL; String Respstr=entityutils.tostring (Response.getentity ()); String sessionId= (String) json.parseobject (RESPSTR). Get ("SessionId"); Callrestful.sessionid=sessionId; returnsessionId; }         Public StaticHttpClient gethttpclient () {HttpClient HttpClient=httpclientbuilder.create (). build (); returnhttpClient; }             Public Static BooleanNAVIGATEURL (String URL)throwsException {String Apiurl=huburl+geturlapi.replace (": SessionId", sessionId); String para= "{\" url\ ": \" "+url+" \ "}"; HttpPost Post=postrequest (Apiurl, Json.parseobject (para)); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode ()!=200)return false; return true; }         Public Static BooleanDeletesession (String sessionId)throwsException {String Apiurl=huburl+deletesessionapi.replace (": SessionId", sessionId); HttpGet Get=getrequest (Apiurl); HttpResponse Response=Client.execute (GET); if(Response.getstatusline (). Getstatuscode ()!=200)return false; return true; }         Public StaticString Findelementbycss (String cssselector)throwsexception{String para= "{\" using\ ": \" CSS selector\ "," + "\" value\ ": \" "+cssselector+" \ "}"; String Apiurl=huburl+findelementapi.replace (": SessionId", sessionId); HttpPost Post=postrequest (Apiurl, Json.parseobject (para)); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode ()!=200)return NULL; String Respstr=entityutils.tostring (Response.getentity ()); String ElementID= (String) json.parseobject (RESPSTR). Getjsonobject ("value"). Get ("ELEMENT")); returnElementID; }         Public Static BooleanClickelement (String ElementID)throwsException {String Apiurl=huburl+Clickelementapi. Replace (": SessionId", sessionId). Replace (": id", ElementID); HttpPost Post= Postrequest (Apiurl,NULL); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode ()!=200)return false; return true; }         Public Static BooleanSendKeys (String elementid, String texttosend)throwsexception{clickelement (ElementID); String para= "{\" value\ ": [\" "+texttosend+" \ "]}"; String Apiurl=huburl+Sendkeysapi. Replace (": SessionId", sessionId). Replace (": id", ElementID); HttpPost Post=postrequest (Apiurl, Json.parseobject (para)); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode ()!=200)return false; return true; }         Public Static BooleanTakescreenshot ()throwsexception{String Apiurl=huburl+takescreenapi.replace (": SessionId", sessionId); HttpPost Post=postrequest (Apiurl,NULL); HttpResponse Response=Client.execute (POST); if(Response.getstatusline (). Getstatuscode ()!=200)return false; Jsonobject Jsonobject=Json.parseobject (entityutils.tostring (Response.getentity ())); String Srcreeninstr= (String) jsonobject.getstring ("Value");        Savetolocal (SRCREENINSTR); return true; }         Public Statichttppost postrequest (String URL, jsonobject postjson) {HttpPost post=Newhttppost (URL); if(postjson==NULL)             returnPost;        Stringentity sentity; Try{sentity=NewStringentity (Json.tojsonstring (Postjson), "Utf-8"); Sentity.setcontenttype ("Application/json");        Post.setentity (sentity); } Catch(Throwable e) {e.printstacktrace (); }        returnPost; }             Public Statichttpget getrequest (String url) {httpget get=Newhttpget (URL); returnget; }         Public Static BooleanSavetolocal (String base64string)throwsException {Decoder Decoder=Base64.getdecoder (); byte[] bytes=Decoder.decode (base64string); FileOutputStream FileOutputStream=NewFileOutputStream (NewFile (". \\screenshot" +NewDate (). GetTime () + ". jpg")); Fileoutputstream.write (Bytes,0, bytes.length);        Fileoutputstream.close (); return true; }}

Learn to call Webdriver Jsonwireprotocol's RESTful API directly

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: info-contact@alibabacloud.com 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.