List randomObjects = Arrays.asList(findTestObject(’//[@id=“rso”]/div[5]’), findTestObject(’//[@id=“rso”]/div[6]’), findTestObject(’//*[@id=“rso”]/div[7]’))
Random rand = new Random()
String randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
WebUI.click(findTestObject(randomPath))
And getting the error below:
Test Cases/TestID01/ID01 FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: Script1640442596283.findTestObject() is applicable for argument types: (java.lang.String) values: [//*[@id=“rso”]/div[5]]
You have your array as a list of “findTestObject”. Then, in your “click” method, you again add another “findTestObject”. I would suggest not to do it like this. How about just have your array as Strings, then add the calculated String as your “findTestObject”, like below:
An alternative is to use a list of Objects and have “findTestObject” attached to the list of Strings and remove it from the “click” method. Note that “randomPath” then is an Object, not a String.
Maybe like (as a list of Objects):
String pageUrl = "https://www.google.com/search?q=best+car&sxsrf=AOaemvKORhcnjk1mD0Ltw6WbeFS6JHK7Yw%3A1640466419177&source=hp&ei=84fHYdPRB4emUKCkqcgI&iflsig=ALs-wAMAAAAAYceWA0-GPHpFPqinjlw2KznXSRnvIBmt&ved=0ahUKEwiTl7bR7f_0AhUHExQKHSBSCokQ4dUDCAc&uact=5&oq=best+car&gs_lcp=Cgdnd3Mtd2l6EAMyBAgjECcyBQgAEJECMgUIABCABDIICAAQgAQQsQMyCAgAEIAEELEDMgUILhCABDILCAAQgAQQsQMQgwEyBQgAEIAEMggIABCxAxCDATILCAAQgAQQsQMQgwFQAFjMEGC3EWgAcAB4AIABnQSIAY8IkgEFNC0xLjGYAQCgAQE&sclient=gws-wiz"
WebUI.openBrowser("")
WebUI.navigateToUrl(pageUrl)
WebUI.waitForPageLoad(10)
List randomObjects = Arrays.asList(findTestObject('id("rso")/div[5]'), findTestObject('id("rso")/div[6]'), findTestObject('id("rso")/div[7]'))
Random rand = new Random()
def randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
WebUI.click(randomPath)
Thanks a lot.
Please suggest also that how i get only a href tags.lnks using xpath from google page in above code in the katalon in below list
List randomObjects = Arrays.asList(’//[@id=“rso”]/div[5]’, ‘//[@id=“rso”]/div[6]’, ‘//*[@id=“rso”]/div[7]’)
I am not sure I correctly understand your question, but a xpath with an href tag and id tag can be formed similarly as a String. Perhaps something like:
The link states that the function returns a list of Strings, so you could loop through them and pull out those that contain, “href”. Something like below:
WebUI.openBrowser(pageUrl)
WebUI.maximizeWindow()
WebUI.waitForPageLoad(10)
List newList = []
List randomObjects = WebUI.getAllLinksOnCurrentPage(true, [])
int cnt = 0
for(String aObj : randomObjects) {
WebUI.comment( aObj )
println( aObj );
if (aObj.indexOf("www") > -1) {
newList.add(aObj)
cnt++;
}
}
WebUI.comment( "found ${cnt}")
I ran a test of the page for the href tag and it returned zero, but I know there are some on the page. So, I don’t think getAllLinksOnCurrentPage is returning what you want because you may not be able to verify that the links were all associated with an href.
Thanks Brandon_Hein
I am still confused that how i will extract only href links on google search keyword page like: https://www.google.com/search?q=best+car&biw=1366&bih=…
in katalon using this code:
List links = WebUiCommonHelper.findWebElements(findTestObject(‘path/to/object’), 30);
Can u suggest the complete code?
Thanks
so the final list will be like:
List links = WebUiCommonHelper.findWebElements(findTestObject(’//div[@id=“rso”]//a[./h3]’), 30);
(’//div[@id=“rso”] this the xpath of whole div that conatins all a hrefs ?
No, when you call findTestObject(‘path/to/object’), you are referencing a Test Object in the Object Repository. This test object is what holds your locator, in this case your xpath of //a[./h3]. So you’d create a new test object with the xpath I gave you, then call it with WebUiCommonHelper.findWebElement(findTestObject(…)). For a reference on creating test objects, see https://docs.katalon.com/katalon-studio/docs/manage-web-test-object.html#in-manual-view
OK i understand what u r saying but the point is how many xpath i will give to this testobject to find all //a[./h3] headings. Means that there are let say 5 h3 headings are in search page now i need to mention xpath of all 5 h3 in my object ??
please suggest ?
If you are expecting only one WebElement, then you use: findWebElement (singular). If you want more than one WebElement, then you use: findWebElements (plural).
Something like:
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
String pageUrl = "https://www.google.com/search?q=best+car&sxsrf=AOaemvKORhcnjk1mD0Ltw6WbeFS6JHK7Yw%3A1640466419177&source=hp&ei=84fHYdPRB4emUKCkqcgI&iflsig=ALs-wAMAAAAAYceWA0-GPHpFPqinjlw2KznXSRnvIBmt&ved=0ahUKEwiTl7bR7f_0AhUHExQKHSBSCokQ4dUDCAc&uact=5&oq=best+car&gs_lcp=Cgdnd3Mtd2l6EAMyBAgjECcyBQgAEJECMgUIABCABDIICAAQgAQQsQMyCAgAEIAEELEDMgUILhCABDILCAAQgAQQsQMQgwEyBQgAEIAEMggIABCxAxCDATILCAAQgAQQsQMQgwFQAFjMEGC3EWgAcAB4AIABnQSIAY8IkgEFNC0xLjGYAQCgAQE&sclient=gws-wiz"
WebUI.openBrowser(pageUrl)
WebUI.maximizeWindow()
WebUI.waitForPageLoad(10)
"create a TestObject in code instead of from the Object Repository"
TestObject myObject = new TestObject()
myObject.addProperty("xpath", ConditionType.EQUALS, "//a[./h3]")
"make a list of all the WebElements"
List<WebElement> links = WebUiCommonHelper.findWebElements(myObject, 30); // see, its plural
"randomly choose one of the WebElements"
links.get(new Random().nextInt(links.size() - 1)).click();
You need to change the last line to “collect” the random choice, then use the random choice in the “click” method, like:
"make a list of all the WebElements"
List<WebElement> links = WebUiCommonHelper.findWebElements(myObject, 30); // see, its plural
"randomly choose one of the WebElements (to save)"
def myElement = links.get(new Random().nextInt(links.size() - 1))
"use the random choice"
myElement.click();
There is one more problem i want to sahre that
sometimes the
def myElement = links.get(new Random().nextInt(links.size() - 1))
“use the random choice”
myElement.click();
work perfectly and open the clicked URL but sometimes it fails and says:
Test Cases/ID01/TestCaseID01 FAILED.
Reason:
org.openqa.selenium.ElementNotInteractableException: element not interactable
what can be the reason u understand?
please suggest!
Thanks!
You are selecting one of several possible links. Perhaps the one you are selecting is either not ready, not in the viewport, or some other possibility. So how about:
"randomly choose one of the WebElements (to save)"
def myElement = links.get(new Random().nextInt(links.size() - 1))
"move to put the element on the screen"
WebUI.scrollToPosition(100, myElement.getLocation().getY())
WebUI.delay(1)
"use the random choice"
myElement.click();
Hi
sorry, this method didn’t work. Can we have any other option like getting the link into visibility so that the random link may be clicked successfully ?
it said:
Reason:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element … is not clickable at point (418, 8). Other element would receive the click: