如何在 Junit 中使用 Assert 而不是 if else?
How to use Assert in Junit instead of if else?
我有一种搜索方法,在第一页上有 link 维基百科:
public void findWiki() {
TryLink = chromeDriver.findElement(By.xpath("//a[@href='https://ru.wikipedia.org/wiki/%D0%A8%D0%BF%D0%B0%D0%B6%D0%BD%D0%B8%D0%BA']"));
if (TryLink.isDisplayed()) {
System.out.println("Yes link is there");
} else {
System.out.println("No link is there");
}
}
该方法的实现:
@Test
public void googleSearchPF() {
chromeDriver.get("https://www.google.com/webhp?hl=en&sa=X&ved=0ahUKEwi88p6D9vbyAhXhkIsKHffmA_oQPAgI");
GoogleSearchPF googleSearchPF = PageFactory.initElements(chromeDriver, GoogleSearchPF.class);
googleSearchPF.find("Gladiolus");
googleSearchPF.findWiki();
}
测试有效,一切正常 - 它找到 link。但是如何使用 assertTrue 实现 link 检查呢?如果是,具体如何?
看来应该这样实现:
Assertions.assertTrue(googleSearchPF.getAllElements().stream().anyMatch(x->x.isDisplayed()),
"Wikipedia was not found");
要使您的 findWiki()
具有断言,可以按以下方式完成:
public void findWiki() {
TryLink = chromeDriver.findElement(By.xpath("//a[@href='https://ru.wikipedia.org/wiki/%D0%A8%D0%BF%D0%B0%D0%B6%D0%BD%D0%B8%D0%BA']"));
assertTrue(TryLink.isDisplayed(),"No link is there"));
}
如果你看到 assertTrue
:
assertTrue
public static void assertTrue(java.lang.String message,
boolean condition)
Asserts that a condition is true. If it isn't it throws an AssertionError with the given message.
Parameters:
message - the identifying message for the AssertionError (null okay)
condition - condition to be checked
现在,您的 findWiki()
方法确实包含 if 和 else 块,我相信您想要断言并摆脱传统的 if else
来检查 conditions
。
代码:
public void findWiki() {
List<WebElement> TryLink = driver.findElements(By.xpath("//a[@href='https://ru.wikipedia.org/wiki/%D0%A8%D0%BF%D0%B0%D0%B6%D0%BD%D0%B8%D0%BA']"));
int size = TryLink.size() ;
assertTrue(size > 0, "try link exists");
}
基本上我们使用 findElements
并检查大小,如果它是 >0
然后在 assertTrue.
内解析 像这样 assertTrue(size > 0, "try link exists");
我有一种搜索方法,在第一页上有 link 维基百科:
public void findWiki() {
TryLink = chromeDriver.findElement(By.xpath("//a[@href='https://ru.wikipedia.org/wiki/%D0%A8%D0%BF%D0%B0%D0%B6%D0%BD%D0%B8%D0%BA']"));
if (TryLink.isDisplayed()) {
System.out.println("Yes link is there");
} else {
System.out.println("No link is there");
}
}
该方法的实现:
@Test
public void googleSearchPF() {
chromeDriver.get("https://www.google.com/webhp?hl=en&sa=X&ved=0ahUKEwi88p6D9vbyAhXhkIsKHffmA_oQPAgI");
GoogleSearchPF googleSearchPF = PageFactory.initElements(chromeDriver, GoogleSearchPF.class);
googleSearchPF.find("Gladiolus");
googleSearchPF.findWiki();
}
测试有效,一切正常 - 它找到 link。但是如何使用 assertTrue 实现 link 检查呢?如果是,具体如何?
看来应该这样实现:
Assertions.assertTrue(googleSearchPF.getAllElements().stream().anyMatch(x->x.isDisplayed()),
"Wikipedia was not found");
要使您的 findWiki()
具有断言,可以按以下方式完成:
public void findWiki() {
TryLink = chromeDriver.findElement(By.xpath("//a[@href='https://ru.wikipedia.org/wiki/%D0%A8%D0%BF%D0%B0%D0%B6%D0%BD%D0%B8%D0%BA']"));
assertTrue(TryLink.isDisplayed(),"No link is there"));
}
如果你看到 assertTrue
:
assertTrue
public static void assertTrue(java.lang.String message,
boolean condition)
Asserts that a condition is true. If it isn't it throws an AssertionError with the given message.
Parameters:
message - the identifying message for the AssertionError (null okay)
condition - condition to be checked
现在,您的 findWiki()
方法确实包含 if 和 else 块,我相信您想要断言并摆脱传统的 if else
来检查 conditions
。
代码:
public void findWiki() {
List<WebElement> TryLink = driver.findElements(By.xpath("//a[@href='https://ru.wikipedia.org/wiki/%D0%A8%D0%BF%D0%B0%D0%B6%D0%BD%D0%B8%D0%BA']"));
int size = TryLink.size() ;
assertTrue(size > 0, "try link exists");
}
基本上我们使用 findElements
并检查大小,如果它是 >0
然后在 assertTrue.
内解析 像这样 assertTrue(size > 0, "try link exists");