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 ()