B4J Library [b4j] jSelenium 2.0.1 - A Test Automation for B4J

This is my very first attempt to create a wrap for a library, It is based on this project:
- https://www.selenium.dev/downloads/
jSelenium - version 1.0

Usage​

* Third party Jar:

AdditionalJar:
#AdditionalJar: client-combined-3.14.0.jar
#AdditionalJar: animal-sniffer-annotations-1.14.jar
#AdditionalJar: byte-buddy-1.8.15.jar
#AdditionalJar: checker-compat-qual-2.0.0.jar
#AdditionalJar: commons-codec-1.10.jar
#AdditionalJar: commons-exec-1.3.jar
#AdditionalJar: commons-logging-1.2.jar
#AdditionalJar: error_prone_annotations-2.1.3.jar
#AdditionalJar: guava-31.1-jre.jar
#AdditionalJar: httpclient-4.5.5.jar
#AdditionalJar: httpcore-4.4.9.jar
#AdditionalJar: j2objc-annotations-1.1.jar
#AdditionalJar: okhttp-3.10.0.jar
#AdditionalJar: jsr305-1.3.9.jar
#AdditionalJar: selenium-api-4.3.0.jar
#AdditionalJar: selenium-chromium-driver-4.3.0.jar
#AdditionalJar: selenium-java-3.14.0.jar
#AdditionalJar: selenium-support-4.3.0.jar
#AdditionalJar: selenium-remote-driver-4.3.0.jar
#AdditionalJar: okio-1.14.1.jar
#AdditionalJar: opentelemetry-sdk-1.16.0.jar
#AdditionalJar: opentelemetry-sdk-extension-autoconfigure-spi-1.16.0.jar

Using:
ClassName
id
CssSelector
Xpath
LinkText
Name
Partiallinktext
Tagname        

Example:
jChrome.SelecElement("//a[contains(text(),'B4J - Desktop, Server and Raspberry Pi')]", "xpath")

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    jChrome.Initialize("jChrome", "D:\B4J\b4jSelenium\jSelenium\Driver", "chromedriver104.exe")
    jChrome.Create
End Sub

Sub Button1_Click  
    'jChrome.HideAutomation(True)
    jChrome.loadURL("https://www.showmyip.com/")
    Sleep(1000)
    jChrome.ReadCookies(File.DirApp, "fb.cookies")
    Sleep(500)
    jChrome.Refresh
End Sub

EVENTS

Events:
Sub jChrome_PageFinished (error As String, success As Boolean)
    Log("PageFinished:" & success)
End Sub
Sub jChrome_AfterClickOn ()
    Log("AfterClickOn")
End Sub
Sub jChrome_BeforeClickOn()
    Log("BeforeClickOn")
End Sub

Sub jChrome_BeforeAlertAccept()
    Log("BeforeAlertAccept")
End Sub
   
Sub jChrome_AfterAlertAccept()
    Log("AfterAlertAccept")
End Sub

Sub jChrome_BeforeAlertDismiss()
    Log("BeforeAlertDismiss")
End Sub
Sub jChrome_AfterAlertDismiss()
    Log("AfterAlertDismiss")
End Sub
   
Sub jChrome_AfterNavigateTo(url As String)
    Log(url)
End Sub
Sub jChrome_BeforeNavigateTo(url As String)
    Log(url)
End Sub
Sub jChrome_AfterNavigateBack(url As String)
    Log(url)
End Sub
Sub jChrome_BeforeNavigateBack(url As String)
    Log(url)
End Sub
Sub jChrome_BeforeNavigateForward(url As String)
    Log(url)
End Sub
Sub jChrome_AfterNavigateForward(url As String)
    Log(url)
End Sub
Sub jChrome_AfterNavigateRefresh(url As String)
    Log(url)
End Sub
Sub jChrome_BeforeNavigateRefresh(url As String)
    Log(url)
End Sub
Sub jChrome_AfterFindBy()
    Log("AfterFindBy")
End Sub
Sub jChrome_BeforeFindBy()
    Log("BeforeFindBy")
End Sub

Sub jChrome_AfterChangeValueOf(keysToSend As String)
    Log("AfterChangeValueOf")
End Sub

Sub jChrome_BeforeChangeValueOf(keysToSend As String)
    Log("BeforeChangeValueOf")
End Sub
Sub jChrome_BeforeScript(script As String)
    Log("BeforeScript")
End Sub

Sub jChrome_AfterScript(script As String)
    Log("AfterScript")
End Sub

Sub jChrome_BeforeSwitchToWindow(windowName As String)
    Log(windowName)
End Sub

Sub jChrome_AfterSwitchToWindow(windowName As String)
    Log(windowName)
End Sub

Sub jChrome_OnException(throwable As String)
    Log(throwable)
End Sub

Sub jChrome_BeforeGetScreenshotAs()
    Log("BeforeGetScreenshotAs")
End Sub

Sub jChrome_AfterGetScreenshotAs()
    Log("AfterGetScreenshotAs")
End Sub

Sub jChrome_BeforeGetText(value As String)
    Log("BeforeGetText")
End Sub

Sub jChrome_AfterGetText(value As String)
    Log("AfterGetText")
End Sub

Download: https://github.com/tummosoft/jSelenium/releases/tag/Selenium
 

Attachments

  • jSelenium-2.0.1.zip
    15.8 KB · Views: 152
Last edited:

mcqueccu

Well-Known Member
Licensed User
Longtime User
Thank you for the library. The 3rd party jars you added and the ones you specify in the example projects are of different version.

Edit: Seen the jar files now.

New issue: If the installed chrome driver is different from the chrome driver 104, it crashes. Is that the expected behaviour?

B4X:
Waiting for debugger to connect...
Program started.
Aug 07, 2022 7:50:25 PM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
Starting ChromeDriver 104.0.5112.29 (eff877e18f767c77fef0481a1cba402c8cbad404-refs/branch-heads/5112@{#422}) on port 19893
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1659901841.241][WARNING]: Deprecated chrome option is ignored: useAutomationExtension
[1659901841.241][WARNING]: Deprecated chrome option is ignored: useAutomationExtension
Error occurred on line: 49 (Main)
org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 104
Current browser version is 103.0.5060.134 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
Driver info: driver.version: ChromeDriver
remote stacktrace: Backtrace:
    Ordinal0 [0x01138823+2197539]
    Ordinal0 [0x010D0911+1771793]
    Ordinal0 [0x00FE3FE8+802792]
    Ordinal0 [0x01005B43+940867]
  ....
.
.

    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
 
Last edited:

tummosoft

Member
Licensed User
Longtime User
Thank you for the library. The 3rd party jars you added and the ones you specify in the example projects are of different version.

Edit: Seen the jar files now.

New issue: If the installed chrome driver is different from the chrome driver 104, it crashes. Is that the expected behaviour?

B4X:
Waiting for debugger to connect...
Program started.
Aug 07, 2022 7:50:25 PM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
Starting ChromeDriver 104.0.5112.29 (eff877e18f767c77fef0481a1cba402c8cbad404-refs/branch-heads/5112@{#422}) on port 19893
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1659901841.241][WARNING]: Deprecated chrome option is ignored: useAutomationExtension
[1659901841.241][WARNING]: Deprecated chrome option is ignored: useAutomationExtension
Error occurred on line: 49 (Main)
org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 104
Current browser version is 103.0.5060.134 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
Driver info: driver.version: ChromeDriver
remote stacktrace: Backtrace:
    Ordinal0 [0x01138823+2197539]
    Ordinal0 [0x010D0911+1771793]
    Ordinal0 [0x00FE3FE8+802792]
    Ordinal0 [0x01005B43+940867]
  ....
.
.

    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

Each version of ChromeDriver supports Chrome with matching major, minor, and build version numbers.

You can download other version and try:
https://chromedriver.chromium.org/downloads
 

miguelcleman

Member
Licensed User
Longtime User
Thank you for the library, but I get this error when try put text in a page :

java.lang.Exception: Sub jchrome_beforeclickon signature does not match expected signature

The examples show how put text but don't work.

Any help?
 

tummosoft

Member
Licensed User
Longtime User
Thank you for the library, but I get this error when try put text in a page :

java.lang.Exception: Sub jchrome_beforeclickon signature does not match expected signature

The examples show how put text but don't work.

Any help?


Library: 1.0.1, example and source code: Download

This is example How to use sendkeys:
-----------
 

miguelcleman

Member
Licensed User
Longtime User
Hi, I cant run exe file created from build standalone package option B4J (never show browser like in IDE with source from post #1)

The jar file, in objects folder, too cant run.

Thank for Help!
 

Tobias Leininger

Member
Licensed User
Longtime User
Hi, I am not really familiar with Selenium, but this library has been very helpful so far!

I do have a question, how can I read the value/text/content of an element?
 

juventino883

Member
Licensed User
Longtime User
Hi, I'm trying to set adtitional arguments to Selenium Browser with
B4X:
jChrome.Initialize("jChrome", "C:\path-to-driver\", "chromedriver.exe")
jChrome.addArgument(Array As String ("--incognito"))
jChrome.Create
but it seems that it's not working, also I'm trying to open the browser with the user's google profile with this instruction but it doesn't work
B4X:
jChrome.AddArgument(Array As String ("{'user-data-dir':'C:\Users\juven\AppData\Local\Temp\scoped_dir7444_539956608\Default'}")
probably I'm not using your library as it should @tummosoft ?
 

Anser

Well-Known Member
Licensed User
Longtime User
From where can I get the additional jars mentioned in the post #1
B4X:
#AdditionalJar: client-combined-3.14.0.jar
#AdditionalJar: animal-sniffer-annotations-1.14.jar
#AdditionalJar: byte-buddy-1.8.15.jar
#AdditionalJar: checker-compat-qual-2.0.0.jar
#AdditionalJar: commons-codec-1.10.jar
#AdditionalJar: commons-exec-1.3.jar
#AdditionalJar: commons-logging-1.2.jar
#AdditionalJar: error_prone_annotations-2.1.3.jar
#AdditionalJar: guava-31.1-jre.jar
#AdditionalJar: httpclient-4.5.5.jar
#AdditionalJar: httpcore-4.4.9.jar
#AdditionalJar: j2objc-annotations-1.1.jar
#AdditionalJar: okhttp-3.10.0.jar
#AdditionalJar: jsr305-1.3.9.jar
#AdditionalJar: selenium-api-4.3.0.jar
#AdditionalJar: selenium-chromium-driver-4.3.0.jar
#AdditionalJar: selenium-java-3.14.0.jar
#AdditionalJar: selenium-support-4.3.0.jar
#AdditionalJar: selenium-remote-driver-4.3.0.jar
#AdditionalJar: okio-1.14.1.jar
#AdditionalJar: opentelemetry-sdk-1.16.0.jar
#AdditionalJar: opentelemetry-sdk-extension-autoconfigure-spi-1.16.0.jar

I believe that all these additional jars are required to successfully compile/run the application

I found that most of the jars are available in the jSelenium_source.7z except few listed below.

#AdditionalJar: guava-31.1-jre.jar
#AdditionalJar: opentelemetry-sdk-1.16.0.jar
#AdditionalJar: opentelemetry-sdk-extension-autoconfigure-spi-1.16.0.jar
 
Last edited:

Anser

Well-Known Member
Licensed User
Longtime User
I am getting 'No such Element' errors. I understand that we get such errors if we try to access/locate/find an HTML element inside an iFrame.
In this library is there a way to handle this ?
 

Anser

Well-Known Member
Licensed User
Longtime User
Before you try searching for the elements within the iframe you will have to switch Selenium focus to the iframe. Hence we need to have a method in the library to switch to iFrame.

For eg
B4X:
driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));

Basically, we can switch over the elements and handle frames in Selenium using 3 ways.
By Index
By Name or Id
By Web Element

To exit from the iFrame and then switch back to the default, we need another method

B4X:
driver.switchTo().defaultContent();

tummusoft, would you mind adding these 2 methods to the library ?

B4X:
     public  SwitchToIFrame(String element, String ByProperties) {
         String prop = ByProperties.toLowerCase();
         try {
            if (prop.contains("classname")) {
                driver.switchTo().frame( driver.findElement(By.className(element)) );
            } else if (prop.contains("id")) {
                driver.switchTo().frame( driver.findElement(By.id(element)) );
            } else if (prop.contains("cssselector")) {
                driver.switchTo().frame( driver.findElement(By.cssSelector(element)) );
            } else if (prop.contains("xpath")) {
               driver.switchTo().frame( driver.findElement(By.xpath(element)) );
            } else if (prop.contains("linktext")) {
                driver.switchTo().frame( driver.findElement(By.linkText(element)) );
            } else if (prop.contains("name")) {
                driver.switchTo().frame( driver.findElement(By.name(element)) );
            } else if (prop.contains("partiallinktext")) {
                driver.switchTo().frame( driver.findElement(By.partialLinkText(element)) );
            } else if (prop.contains("tagname")) {
                driver.switchTo().frame( driver.findElement(By.tagName(element)) );
            }           
         } catch (Exception ex) {
            BA.LogError(ex.toString());
         }
      
     }


    public void SwitchtoDefault() {
        driver.switchTo().defaultContent();     
    }

I am not a JAVA developer and I haven't used Eclipse or cradle or anything hence don't know how to compile this to a library
 
Last edited:

tummosoft

Member
Licensed User
Longtime User
New update on jSelenium 2.0.1
---------------------
(1) Add the function to switch and interact with HTML frames
(2) Fix bugs with Argument options
(3) Enhance the function to export the application as an exe file.
----------------------
* Load Chrome Profiles:
jChrome.ProfilePath("c:\")
-----------
* If you want to publish your B4J project in EXE format, you need to add 2 additional options (see example below).

Options:
#PackagerProperty: AdditionalModuleInfoString = uses org.openqa.selenium.remote.session.CapabilitiesFilter;
#PackagerProperty: AdditionalModuleInfoString = uses org.openqa.selenium.remote.session.CapabilityTransform;

In the demo zip file, I have prepared a project that can be published in EXE format, if you want. In the source code folder on Github, there are also the necessary Java libraries for you to download. The source code is also uploaded to Github.

 

Attachments

  • jSelenium-2.0.1.zip
    15.8 KB · Views: 113
Last edited:

syerif

Active Member
Licensed User
Longtime User
Hi ,
i Got this error

java.lang.NullPointerException at com.tummosoft.Chrome.Create(Chrome.java:84)

Init:
jChrome.Initialize("jChrome", "D:\B4J\Selenium\chromedriver_win32", "chromedriver.exe")
    jChrome.Create

any solution

Thanks
 

tummosoft

Member
Licensed User
Longtime User
Hi ,
i Got this error

java.lang.NullPointerException at com.tummosoft.Chrome.Create(Chrome.java:84)

Init:
jChrome.Initialize("jChrome", "D:\B4J\Selenium\chromedriver_win32", "chromedriver.exe")
    jChrome.Create

any solution

Thanks
You must add this option:
jChrome.AddArgument(Array As String("--disable-notifications"))

B4X:
jChrome.Initialize("jChrome", "C:\Users\hienh\Downloads\chromedriver_win32", "chromedriver.exe")
    jChrome.AddArgument(Array As String("--disable-notifications"))
        
    jChrome.Create
 

syerif

Active Member
Licensed User
Longtime User
You must add this option:
jChrome.AddArgument(Array As String("--disable-notifications"))

B4X:
jChrome.Initialize("jChrome", "C:\Users\hienh\Downloads\chromedriver_win32", "chromedriver.exe")
    jChrome.AddArgument(Array As String("--disable-notifications"))
       
    jChrome.Create
Thank you it works, and how to scroll the window, current i work with scrap data from google maps any idea scroll window to bottom/end of data


Thanks
 
Top