Combined with the results of the last study selenium webdriver potocol , Write it yourself http request call remote driver Instead of selenium API

selenium web driver Json protocol Please see  http://www.cnblogs.com/tobecrazy/p/5020741.html

What I use here is Gson and httpclient

First , Make one remote sever

 java -Dwebdriver.ie.driver="IEDriverServer.exe" -Dwebdriver.chrome.driver="chromedriver.exe" -jar selenium-server-standalone-2.48.0.jar

We need to use httpclient Of Post and delete method

Create a httpclient object

 HttpClient httpClient = HttpClients.createDefault();

Create a post request

 JsonObject setCapability = new JsonObject();
setCapability.addProperty("browserName","firefox");
JsonObject capability = new JsonObject();
capability.add("desiredCapabilities",setCapability);
HttpPost httpPost = new HttpPost(base);

Create a delete request

 url = base + sessionId ;
HttpDelete httpDelete = new HttpDelete(url);

from respose In order to get session ID

HttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength()); String resultEntity = EntityUtils.toString(entity);
System.out.println("Response content: " + resultEntity);
JsonObject result= new JsonParser().parse(resultEntity).getAsJsonObject();
JsonElement sessionIdJson = result.get("sessionId");
if(!sessionIdJson.isJsonNull())
sessionId =sessionIdJson.getAsString();
JsonElement valueJson = result.get("value"); if(!valueJson.isJsonNull())
{
JsonObject tm=valueJson.getAsJsonObject();
JsonElement elementIdJson = tm.get("ELEMENT");
if(elementIdJson!=null)
elementId=elementIdJson.getAsString(); } }
} finally {
((Closeable) response).close();
}

All codes are as follows :

import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser; public class webDriverJson {
private static String base = "http://127.0.0.1:4444/wd/hub/session/";
private static String elementId;
static String sessionId = ""; public static void main(String[] args) throws Exception, IOException { HttpClient httpClient = HttpClients.createDefault(); JsonObject setCapability = new JsonObject();
setCapability.addProperty("browserName","firefox");
JsonObject capability = new JsonObject();
capability.add("desiredCapabilities",setCapability);
HttpPost httpPost = new HttpPost(base);
//create session
postExecutor(httpClient, httpPost, capability); String url = base + sessionId + "/url";
httpPost = new HttpPost(url); JsonObject getUrl = new JsonObject();
getUrl.addProperty("url", "http://www.baidu.com"); postExecutor(httpClient, httpPost, getUrl); //find input box
url = base + sessionId + "/element";
httpPost = new HttpPost(url);
JsonObject findElement = new JsonObject();
findElement.addProperty("using", "id");
findElement.addProperty("value", "kw");
postExecutor(httpClient, httpPost, findElement); System.out.println(elementId); url = base + sessionId + "/element/"+elementId+"/value";
httpPost = new HttpPost(url);
JsonObject typeElement = new JsonObject(); String json = "{\"value\":[\"webdriver\"]}";
JsonParser jp = new JsonParser();
typeElement = (JsonObject) jp.parse(json); postExecutor(httpClient, httpPost, typeElement); //find search button url = base + sessionId + "/element";
httpPost = new HttpPost(url);
JsonObject findSearchButton = new JsonObject();
findSearchButton.addProperty("using", "id");
findSearchButton.addProperty("value", "su");
postExecutor(httpClient, httpPost, findSearchButton);
System.out.println(elementId); url = base + sessionId + "/element/"+elementId+"/click";
httpPost = new HttpPost(url);
postExecutor(httpClient, httpPost,null); //delete session
url = base + sessionId ;
HttpDelete httpDelete = new HttpDelete(url); deleteExecutor(httpClient, httpDelete); } /**
* @author Young
* @param httpClient
* @param httpPost
* @param jo
* @throws UnsupportedEncodingException
* @throws IOException
* @throws ClientProtocolException
*/
public static void postExecutor(HttpClient httpClient, HttpPost httpPost,
JsonObject jo) throws UnsupportedEncodingException, IOException,
ClientProtocolException {
if(jo!=null)
{
StringEntity input = new StringEntity(jo.toString());
input.setContentEncoding("UTF-8");
input.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpPost.setEntity(input);
} HttpResponse response = httpClient.execute(httpPost); try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength()); String resultEntity = EntityUtils.toString(entity);
System.out.println("Response content: " + resultEntity);
JsonObject result= new JsonParser().parse(resultEntity).getAsJsonObject();
JsonElement sessionIdJson = result.get("sessionId");
if(!sessionIdJson.isJsonNull())
sessionId =sessionIdJson.getAsString();
JsonElement valueJson = result.get("value"); if(!valueJson.isJsonNull())
{
JsonObject tm=valueJson.getAsJsonObject();
JsonElement elementIdJson = tm.get("ELEMENT");
if(elementIdJson!=null)
elementId=elementIdJson.getAsString(); } }
} finally {
((Closeable) response).close();
}
} /**
* @author Young
* @param httpClient
* @param delete
* @throws UnsupportedEncodingException
* @throws IOException
* @throws ClientProtocolException
*/
public static void deleteExecutor(HttpClient httpClient, HttpDelete delete) throws UnsupportedEncodingException, IOException,
ClientProtocolException { HttpResponse response = httpClient.execute(delete); try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength()); String resultEntity = EntityUtils.toString(entity);
System.out.println("Response content: " + resultEntity);
JsonObject result= new JsonParser().parse(resultEntity).getAsJsonObject();
JsonElement sessionIdJson = result.get("sessionId");
if(!sessionIdJson.isJsonNull())
sessionId =sessionIdJson.getAsString();
JsonElement valueJson = result.get("value"); if(!valueJson.isJsonNull())
{
JsonObject tm=valueJson.getAsJsonObject();
JsonElement elementIdJson = tm.get("ELEMENT");
if(elementIdJson!=null)
elementId=elementIdJson.getAsString(); } }
} finally {
((Closeable) response).close();
}
} }

Running effect :

understand selenium What's the point of the principle ?

Most people use selenium To automate , But not everyone knows selenium Principle , If you can master it selenium principle

Can transform selenium API, Use webdriver protocol To do something that can improve the automated testing framework .、

such as , Maybe you are selenium There are many problems in the process of automation get I can't open the page , To ensure the robustness of your scripts , At this point, you can add a paragraph httprequest To get

response The key value of , If not 2 The beginning can be set refresh, For example, we need to do some preparatory work , For example, environment configuration can also be used

Use httpclient call selenium webdriver More articles about

  1. About IIS Permission problems (Selenium WebDriver Call error record )

    Local VS During debugging, use Selenium WebDriver open FF The browser works , The project is deployed to IIS After the request to call the browser has been prompted timeout , Exceptions are as follows : Because local debugging can succeed , First of all, exclude the component version problem and browser compatibility problem ...

  2. selenium webdriver Right click Save as download file ( combination robot and autoIt)

    First, thank you. Lakshay Sharma  The guidance of the great God I've been studying selenium webdriver Right-click menu , Find out selenium webdriver Unable to operate the browser right-click menu , Pictured If I want to right-click save as ...

  3. Selenium Webdriver java Accumulate one

    Selenium Webdriver Study : http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...

  4. Selenium Of PO Pattern (Page Object Model)|(Selenium Webdriver For Python)

            Research Selenium + python Automated testing for nearly two months , I can't say very skilled , At least selenium In depth understanding of automated execution . From the initial unstructured code , To the use of classes , Method encapsulation , From the original function ...

  5. Selenium webdriver Operate the calendar control

    General date controls are input Pop up under the label , If you use webdriver To set the date , 1. Locate the input 2. Use sendKeys Method such as : however , Some date controls are readonly Of such as 1 ...

  6. Selenium WebDriver The use of some mouse and keyboard events in

    from :http://www.ithov.com/linux/133271.shtml In the use of Selenium WebDriver When doing automated tests , Will often simulate some behavior of mouse and keyboard . Like using a mouse click ...

  7. selenium webdriver (python) The first edition PDF

    Preface If you are a person with python Language foundation students , I want to go through python+ selenium To implement automation , So you're very lucky to find this document , I'm also very honored to be able to help you on the road of automated learning . Actually , I am here sel ...

  8. selenium webdriver (python) The third edition

    thank Thank you for buying the second edition , Thank you for your support for the fruits of my work ! You often ask me if I can't publish the third edition , It's also your encouragement , Let me continue to learn how to organize this document . Thank you , The second version of the document is sold on his Taobao website , Thanks for his help ...

  9. Selenium WebDriver + Python Environment configuration

    1.   Download the necessary tools and installation package 1.1.[Python development environment ] Download and install Python 2.7.x edition ( The current support 2.x edition , Don't download the latest 3.X Because python3 Not completely compatible python2) ...

Random recommendation

  1. Careercup - Google Interview questions - 4807591515389952

    2014-05-06 00:45 Topic link The original title is : What would happen if you have only one server for a web cache (a web brows ...

  2. jQuery Flexible sliding navigation menu implementation ideas and code

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8" /> <meta na ...

  3. mybatis Cache creation process

    With Part 1 The problem of , Look again. mybatis The creation process of 1. from SqlSessionFactoryBuilder analysis mybatis-config.xml Start Parsing the file stream XMLConfigBuilder ...

  4. iOS Software architecture —— Architecture mode (Architectural Pattern)

    An architecture pattern describes the basic structure or framework of a software system . Architecture patterns provide some pre-defined subsystems , Assign their responsibilities , And give the rules and guidelines for organizing them together . Some authors call this architecture pattern system pattern [STELTING02]. ...

  5. CentOS6.x Machine installation Azure CLI2.0【1】

    install Azure CLI 2.0 The premise is : There must be... In the machine Python 2.7.x or Python 3.x. If there is no one in the machine Python edition , Please install it in time 1. Prepare one CentOS 6.9 Machine ...

  6. eclipse Make association code

    I've been looking for the association code for a long time , Finally found a detailed explanation of : http://blog.csdn.net/long2010yu2010/article/details/8497505

  7. Oral English practice series -C28- seaside - Identification - The trees on the edge of the cliff

    beach seaside brown hut near body of water green trees near sea during daytime photography Seaside resorts beach [ ...

  8. DbGridEh Change the color of the line in

    DbGridEh Change the color of the line in The code is also very simple , It just didn't occur to me at that time : if ds2.FieldByName('AutoRead').AsInteger=0 thenbegin  DBGridEh1.Ca ...

  9. TypeScript install

    1.Installing Node.js and updating npm Updating npm----npm install npm@latest -g Installing TypeScrip ...

  10. php Share a object(stdclass) Functions that convert arrays

    Often after invoking an excuse The content returned is json strand , utilize php function json_decode() analysis json After the string The data type is object(stdclass) This is an object , It's OK to take the content obtain , ...