18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Getting a List of Links Using the Ruby Sahi Driver

CliftonClifton Members
edited November -1 in Sahi - Open Source
I would like to get a list of links on a page using the Sahi driver for Ruby. If possible, I would like to do this -without- having to change the code on the page, as done in post #1028. I would reference it, but I can't post links since I am a new user.

Background: I am trying to implement a set of test cases using Cucumber, and one of them requires that I select a random link (matching a specific set of rules) from the page, and then test that page for the presence of certain fields.

I was hoping I could use Sahi and Ruby to do this, yet I don't see how.

Yes, there are ways to access specific links, but it doesn't look like there is a way to get a list of elements matching a certain criterion (CSS rules?). I know this information should be available to Sahi when I am writing Cucumber test cases, I just don't know how to access it.

Any help would be greatly appreciated.

Thanks!

- Clifton

Comments

  • I should clarify this a bit. If there is a way to *inject* javascript into the page, and if this injection recognizes helper methods of the scripts already used by the page, then that would be an acceptable workaround. I finally understand the use of the <browser> tags, but I am not using Sahi scripts but pure Ruby code.

    With that said, I am experimenting with setBrowserJS()

    So far, I haven't gotten this working yet, either.

    Onwards...
  • So here is what I've managed to get working...

    def getElements(cssRule)
    js = <<JS
    $$('#{cssRule}').findAll(function(el) {
    if (el.href.match(/\\/activities.cfm\\?mode=edit/))
    return 1
    }).toArray()
    JS

    return (at)browser.fetch(js)
    end

    Now what this returns is a list of matching HREFs in a comma delimited string, not an array of Objects as I would expect. I could conceivably finish off the functionality I am looking to implement with this, however I to wonder...is there a way to get a list of DOM objects using either fetch() or set() (or whatever the Sahi equivalent is)?

    Thanks in advance for any assistance.

    PS - This is my third post to these forums. Can I PLEASE ask that you lift whatever limitation on special symbols so I can properly get support? I can't enter an "(at)" character, so I am forced to use imprecise aliases like "(at)"!
Sign In or Register to comment.