我正在使用Selenium自动化门户网站的UI。其中一个对话框包含文本
"Record with UUID:530d79e2-4d9a-4e8e-9114-da1431f0dd52 inserted successfully."
我如何为此文本设置断言,因为UUID每次都在不断更改?我有一个解决方法,如下所述。
Assert.assertTrue(string.contains("Record with UUID:");
Assert.assertTrue(string.contains("inserted successfully.");
但这对我来说看起来很糟糕。有什么建议以更清洁的方式做到吗?
答案 0 :(得分:2)
最简单的方法是使用String#matches函数,例如:
Assert.assertTrue(string.matches("Record with UUID:[0-9a-z\\-]+ inserted successfully."));
您可以使用更高级的正则表达式来验证UUID,例如从以下答案中获取: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.")
);
或其他:
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.")
);
此页面:Regular Expression Test Page for Java可用于再次测试各种输入文本的正则表达式。