Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Using Silk Test, how can I click a random HtmlLink on a web-page?

Using Silk Test, how can I click a random HtmlLink on a web-page?

In Silk Test a random link on a Webpage can be picked using the code below. This code is has been tested against www.google.com and uses the RandPick() method to randomly chose an element from a list. In this case, we have created a list of all HtmlLinks on the Google page.

[-] testcase two () appstate none
    [ ] 
    [ ] list of window lwChildren 
    [ ] list of window lwLinks
    [ ] window wChild
    [ ] 
    [ ] Google.SetActive ()
 [ ] // perform a GetChildren method on the page to return all the objects on the page
    [ ] listprint (Google.GetChildren ())
 [ ] // print out the list
    [ ] print()
 [ ] // save list to a list of window variable called lwChildren
    [ ] lwChildren = Google.GetChildren  ()
    [ ] 
    [ ] 
 [ ] // loop through your complete list of objects and using the GetClass, return only
    [ ] // those objects which are of class HtmlLink and save them to a new list of window var
    [-] for each wChild in lwChildren 
        [-] if wChild.GetClass () == HtmlLink
            [ ] listappend (lwLinks, wChild)
    [ ] 
 [ ] // print out list of links on the page   [ ] print ()
    [ ] listprint (lwLinks)
    [ ] 
    [ ] 
    [ ] // Using the new list of window lwLinks, you can now loop through and directly 
 [ ] // click on every link on the page
    [-] for each wChild in lwLinks
        [ ] wChild.Click ()
        [ ] Sleep (0.5)
        [ ] Browser.Back.Click ()
        [ ] Sleep (0.5)
        [ ] 
    [ ] 
 [ ] // use the RandPick function to randomly pick a link from the list of links on the page
    [ ] // and click it.
    [ ] window sRandomLink = RandPick (lwLinks)
    [ ] print()
    [ ] print ("Randomly selected link: ", sRandomLink)
    [ ] sRandomLink.Click ()

Related documents:

Old KB# 21077

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:16
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.