简体   繁体   中英

How to verify text in dialog box when it has dynamic text

I am using Selenium to automate UI of our portal. One of the dialog boxes has the text

"Record with UUID:530d79e2-4d9a-4e8e-9114-da1431f0dd52 inserted successfully."

How do i put an assert for this text as UUID keeps changing every time? One workaround that I have is mentioned below.

Assert.assertTrue(string.contains("Record with UUID:");


Assert.assertTrue(string.contains("inserted successfully.");

But this to me looks like a bad approach. Any suggestions to do it in a cleaner way?

The simplest way is to use String#matches function, for example:

Assert.assertTrue(string.matches("Record with UUID:[0-9a-z\\-]+ inserted successfully."));

You can use more advanced regular expression for validating UUID, for example from this answer: java regex for UUID

Assert.assertTrue(
  string.matches("Record with UUID:[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12} inserted successfully.")
);

or another:

Assert.assertTrue(
  string.matches("Record with UUID:[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12} inserted successfully.")
);

This page: Regular Expression Test Page for Java can be used to test regular expressions agains various input text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM