1

I am trying to validate a pin code in my application. I am using Katalon and I have not been able to find an answer.

The pin code that I need to validate is the same length but different each time I run the test and looks like this on my page: PIN Code: 4938475948.

How can I account for the number changing each time I run the test?

I have tried the following regular expressions:

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")
);

Note: This was coded in Selenium and converted to Katalon.

Barry
  • 3,303
  • 7
  • 23
  • 42
mwidener
  • 11
  • 2

3 Answers3

1

In Katalon, use a combination of WebUI.getText() and WebUI.verifyMatch() to do the same thing.

E.g.

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)

Use also toInteger() or toString() groovy methods to convert types, if needed.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
1

Editing upper example but to get this working

 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)

This can be done as variable but in Your case I recommend using some java

import java.util.Random;

Random rand = new Random();

int  n = rand.nextInt(9000000000) + 1000000000;
// this will also cover the bad PIN (above limit)
siranen
  • 374
  • 1
  • 11
0

I'd tweak your regex just a little since your pin code is the same length each time: you could limit the number of digits that the regex looks for and make sure the following character is white space (i.e. not a digit, or another stray character). Lastly, use the "true" flag to let the WebUI.verifyMatch() know it should expect a regular expression from the second string (the regex must be the second parameter).

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)

Hope that helps!

nbburn
  • 80
  • 12