katalon系列十六:程式碼執行時實時建立元素物件或列表
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