Katalon的常规方法是先抓取元素并保存到仓库,在脚本中需要用到的时候调取,但假如元素属性和个数是可变的,就不能事先保存到仓库了,需要在脚本运行时实时创建。

 

代码运行时实时创建一个元素对象的例子
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String dynamicId = 'Katalon123'
String xpath = '//div[@id="' + dynamicId + '"]'
TestObject to = new TestObject("objectName")
to.addProperty("xpath", ConditionType.EQUALS, xpath)
WebUI.click(to)

扩展:
'Change selection method from another selector to XPATH selector'
element.setSelectorMethod(SelectorMethod.XPATH)

'Change value of CSS selector'
element.setSelectorValue(SelectorMethod.CSS, 'your_desired_value')


但如果是有一组对象需要点击呢?比如每次页面生成不等的button,全部要点击一遍。

import org.openqa.selenium.WebDriver as Webdriver
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.By as By
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

'获取driver'
Webdriver driver = DriverFactory.getWebDriver()
'查找一组元素对象'
List elementsList = driver.findElements(By.xpath('//span[@class="menu-text"]'))
'循环遍历每个对象'
for (WebElement ele : elementsList) {
ele.click()
println(ele.text)
}


参考:
https://docs.katalon.com/katalon-studio/tutorials/handling_static_dynamic_test_objects.html
https://docs.katalon.com/katalon-studio/tutorials/using_selenium_webdriver_katalon_studio.html

内容来源于网络如有侵权请私信删除