Passing WebElement through Keywords

Hello,

I’m trying to face to following problem:

I’ve written a TestCase in which I use a CustomKeyword A to test a Table represented by a WebElement.

TestCase

WebDriver driver = DriverFactory.getWebDriver()

WebElement table = driver.findElement(By.id(‘elementA’))

CustomKeywords.A.testMyElement(table)

That works properly so far. Inside of Keyword A I call a method of Keyword B and try to pass table as an argument.

Keyword A

@Keyword

def testMyElement(WebElement table){

int n = B.getNum(table)

// Alternative

// int n = B.getNum((RemoteWebElement) table)

}

Keyword B

@Keyword
def getNum(WebElement table){

// fency

}

This causes a groovy.lang.MissingMethodException:

No signature of method: static B.getNum() is applicable for argument types: (org.openqa.selenium.remote.RemoteWebElement) values: [[[ExistingRemoteWebDriver: on ANY (0f59c1c1cbf9f75d05c48816884cc0d3)] → id: elementA]]
Possible solutions: getTotalAmountOfRows(org.openqa.selenium.remote.RemoteWebElement)

I tried to cast table to RemoteWebElement but that didn’t help. And it didn’t help to change the Argument Type at getNum() from WebElement to RemoteWebElement as well.

Is it possible to pass WebElements through Keywords in general? Waht could solve my problem?

Hi Paul,

RemoteWebElement implements WebElement, so there shouldn’t be a problem. Do you have correct imports in your Keywords?

One more thought - try to use static methods instead of keywords:

TestCase
WebDriver driver = DriverFactory.getWebDriver()
WebElement table = driver.findElement(By.id('elementA'))
A.testMyElement(table)
Keyword Apublic static void testMyElement(WebElement table){
int n = B.getNum(table)}Keyword Bpublic static void getNum(WebElement table){// fency
}
1 Like

Thank you really much for your answer, Marek - I didn’t expect one that quickly. Your second succestion solved my problem. A.testMyElement is still a Keyword, but B.getNum is a public method now. Quite simple solution in fact.

TestCase
WebDriver driver = DriverFactory.getWebDriver()
WebElement table = driver.findElement(By.id('elementA'))
A.testMyElement(table)
Keyword A@Keyword
def testMyElement(WebElement table){
int n = B.getNum(table)}Keyword Bpublic static void getNum(WebElement table){// fency
}

So can I say in generally that only parts, that get called directly in a test case should be marked as @Keyword with def and others should have a signature as plain java code?