B4J Library Selenium Browser Automation - Create any Robot with Browser

Discussion in 'B4J Libraries & Classes' started by Alberto Iglesias, Mar 31, 2019.

  1. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    [​IMG]

    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

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

    [​IMG]


    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
    [​IMG]



    Check with a few lines of code you can retrieve a table from nasdaq website
    Code:
    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:
    [​IMG]


    [​IMG]
     
    Last edited: May 21, 2019
    hibrid0, tchart, aeric and 14 others like this.
  2. Harris

    Harris Well-Known Member Licensed User

    Wee bit confused here.

    Can I use this - for example:

    Run this bot against my ( low powered VPS - web app (ABM) - stress test ) and determine what the maximum hit rate would be (before failure) - and what I need to upgrade the power of my VPS too (suggested)?

    This has always been a concern for me, since I have no idea what "level" of VPS power I need to handle expected load. I start with cheapest, since it is only me testing.

    Thanks
     
  3. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hello Harris,

    You can access your webapp throught Chrome? If yes, is possible to automate to get VPS power. This webapp have some public URL ? I can see?

    Thanks

    ALberto Iglesias
     
  4. icakinser

    icakinser Member Licensed User

    Is there a way to do this without the browser actually opening?
     
  5. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hello icakinser, no, this process with selenium is for interacting with browser...
     
    marcellissimo likes this.
  6. marcellissimo

    marcellissimo Member Licensed User

    Hi Alberto, I'm trying your library but objSelenium.ClickByXPath doesn't seem to work, work like objSelenium.Click
     
  7. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Hello my friend,

    I will check this, because I make tests with Xpath and works fine.. Can you send your sample to test? And you sure the xpath is correct?

    I'm using the XPATH FINDER for Chrome to get the correct xpaths... How you get the xpaths?
     
    marcellissimo likes this.
  8. aeric

    aeric Active Member Licensed User

    It looks like a web scraping tool. Am I correct?
     
  9. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Ye like this, but selenium was create for automation purposes and of course extract data from websites
     
  10. aeric

    aeric Active Member Licensed User

    Okay. Thanks. I shall check the samples when I am free.
     
  11. marcellissimo

    marcellissimo Member Licensed User

    thanks for the quick response

    link privacy from google ...
    for example:

    Code:
    #Region Project Attributes
        
    #MainFormWidth: 600
        
    #MainFormHeight: 600
    #End Region

    Sub Process_Globals
        
    Private fx As JFX
        
    Private MainForm As Form
        
    Dim objSelenium As Selenium
        
    Private robot As JavaObject
        
    Private Button1 As Button
        
    Private Button2 As Button
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
        MainForm.RootPane.LoadLayout(
    "btn"'Load the layout file.
        MainForm.Show
        objSelenium.DebugMode = 
    True
        objSelenium.Initialize(
    "chromedriver.exe","objSelenium")

        
    Dim jo As JavaObject
        jo.InitializeStatic(
    "com.sun.glass.ui.Application")
        robot = jo.RunMethodJO(
    "GetApplication",Null).RunMethodJO("createRobot",Null)
        objSelenium.Navigate(
    "https://www.google.com")
       
    End Sub

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

    Sub Button1_Click
        
    '//*[@id="fsr"]/a[1] <- from google copy xpath
        objSelenium.ClickByXPath("//*[@id=""fsr""]/a[1]")
        
    ' error : ->org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"//*[@id="fsr"]/a[1]"}
       
       
       
    End Sub



    '/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]

    Sub Button2_Click
        
    '/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1] <- from firefox copy xpath
        objSelenium.ClickByXPath("/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]")
        
    ' error : ->org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]"}
    End Sub

    errore...
    Code:
    apr 232019 1:33:47 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFORMAZIONI: Detected dialect: OSS
    [Java][EventB4]onInitialized
    [Java][Navigate]URL=https://www.google.com
    [Java][DEMO] Remember this 
    is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
    [Java][EventB4]onNavigate
    [Java][DEMO] Remember this 
    is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
    [Java][ClickByXPath]ElementID=/html/body/div/div[
    7]/div[1]/div/div/div/div/span[1]/a[1]
    Errore nella linea: 
    49 (Main)
    org.openqa.selenium.NoSuchElementException: no such element: Unable 
    to locate element: {"method":"id","selector":"/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]"}
      (Session info: chrome=73.0.3683.103)
      (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 0 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
    System info: host: 'DESKTOP-E1RMGQ1', ip: '192.168.1.38', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 73.0.3683.68 (47787ec04b6e3..., userDataDir: C:\Users\Marcy\AppData\Loca...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:54876}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.103, webStorageEnabled: true}
    Session ID: 650a3682eff78d20532e7d9d1eede32d
    *** Element info: {Using=id, value=/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
     
    Alberto Iglesias likes this.
  12. tchart

    tchart Active Member Licensed User

    This is great, can't wait to test this. I have used Selenium before but from PowerShell. Thanks for sharing!
     
    Alberto Iglesias likes this.
  13. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    @marcellissimo Hello my friend, sorry, I receive just today your message.

    You right, find by xpath I found one bug on library and I already fixed on version v1.0.1

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

    but for me, the xpath is:
    Code:
    objSelenium.ClickByXPath("/html/body/div[1]/div[8]/div[1]/div/div/div/div/span[1]/a[1]")
    Check this video:
     
    José J. Aguilar likes this.
  14. aeric

    aeric Active Member Licensed User

    I tested both buttons are working now. Thanks for the example.
    Code:
    Sub Button1_Click
        
    '//*[@id="fsr"]/a[1] <- from google copy xpath
        objSelenium.ClickByXPath($"//*[@id="fsr"]/a[1]"$)
    End Sub

    Sub Button2_Click
        
    '/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1] <- from firefox copy xpath
        objSelenium.ClickByXPath($"/html/body/div[1]/div[8]/div[1]/div/div/div/div/span[1]/a[1]"$)
    End Sub
     
    Alberto Iglesias likes this.
  15. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Great!!!!!!!!
     
  16. tchart

    tchart Active Member Licensed User

    Alberto, the SendKeys function seems to want the element name and not the id. Can you please add a second method for SendKeys by ID?
     
  17. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    @tchart You are completely right!

    Is fixed and I put on first post, library v1.0.2 you can see now SendKeysByID method!

    thanks for the feedback
     
    DonManfred likes this.
  18. tchart

    tchart Active Member Licensed User

    Thank you Alberto.
     
  19. fabio borges

    fabio borges Member Licensed User

    How to use headless mode?

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");
     
  20. Alberto Iglesias

    Alberto Iglesias Well-Known Member Licensed User

    Let me check this property on selelium driver and I will tell you asap....
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice