gpt4 book ai didi

java - 避免使用自定义 WebDriver 谓词进行额外工作的更好方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:12 25 4
gpt4 key购买 nike

在我的 webdriver 测试用例中,我必须从 URL 中解析出一个数字(如果不存在,则测试失败),但首先等待我被重定向到包含数字的 URL。例如,当我转到“localhost/#createRecord”时,我将被重定向到“localhost/#editRecord/some-number”

    wait.until(ExpectedConditions.urlMatches("#editRecord/([0-9]*)$"));
String url = driver.getCurrentUrl();
Pattern p = Pattern.compile("#editRecord/([0-9]*)$");
Matcher m = p.matcher(url);

int newStudentId=0;
if (m.find()) {
newStudentId = Integer.parseInt(m.group(1));
System.out.println(m.group(1)); // The matched substring
} else fail("There is no ID number in #editRecord page");

在这种情况下,在 wait.until(...) 之后实际上发生了额外的匹配工作经过一番谷歌搜索后,我找到了这种代码变体。成功后立即保存解析结果:

    Pattern p = Pattern.compile("#editRecord/([0-9]*)$");
final int[] newStudentId = new int[1]; //declared like this because otherwise we won't be able to assign value to it from lambda expression
try {
wait.until((WebDriver driver) -> {
String url = driver.getCurrentUrl();
Matcher m = p.matcher(url);
if (m.find()) {
newStudentId[0] = Integer.parseInt(m.group(1));
return true;
} else return false;
});
} catch (org.openqa.selenium.TimeoutException e) {
fail("There is no ID number in #editRecord page or we haven't been redirected to #editRecord page");
}
System.out.println(newStudentId[0]);

两个版本都可以工作,但我不确定就可读性(我以前没有使用过 lambda,也不完全理解它)和可靠性而言,哪个版本更好。另外,在第二个选项中捕获超时异常比第一个选项涵盖更多的失败情况

最佳答案

如果你经常等待URL改变,你可以编写自己的方法并多次使用:

public void waitForUrlChange(final WebDriver driver, final String previousUrl, final int waitTime) {
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until((WebDriver d) -> !d.getCurrentUrl().equals(previousUrl));
}

或者等待 URL 更改为给定 URL:

public void waitForUrlToEqual(final WebDriver driver, final String urlToEqual, final int waitTime) {
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until((WebDriver d) -> d.getCurrentUrl().equals(urlToEqual));
}

或者等待 URL 匹配模式:

public void waitForUrlToMatch(final WebDriver driver, final String patternToMatch, final int waitTime) {
WebDriverWait wait = new WebDriverWait(driver, waitTime);
final Pattern pattern = Pattern.compile(patternToMatch);
wait.until((WebDriver d) -> {
Matcher matcher = pattern.matcher(d.getCurrentUrl());
return matcher.find();
});
}

在这些方法中,每 500 毫秒调用一次 -> 之后的部分,直到其计算结果为 true。如果在 waitTime 内不为 true,则会抛出 TimeoutException

您的第一个解决方案...

对比:

  • 您使用了两次"#editRecord/([0-9]*)$"。您应该声明一个 String 变量并重用它。

你的第二个解决方案...
优点:

  • 异常处理。但只需将 wait.until 调用与 try-catch block 结合起来即可将其添加到第一个解决方案中。

对比:

  • 您必须声明一个 final 变量。

如果没有,我会选择更具描述性的内容,如下所示:

int newStudentId = 0;
String urlPattern = "#editRecord/([0-9]*)$";
String failMessage = "There is no ID number in #editRecord page";
try {
wait.until(ExpectedConditions.urlMatches(urlPattern));
String url = driver.getCurrentUrl();
Pattern p = Pattern.compile(urlPattern);
Matcher m = p.matcher(url);
if (m.find()) {
newStudentId = Integer.parseInt(m.group(1));
System.out.println(newStudentId);
} else {
throw new IndexOutOfBoundsException(failMessage);
}
} catch (org.openqa.selenium.TimeoutException | IndexOutOfBoundsException e) {
fail(failMessage);
}

而且看起来很清楚,多了一个字:
感谢您对 lambda 的澄清...这让我思考!

关于java - 避免使用自定义 WebDriver 谓词进行额外工作的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42234807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com