1. Drive Google Chrome Browser

Using libcurl (found in Eu 4 Standard library) and chromedriver (https://sites.google.com/a/chromium.org/chromedriver/), we can drive Google Chrome Browser as si following example:

include std/filesys.e 
include std/error.e 
include std/dll.e 
include std/net/url.e 
include std/convert.e 
include std/os.e 
include lib/_debug_.e 
include lib/_curl_constants_.e 
include lib/_curl_.e 
include lib/_http_.e 
include lib/_json_.e 
include std/console.e 
 
constant 
  PROXY = "localhost:8888",   -- not used here. See below how to use proxy (constant REQUIRED_CAPABILITIES_CHROME) 
  LOGIN = "myLogin", 
  PASSWORD = "myPassword" 
   
-- chromedriver --log-path=chromedriver.log [--verbose] 
 
function string_to_list(sequence s) 
  sequence result = "[\"" & s[1] & "\"" 
  for i = 2 to length(s) do 
    result &= ", \"" & s[i] & "\""   
  end for 
  return result & "]"  
end function 
 
function curl_send(atom curl) 
  object res = curl_easy_perform_ex(curl) 
 
  printf(f_debug, "Status: %d\n", {res[1]}) 
  printf(f_debug, "Effective URL: %s\n", {res[2]}) 
  analyze_object(res[3], "Headers", f_debug) 
  analyze_object(res[4], "Content", f_debug) 
  analyze_object(res[4], "Content", 1) 
 
  return res 
end function 
 
function curl_get(atom curl, sequence url) 
  curl_easy_setopt(curl, CURLOPT_URL, url) 
--    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL) 
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0) 
  curl_easy_setopt(curl, CURLOPT_POST, 0) 
 
  return curl_send(curl) 
end function 
 
function curl_post(atom curl, sequence url, sequence data) 
  puts(f_debug, "POST " & data & "\n") 
  curl_easy_setopt(curl, CURLOPT_URL, url) 
  curl_easy_setopt(curl, CURLOPT_POST, 1) 
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data) 
--    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, length(data)) 
 
  return curl_send(curl) 
end function 
 
 
function curl_delete(atom curl, sequence url) 
  curl_easy_setopt(curl, CURLOPT_URL, url) 
--  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL) 
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0) 
  curl_easy_setopt(curl, CURLOPT_POST, 0) 
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE") 
 
  return curl_send(curl) 
end function 
 
constant 
  REQUIRED_CAPABILITIES_CHROME = "{" & 
    " \"requiredCapabilities\": {}," & 
    " \"desiredCapabilities\": {" & 
      " \"platform\": \"ANY\"," & 
      " \"browserName\": \"chrome\"," & 
      " \"version\": \"\"," & 
/*  include following lines if proxy used 
      " \"proxy\": {" & 
        " \"proxyType\": \"MANUAL\"," & 
        " \"noProxy\": null," & 
        " \"autodetect\": false," & 
        " \"sslProxy\": \"" & PROXY & "\"," & 
        " \"httpProxy\": \"" & PROXY & "\"," & 
        " \"class\": \"org.openqa.selenium.Proxy\"," & 
        " \"ftpProxy\": \"" & PROXY & "\"" & 
     "}," & 
*/ 
     " \"javascriptEnabled\": true}" & 
  "}" 
 
object res 
atom curl 
 
integer p, pg 
sequence request, buffer, s 
object data, o 
   
f_debug = open(InitialDir & SLASH & "debug.log", "w") 
crash_file(InitialDir & SLASH & "ex.err") 
res = curl_global_init(CURL_GLOBAL_DEFAULT) 
curl = curl_easy_init() 
if curl then 
--  curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP) 
--  curl_easy_setopt(curl, CURLOPT_PROXY, "") 
  curl_easy_setopt(curl, CURLOPT_COOKIEFILE, InitialDir & SLASH & "cookies.txt") 
  curl_easy_setopt(curl, CURLOPT_COOKIEJAR, InitialDir & SLASH & "cookies.txt") 
  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1) 
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) 
-- Connection: close for standalone server, keep-alive for chromedriver 
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Connection: keep-alive") 
--  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "close") 
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Accept: application/json") 
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, "Content-Type: application/json; charset=UTF-8") 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session", REQUIRED_CAPABILITIES_CHROME) 
 
  sequence sessionId = get_json_value("sessionId", json_to_sequence(res[4]), 0) 
  printf(1, "Session ID: %s\n", {sessionId}) 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/timeouts/implicit_wait", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"ms\": 30000.0" & 
                  "}") 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/url", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"url\": \"http://192.168.1.250/phpMyAdmin/\"" & 
                  "}") 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/elements", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"using\": \"tag name\", " & 
                    "\"value\": \"input\"" & 
                  "}") 
 
--  sequence values = get_json_value("value", json_to_sequence(res[4]), 0) 
 
  -- manage login 
     
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"using\": \"id\", " & 
                    "\"value\": \"input_username\"" & 
                  "}") 
 
  sequence elementId = get_json_value("ELEMENT", json_to_sequence(res[4]), 0) 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element/" & elementId & "/clear", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"id\": \"" & elementId & "\"" & 
                  "}") 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element/" & elementId & "/value", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"id\": \"" & elementId & "\", " & 
                    "\"value\": " & string_to_list(LOGIN) & 
                  "}") 
 
  -- manage password 
   
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"using\": \"id\", " & 
                    "\"value\": \"input_password\"" & 
                  "}") 
 
  elementId = get_json_value("ELEMENT", json_to_sequence(res[4]), 0) 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element/" & elementId & "/clear", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"id\": \"" & elementId & "\"" & 
                  "}") 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element/" & elementId & "/value", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"id\": \"" & elementId & "\", " & 
                    "\"value\": " & string_to_list(PASSWORD) & 
                  "}") 
 
  -- submit form 
   
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"using\": \"id\", " & 
                    "\"value\": \"input_go\"" & 
                  "}") 
 
  elementId = get_json_value("ELEMENT", json_to_sequence(res[4]), 0) 
 
  res = curl_post(curl, "http://127.0.0.1:9515/session/" & sessionId & "/element/" & elementId & "/click", "{" & 
                    "\"sessionId\": \"" & sessionId & "\", " & 
                    "\"id\": \"" & elementId & "\"" & 
                  "}") 
 
  sleep(5) 
   
  res = curl_delete(curl, "http://127.0.0.1:9515/session/" & sessionId) 
 
  curl_easy_cleanup(curl) 
end if 
 
curl_global_cleanup() 
 
close(f_debug) 
 
maybe_any_key() 
 

URL http://192.168.1.250/phpMyAdmin/ is a MySQL manager on my Synology NAS. Replace it by yours.

Jean-Marc

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu