在创建新的自定义关键字时,可以选择生成用于Web、移动或API测试的示例自定义关键字。它们提供了一个很好的例子来学习如何在Katalon Studio中从头开始创建具有适当编码约定的自定义关键字。
当您创建新的自定义关键字时,这些选项将直接显示。
Option | Description |
---|---|
Generate sample keywords for Web | 生成一些用于Web测试的示例函数 |
Generate sample keywords for Mobile | 生成一些用于移动测试的示例函数 |
Generate sample keywords for API | 生成一些用于API测试的示例函数 |
您可以选择一个或所有这些选项,以便在一个文件中生成所有示例自定义关键字。例如,选择所有选项,生成的自定义关键字如下所示:
class sampleKeywords {
/**
* Refresh browser
*/
@Keyword
def refreshBrowser() {
KeywordUtil.logInfo("Refreshing")
WebDriver webDriver = DriverFactory.getWebDriver()
webDriver.navigate().refresh()
KeywordUtil.markPassed("Refresh successfully")
}
/**
* Click element
* @param to Katalon test object
*/
@Keyword
def clickElement(TestObject to) {
try {
WebElement element = WebUiBuiltInKeywords.findWebElement(to);
KeywordUtil.logInfo("Clicking element")
element.click()
KeywordUtil.markPassed("Element has been clicked")
} catch (WebElementNotFoundException e) {
KeywordUtil.markFailed("Element not found")
} catch (Exception e) {
KeywordUtil.markFailed("Fail to click on element")
}
}
/**
* Get all rows of HTML table
* @param table Katalon test object represent for HTML table
* @param outerTagName outer tag name of TR tag, usually is TBODY
* @return All rows inside HTML table
*/
@Keyword
def List<WebElement> getHtmlTableRows(TestObject table, String outerTagName) {
WebElement mailList = WebUiBuiltInKeywords.findWebElement(table)
List<WebElement> selectedRows = mailList.findElements(By.xpath("./" + outerTagName + "/tr"))
return selectedRows
}
/**
* Check if element present in timeout
* @param to Katalon test object
* @param timeout time to wait for element to show up
* @return true if element present, otherwise false
*/
@Keyword
def isElementPresent_Mobile(TestObject to, int timeout){
try {
KeywordUtil.logInfo("Finding element with id:" + to.getObjectId())
WebElement element = MobileElementCommonHelper.findElement(to, timeout)
if (element != null) {
KeywordUtil.markPassed("Object " + to.getObjectId() + " is present")
}
return true
} catch (Exception e) {
KeywordUtil.markFailed("Object " + to.getObjectId() + " is not present")
}
return false;
}
/**
* Get mobile driver for current session
* @return mobile driver for current session
*/
@Keyword
def WebDriver getCurrentSessionMobileDriver() {
return MobileDriverFactory.getDriver();
}
/**
* Send request and verify status code
* @param request request object, must be an instance of RequestObject
* @param expectedStatusCode
* @return a boolean to indicate whether the response status code equals the expected one
*/
@Keyword
def verifyStatusCode(TestObject request, int expectedStatusCode) {
if (request instanceof RequestObject) {
RequestObject requestObject = (RequestObject) request
ResponseObject response = WSBuiltInKeywords.sendRequest(requestObject)
if (response.getStatusCode() == expectedStatusCode) {
KeywordUtil.markPassed("Response status codes match")
} else {
KeywordUtil.markFailed("Response status code not match. Expected: " +
expectedStatusCode + " - Actual: " + response.getStatusCode() )
}
} else {
KeywordUtil.markFailed(request.getObjectId() + " is not a RequestObject")
}
}
/**
* Add Header basic authorization field,
* this field value is Base64 encoded token from user name and password
* @param request object, must be an instance of RequestObject
* @param username username
* @param password password
* @return the original request object with basic authorization header field added
*/
@Keyword
def addBasicAuthorizationProperty(TestObject request, String username, String password) {
if (request instanceof RequestObject) {
String authorizationValue = username + ":" + password
authorizationValue = "Basic " + authorizationValue.bytes.encodeBase64().toString()
// Find available basic authorization field and change its value to the new one, if any
List<TestObjectProperty> headerProperties = request.getHttpHeaderProperties()
boolean fieldExist = false
for (int i = 0; i < headerProperties.size(); i++) {
TestObjectProperty headerField = headerProperties.get(i)
if (headerField.getName().equals('Authorization')) {
KeywordUtil.logInfo("Found existent basic authorization field. Replacing its value.")
headerField.setValue(authorizationValue)
fieldExist = true
break
}
}
if (!fieldExist) {
TestObjectProperty authorizationProperty = new TestObjectProperty("Authorization",
ConditionType.EQUALS, authorizationValue, true)
headerProperties.add(authorizationProperty)
}
KeywordUtil.markPassed("Basic authorization field has been added to request header")
} else {
KeywordUtil.markFailed(request.getObjectId() + "is not a RequestObject")
}
return request
}
}