Forum > Networking and Web Programming

Browser Automation with selenium

(1/5) > >>

Jurassic Pork:
hello,
i have found this library to do that with pascal language : Webdriver For Delphi lazarus.
The problem is that the lazarus part isn't done.
1 - the main problem is the json library used  in the unit Webdriver4D.pas : JsonDataObjects  . How to replace it with fcl-json and jsonparser ?
for example how to replace the code in brackets with the equivalent  of the fcl-json library :

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TWebDriver.ExecuteScript(const Script: string;  const Args: string = '[]'): string;var  Command: string;  Data: string;  Resp: string;  Json: TJsonObject;begin  Json := TJsonObject.Create;  try    Command := Host + '/session/' + FSessionID + '/execute/sync';{   Json.S['script'] := Script;    Json.A['args'].FromJSON(Args);    Data := Json.ToJSON();    }    Resp := ExecuteCommand(cPost, Command, Data);    result := ProcResponse(Resp);  Finally    FreeAndNil(Json);  end;end;            
Friendly, J.P

Leledumbo:
.S[] -> https://www.freepascal.org/docs-html/current/fcl/fpjson/tjsonobject.strings.html
.A[] -> https://www.freepascal.org/docs-html/current/fcl/fpjson/tjsonobject.arrays.html
.FromJSON -> https://www.freepascal.org/docs-html/current/fcl/fpjson/getjson.html (not a method, but same functionality to convert a JSON formatted string to a JSON data, probably needs a cast as well)
.ToJSON -> either https://www.freepascal.org/docs-html/current/fcl/fpjson/tjsondata.formatjson.html or https://www.freepascal.org/docs-html/current/fcl/fpjson/tjsondata.asjson.html, depending on whether you want to format it or not

Jurassic Pork:
hello,
thanks Leledumbo, i have found how to do that with  fpjson :

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TWebDriver.ExecuteScript(const Script: string;  const Args: string = '[]'): string;var  Command: string;  Data: string;  Resp: string;  Json: TJsonObject;begin  Json := TJsonObject.Create(['script', Script]);  try    Command := Host + '/session/' + FSessionID + '/execute/sync';    Json.Add('args',GETJson(Args));    Data := Json.AsJSON;    Resp := ExecuteCommand(cPost, Command, Data);    result := ProcResponse(Resp);  Finally    FreeAndNil(Json);  end;end; I started a lazarus version of WebDriver4D ( name WebDriver4L) .
replace the delphi unit httpclient with the lazarus unit   fphttpclient
replace the delphi unit JsonDataObjects with the lazarus units fpson, jsonparser
version of WebDriver4L is alpha but it begins to work :
Example of code to open chrome and clear image and files cache  in  settings :

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---implementationuses  Webdriver4L;{$R *.lfm} { TForm1 } procedure TForm1.Button2Click(Sender: TObject);const CacheDlg = 'return document.querySelector(''settings-ui'')' +                 '.shadowRoot.querySelector(''settings-main'')' +                 '.shadowRoot.querySelector(''settings-basic-page'')' +                 '.shadowRoot.querySelector(''settings-section > settings-privacy-page'')' +                 '.shadowRoot.querySelector(''settings-clear-browsing-data-dialog'')';const btClear  = '.shadowRoot.querySelector(''#clearBrowsingDataDialog'')' +                 '.querySelector(''#clearBrowsingDataConfirm'')';const chkboxH1  = '.shadowRoot.querySelector(''iron-pages'')' +                  '.querySelector(''#browsingCheckboxBasic'')';const chkboxH2  = '.shadowRoot.querySelector(''iron-pages'')' +                  '.querySelector(''#cookiesCheckboxBasic'')';const chkboxH3  = '.shadowRoot.querySelector(''iron-pages'')' +                  '.querySelector(''#cacheCheckboxBasic'')';var Robot2 : TWebDriver;    Element : TWebElement;begin  Robot2 := TChromeDriver.Create(nil);  Robot2.StartDriver('chromedriver.exe');  Robot2.NewSession;  Robot2.Implicitly_Wait(1000);  Robot2.Set_Window_Size(640, 640);  Sleep(5000);  Robot2.GetURL('chrome://settings/clearBrowserData');  Sleep(2000);  Element := Robot2.FindElementByXPath('//settings-ui');  Element.ElementData := Robot2.ExecuteScript(CacheDlg + chkboxH1 + ';');  Memo1.Append('Etiquette case à cocher 1 : ' + Element.AttributeValue('label'));  if Element.PropertyValue('checked') = 'True' then  Element.Click();  Sleep(1000);  Element.ElementData := Robot2.ExecuteScript(CacheDlg + chkboxH2 + ';');  Memo1.Append('Etiquette case à cocher 2 : ' + Element.AttributeValue('label'));  if Element.PropertyValue('checked') = 'True' then  Element.Click();  Sleep(1000);  Element.ElementData := Robot2.ExecuteScript(CacheDlg + chkboxH3 + ';');  Memo1.Append('Etiquette case à cocher 3 : ' + Element.AttributeValue('label'));  if Element.PropertyValue('checked') = 'False' then  Element.Click();  Sleep(1000);  Element.ElementData := Robot2.ExecuteScript(CacheDlg + btClear + ';');  Element.Click();  Sleep(30000);  Robot2.Clear;end;
Friendly, J.P

Jurassic Pork:
hello,
now my alpha version of webdriver4L is working on linux (centos 8 - lazarus 2.0.8 ) . Screenshots work.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---implementationuses  Webdriver4L;{$R *.lfm}{ TForm1 }procedure TForm1.Button2Click(Sender: TObject);const CacheDlg = 'return document.querySelector(''settings-ui'')' +                 '.shadowRoot.querySelector(''settings-main'')' +                 '.shadowRoot.querySelector(''settings-basic-page'')' +                 '.shadowRoot.querySelector(''settings-section > settings-privacy-page'')' +                 '.shadowRoot.querySelector(''settings-clear-browsing-data-dialog'')';const btClear  = '.shadowRoot.querySelector(''#clearBrowsingDataDialog'')' +                 '.querySelector(''#clearBrowsingDataConfirm'')';const chkboxH1  = '.shadowRoot.querySelector(''iron-pages'')' +                  '.querySelector(''#browsingCheckboxBasic'')';const chkboxH2  = '.shadowRoot.querySelector(''iron-pages'')' +                  '.querySelector(''#cookiesCheckboxBasic'')';const chkboxH3  = '.shadowRoot.querySelector(''iron-pages'')' +                  '.querySelector(''#cacheCheckboxBasic'')';var Robot2 : TWebDriver;    Element : TWebElement;    png :  TPortableNetworkGraphic;begin  Robot2 := TChromeDriver.Create(nil);  Robot2.StartDriver('chromedriver');  Sleep(5000);  Robot2.NewSession;  Robot2.Implicitly_Wait(1000);  Robot2.Set_Window_Size(640, 640);  Robot2.GetURL('chrome://settings/clearBrowserData');  Sleep(2000);  Robot2.ScreenShot('clearBrowserData1.png');  Element := Robot2.FindElementByXPath('//settings-ui');  Element.ElementData := Robot2.ExecuteScript(CacheDlg + chkboxH1 + ';');  Memo1.Append('Etiquette case à cocher 1 : ' + Element.AttributeValue('label'));  if Element.PropertyValue('checked') = 'True' then  Element.Click();  Sleep(1000);  Element.ElementData := Robot2.ExecuteScript(CacheDlg + chkboxH2 + ';');  Memo1.Append('Etiquette case à cocher 2 : ' + Element.AttributeValue('label'));  if Element.PropertyValue('checked') = 'True' then  Element.Click();  Sleep(1000);  Element.ElementData := Robot2.ExecuteScript(CacheDlg + chkboxH3 + ';');  Memo1.Append('Etiquette case à cocher 3 : ' + Element.AttributeValue('label'));  if Element.PropertyValue('checked') = 'False' then  Element.Click();  Sleep(1000);  Robot2.ScreenShot('clearBrowserData2.png');  Element.ElementData := Robot2.ExecuteScript(CacheDlg + btClear + ';');  Element.Click();  Application.ProcessMessages;  Sleep(10000);  Robot2.Clear;  png := TPortableNetworkGraphic.Create;  png.Assign(Self.GetFormImage);  png.SaveToFile('clearBrowserDataLazForm.png');  png.free;end;
Results in Attachments (click on the first attachment to see animation)

Friendly, J.P

Blade:
This is very useful.  Thanks for sharing.

Navigation

[0] Message Index

[#] Next page

Go to full version