B4J Library Selenium Browser Automation - Create any Robot with Browser

Selenium.jpg


Selenium WebDriver - Mastering Test Automation with Selenium

What is Selenium?
Selenium is an open source automated testing suite for web applications across different browsers and platforms. It is quite similar to HP Quick Test Professional (QTP) only that Selenium focuses on automating web-based applications.

Selenium is not just a single tool but a suite of software's, each catering to different testing needs of an organization.

Based on Selenium WebDrivers, this new Library for B4J can automate browsers and create any robot and get any information from websites.

And you can use with or Without UI



Step 1:
Download the library and dependencies and put in your B4J Library Folder

https://visualnet.ddns.me/vncorp/store




Step 2:
Download the ChromeDriver for your platform.
Can be used on Windows/Linux/MacOSX, just download correct driver for your platform on:

http://chromedriver.chromium.org/downloads

TIP: Inside the samples you already have ChromeDriver.exe for windows


Step 3:
Try the samples


Sample 1: (SampleNasdaq.zip) Open Nasdaq.com website and get the top stocks on market

Sample 2: (SampleOyster.zip) Open UK TLF website for retrieve Oysters card and balance from each one

Check with a few lines of code you can retrieve a table from nasdaq website
B4X:
Dim objSelenium As Selenium
objSelenium.DebugMode = True
objSelenium.Initialize("chromedriver.exe","objSelenium")
objSelenium.Navigate("https://www.nasdaq.com/")
Dim lstElements As List = objSelenium.findElementList("indexTable")
For Each k As String In lstElements
      Log(k)
Next


Check others libraries on:

https://visualnet.ddns.me/vncorp/store
 
Last edited:

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello Fabio,

Is already update with this new feature, get the latest version on first post (v1.0.3).

Just add arguments BEFORE initialize, like this:
B4X:
objSelenium.addArgument("--headless")
objSelenium.Initialize("chromedriver.exe","objSelenium")

you can add more than one, like this:

B4X:
objSelenium.addArgument("--headless")
objSelenium.addArgument("--arg2")
objSelenium.addArgument("--arg3")
objSelenium.addArgument("--arg4")
objSelenium.Initialize("chromedriver.exe","objSelenium")
 

walterf25

Expert
Licensed User
Longtime User
Hello Fabio,

Is already update with this new feature, get the latest version on first post (v1.0.3).

Just add arguments BEFORE initialize, like this:
B4X:
objSelenium.addArgument("--headless")
objSelenium.Initialize("chromedriver.exe","objSelenium")

you can add more than one, like this:

B4X:
objSelenium.addArgument("--headless")
objSelenium.addArgument("--arg2")
objSelenium.addArgument("--arg3")
objSelenium.addArgument("--arg4")
objSelenium.Initialize("chromedriver.exe","objSelenium")
Does this library create an HTML report once you have tested something?

Walter
 

alejandro842g

New Member
Licensed User
After this dim obj = objSelenium.findElementByClassName("captchaimage")
how can i get src property? obj.src dosent work

thanks
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
After this dim obj = objSelenium.findElementByClassName("captchaimage")
how can i get src property? obj.src dosent work

thanks
Hello Alejandro,

Sorry about the delay, I make some changes to put this new feature. "AttributeFromWebElement"



try this one:

B4X:
Dim elem As Object = objSelenium.findElementByClassName("navGuestGetOyster")
Log(objSelenium.AttributeFromWebElement(elem,"src"))

get the version 1.1.0

http://vnsoft.es/download/b4j/selenium/LibrayB4J_v1.1.0.zip
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello.

I have this error: http://prntscr.com/r01lj9

Any ideea?
Thank you!
According with this answer, check your version of chome driver and chrome

 

Don Oso

Active Member
Licensed User
Longtime User
Hi Alberto

Your last library its free? donationware? or just a demo?

Thanks

Code:
Waiting for debugger to connect...
Program started.
[Java][Initialize] eventname = objSelenium
Starting ChromeDriver 80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185}) on port 5578
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1581342607.432][WARNING]: Timed out connecting to Chrome, retrying...
feb 10, 2020 10:50:09 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMACIÓN: Detected dialect: W3C
[Java][EventB4]onInitialized
[Java][Navigate]URL=https://www.nasdaq.com/
[1581342610.018][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342610.121][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342610.402][SEVERE]: Timed out receiving message from renderer: 0.100
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[1581342610.643][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342610.878][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342611.574][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342611.978][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342612.514][SEVERE]: Timed out receiving message from renderer: 0.100
[Java][EventB4]onNavigate
[Java][GetTitle]
Daily Stock Market Overview, Data Updates, Reports & News | Nasdaq
[Java][GetURL]
https://www.nasdaq.com/
[Java][findElementList]ElementID=indexTable
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hi Alberto

Your last library its free? donationware? or just a demo?

Thanks

Code:
Waiting for debugger to connect...
Program started.
[Java][Initialize] eventname = objSelenium
Starting ChromeDriver 80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185}) on port 5578
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1581342607.432][WARNING]: Timed out connecting to Chrome, retrying...
feb 10, 2020 10:50:09 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMACIÓN: Detected dialect: W3C
[Java][EventB4]onInitialized
[Java][Navigate]URL=https://www.nasdaq.com/
[1581342610.018][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342610.121][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342610.402][SEVERE]: Timed out receiving message from renderer: 0.100
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[1581342610.643][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342610.878][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342611.574][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342611.978][SEVERE]: Timed out receiving message from renderer: 0.100
[1581342612.514][SEVERE]: Timed out receiving message from renderer: 0.100
[Java][EventB4]onNavigate
[Java][GetTitle]
Daily Stock Market Overview, Data Updates, Reports & News | Nasdaq
[Java][GetURL]
https://www.nasdaq.com/
[Java][findElementList]ElementID=indexTable
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
Hello! is donation ! the library is full funcional but to remove that message I accept donations to keep updating this
 

Ferpo

Member
Licensed User
Longtime User
Sorry I am starting test the library and may be my question is simple, Is possible go to the end the page to the page charge all data?
Many web site only show 10, 20 data information and when go to the end the page scrolling, refresh page with more data.
May be I can use sendkey but I don't know the element we need.
By other hand, is it possible run javascript?
I am looking to do some like that:
WebDriver driver = new ChromeDriver();
//Creating an object 'action'
Actions action = new Actions(driver);
//open SoftwareTestingMaterial.com
driver.get(Site URL); // Give site URL as name of the web page
//sleep for 3secs to load the page
Thread.sleep(3000);
//SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
//SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();
 
Last edited:

icakinser

Member
Licensed User
Longtime User
Is there way to handle more than one component with same name? Example if there nare two div sections but all the controls are same name?
 

astronald

Member
Licensed User
Longtime User
Hello first thanks for your wonderffull library, i have some questions
1. i can resize Chrome windows ?
2. Works with Gecko driver?
3. I Try know if a control exist with objSelenium.IsDisplayed(Control) before click but i receive crash, i solve this with try catch but i think is not correct.
4. After Control Click page is redirect but objSelenium_onNavigate() is not fired, this is critical for my Robot, how i can detect new page is loaded.
5. How i can donate if i decide migrate to Selenium.

Thanks again
Ronald.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello my friend,
1. i can resize Chrome windows ?

I can see on selenium this option, but is not yet implemented on my library, I can do this, yes...
1594811561738.png

2. Works with Gecko driver?

No, so far, just with chrome. Probably in the future yes.

3. I Try know if a control exist with objSelenium.IsDisplayed(Control) before click but i receive crash, i solve this with try catch but i think is not correct.

I have to investigate on original component, because I not do anything when you call this method, probably this will be fix on original library, not depend of me

4. After Control Click page is redirect but objSelenium_onNavigate() is not fired, this is critical for my Robot, how i can detect new page is loaded.

Can you give me some example of your code?

5. How i can donate if i decide migrate to Selenium.

You can see the paypal button on first post
 

astronald

Member
Licensed User
Longtime User
Hello my friend,
thanks very much, this is my code:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    
    Dim objSelenium As Selenium
    Private Sesion As Boolean
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    objSelenium.DebugMode = True
    objSelenium.Initialize("chromedriver.exe","objSelenium")
    objSelenium.addArgument("--headless")
    objSelenium.Navigate("https://www.loginpage.com/")

    Log(objSelenium.GetTitle)
    Log(objSelenium.GetURL)
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub objSelenium_onNavigate()
    Log(objSelenium.GetURL)
    Dim strPagina As String = objSelenium.GetURL
    strPagina = strPagina.SubString(strPagina.LastIndexOf("/")+1).ToUpperCase
                
    If  strPagina.IndexOf(";JSESSIONID") > 0 Then strPagina = strPagina.SubString2(0, strPagina.IndexOf(";JSESSIONID"))
    If  strPagina.IndexOf("?") > 0 Then strPagina = strPagina.SubString2(0, strPagina.IndexOf("?"))
    If  strPagina.IndexOf("#") > 0 Then strPagina = strPagina.SubString2(0, strPagina.IndexOf("#"))
'                Log("Page LOADED: "& strPagina)
    Navego (strPagina)

End Sub

private Sub Navego (Pagina As String)
    Select Pagina
        Case "DEFLOGIN.FACES","DEFINICIOMUISCA.FACES","DEFLOGINOLD.FACES"
            If Encontrar("vistaActuarAutorizacion:frmActuarAutorizacion") Then
                HacerLogin
                
            Else If Encontrar("vistaLogin:frmLogin") Then
                HacerLogin
            Else if Encontrar("vistaMenuUsuario:frmMenuUsuario") Then
                If Not(Sesion) Then
                    Sesion = True
                    Log("incio Sesion")
                    objSelenium.Navigate ( "https://salidademercancias.dian.gov.co/WebGestionexpediente/DefBandejaExpedientes.faces")
                End If
            End If
        Case "DEFMOSTRARREPORTEVACIO.FACES"
        Case "DEFLSTDETALLECASOEXPEDIENTE.FACES","DEFPAGPRINCIPALPALDUA.FACES"
            If Encontrar("vistaPagPrincipalpalDua:frmPagPrincipalpalDua:numeroDocAnterior") Then
                objSelenium.SendKeysByID("vistaPagPrincipalpalDua:frmPagPrincipalpalDua:numeroDocAnterior","6007662615738")
            End If
        Case Else
            Log("Pagina sin caso: """ & Pagina & """")
    End Select
End Sub

Private Sub HacerLogin
    objSelenium.SendKeys("vistaLogin:frmLogin:selNit","0")
    objSelenium.SendKeys("vistaLogin:frmLogin:txtNit","xxxxxxxx")
    objSelenium.SendKeys("vistaLogin:frmLogin:selTipoDoc","13")
    objSelenium.SendKeys("vistaLogin:frmLogin:txtUsuario","xxxxxxxxx")
    objSelenium.SendKeys("vistaLogin:frmLogin:txtCadena","xxxxxxxx")
    objSelenium.ClickByXPath ("//*[@id='vistaLogin:frmLogin']/table[3]/tbody/tr[2]/td/table/tbody/tr[1]/td[4]/input")

End Sub

private Sub Encontrar (Ruta As String) As Boolean
    Try
        Dim res As Boolean =  objSelenium.IsDisplayed(Ruta)
        Return res
    Catch
        Log(LastException)
    End Try
    Return False
End Sub
 

ToolboxZX

Member
Licensed User
Longtime User
does the wrapper support adding cookies similar to this method within the driver such as:

.addCookie(new Cookie("key", "value"));

Or can this be done using an attribute?
 

xavier66

New Member
It works perfect on release and debug but not with B4JPackager11 Version 1.21

C:\Users\xavier-gros\Documents\b4j\SampleOyster\SampleOyster\Objects\temp\build>cd bin

C:\Users\xavier-gros\Documents\b4j\SampleOyster\SampleOyster\Objects\temp\build\bin>java.exe @release_java_modules.txt -m b4j/com.visualnet.seleniumsample.main
[Java][Initialize] eventname = objSelenium
main._appstart (java line: -1)
java.util.ServiceConfigurationError: com.google.common.base.PatternCompiler: module b4j does not declare `uses`
at java.base/java.util.ServiceLoader.fail(Unknown Source)
at java.base/java.util.ServiceLoader.checkCaller(Unknown Source)
at java.base/java.util.ServiceLoader.<init>(Unknown Source)
at java.base/java.util.ServiceLoader.load(Unknown Source)
at b4j/com.google.common.base.Platform.loadPatternCompiler(Unknown Source)
at b4j/com.google.common.base.Platform.<clinit>(Unknown Source)
at b4j/com.google.common.base.Strings.isNullOrEmpty(Unknown Source)
at b4j/org.openqa.selenium.remote.internal.OkHttpClient$Factory.createClient(Unknown Source)
at b4j/org.openqa.selenium.remote.HttpCommandExecutor.<init>(Unknown Source)
at b4j/org.openqa.selenium.remote.HttpCommandExecutor.<init>(Unknown Source)
at b4j/org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(Unknown Source)
at b4j/org.openqa.selenium.chrome.ChromeDriverCommandExecutor.<init>(Unknown Source)
at b4j/org.openqa.selenium.chrome.ChromeDriver.<init>(Unknown Source)
at b4j/org.openqa.selenium.chrome.ChromeDriver.<init>(Unknown Source)
at b4j/org.openqa.selenium.chrome.ChromeDriver.<init>(Unknown Source)
at b4j/com.visualnet.selenium.Initialize(Unknown Source)
at b4j/com.visualnet.seleniumsample.main._appstart(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at b4j/anywheresoftware.b4a.BA.raiseEvent2(Unknown Source)
at b4j/anywheresoftware.b4a.BA.raiseEvent(Unknown Source)
at b4j/com.visualnet.seleniumsample.main.start(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)


C:\Users\xavier-gros\Documents\b4j\SampleOyster\SampleOyster\Objects\temp\build\bin>pause
Presione una tecla para continuar . . .
 
Top