在测试中验证动态文本

问题描述 投票:1回答:3

我想在我的应用程序中验证密码。我正在使用Katalon,但我找不到答案。

我需要验证的密码长度相同但每次运行测试时都不同,在我的页面上看起来像这样:PIN码:4938475948。

如何在每次运行测试时考虑更改的数量?

我尝试了以下正则表达式:

assertEquals(
    "PIN Code: [^a-z ]*([.0-9])*\\d", 
    selenium.getText("//*[@id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span")
);

注意:这是在Selenium编码并转换为Katalon。

testing katalon-studio regression-testing
3个回答
1
投票

在Katalon中,使用WebUI.getText()WebUI.verifyMatch()的组合来做同样的事情。

EG

TestObject object = new TestObject().addProperty('xpath', ConditionType.EQUALS, '//*[@id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span')
def actualText = WebUI.getText(object) 
def expectedText = '4938475948'
WebUI.verifyMatch(actualText, expectedText, true)

如果需要,还可以使用toInteger()toString() groovy方法转换类型。


1
投票

编辑上面的示例,但要使其正常工作

 TestObject object = new TestObject().addProperty('xpath', ConditionType.EQUALS, '//*[@id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span')
        def actualText = WebUI.getText(object) 
        def expectedText = '4938475948'
        WebUI.verifyMatch(actualText, expectedText, true)

这可以作为变量完成,但在您的情况下,我建议使用一些Java

import java.util.Random;

Random rand = new Random();

int  n = rand.nextInt(9000000000) + 1000000000;
// this will also cover the bad PIN (above limit)

0
投票

我稍微调整你的正则表达式,因为你的pin码每次都是相同的长度:你可以限制正则表达式所寻找的位数,并确保后面的字符是空格(即不是数字或其他杂散)字符)。最后,使用“true”标志让WebUI.verifyMatch()知道它应该期望第二个字符串的正则表达式(正则表达式必须是第二个参数)。

def regexExpectedText = "PIN Code: ([0-9]){10}\\s"
TestObject pinCodeTO = new TestObject().addProperty('xpath', ConditionType.EQUALS, '//*[@id='RegItemContent0']/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div[1]/div[3]/ul/li[2]/span')
def actualText = WebUI.getText(pinCodeTO) 

WebUI.verifyMatch(actualText, expectedText, true)

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.