场景是这样的......
网络元素中存在动态文本,由于其长度,有时末尾会包含“...”。例如敏捷的棕色狐狸...
我希望能够提取“...”之前的所有文本,然后将此文本与基准文本进行比较。基准文本包含完整的句子,即“敏捷的棕色狐狸跳过了懒惰的狗”。我希望能够提取提取的文本,并将文本的字符长度与基准文本的字符长度进行比较,这样我们就可以将苹果与苹果进行比较。所以就像这样 Get The Quick Brown Fox 与 The Quick Brown Fox 相比
提前致谢
最佳答案
基本前提是,如果被测试的字符串以点结尾,则检查预期的字符串是否以被测试的字符串开头(但没有点)。
下面的代码未经测试,您可能需要考虑 null
字符串场景。
static final String DOTS = "...";
String actual = "The quick brown fox...";
String expected = "The quick brown fox jumps over the lazy dog";
public void testStringWithDots(String actual, String expected) {
if (actual.endsWith(DOTS)) {
String prefix = actual.substring(0, actual.length() - DOTS.length());
Assert.assertTrue("Strings are same up to dots", expected.startsWith(prefix);
} else {
Assert.assertEquals("Strings are same", expected, actual);
}
}
关于java - Selenium - 从 Web 元素获取并比较指定长度的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18181710/