Java 如何在 selenium webdriver 中使用断言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34202643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use assert in selenium webdriver?
提问by
Thanks to all in advance.
提前感谢大家。
i need to know how can i do assertion in selenium webdriver. My situation is i have one edit but on screen but that edit button is present on some certain criteria. so i want to check that if that button is present, it should be clicked on and it should open another child window and should perform certain actions. if that edit button element is not present on the screen, it should check next condition which is log off button in my keyword framework.
我需要知道如何在 selenium webdriver 中进行断言。我的情况是我有一个编辑但在屏幕上但该编辑按钮存在于某些特定条件下。所以我想检查该按钮是否存在,是否应该点击它,它应该打开另一个子窗口并应该执行某些操作。如果屏幕上不存在该编辑按钮元素,它应该检查下一个条件,即我的关键字框架中的注销按钮。
i have tried try and catch block and it working fine.here is the code:
我试过 try 和 catch 块,它工作正常。这是代码:
public void click_edit_childwindow2(String objectName) {
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
try{
WebElement elemnt = driver.findElement(By.xpath("//*[@id='main']/div[1]/table/tbody/tr[2]/td[6]/button"));
elemnt.click();
driver.findElement(By.xpath("//*[@id='main']/div[1]/table/tbody/tr[2]/td[6]/button")).click();
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.findElement(By.xpath("//*[@id='myModal']/div/div/div[2]/form/div/div[10]/div/button[1]")).click();
//Close the new window, if that window no more required
//driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
}catch(Exception e){
driver.findElement((By.xpath("//*[@id='logoutForm']/ul/li[2]/a"))).click();
}
}
but i want to do it with assert. though try and catch is not stopping the code but after testng execution it is showing that test case as failed. how can i do it with assert?
但我想用断言来做。尽管 try 和 catch 并没有停止代码,但是在 testng 执行之后,它显示该测试用例失败。我怎么能用断言做到这一点?
i am using keyword framework in which one is class is for keyword and other is for reading excel file.
我正在使用关键字框架,其中一个类用于关键字,另一个用于读取 excel 文件。
回答by ddavison
A typical Selenium setup will include a Test framework that is attached to your project.
典型的 Selenium 设置将包括一个附加到您的项目的测试框架。
There are several test frameworks to use, but here are the most popular for Java:
有多种测试框架可供使用,但以下是最流行的 Java 测试框架:
- jUnit
- TestNG
- ...
- jUnit
- 测试NG
- ...
When you have a test framework attached to your project, you could then use asserts like:
当您将测试框架附加到您的项目时,您可以使用如下断言:
Assert.assertTrue(driver.getWindowHandles().size().equals(2));
If the assertion fails, it will fail the test script. This is just one small example. I'd certainly do some research on how to use your test framework of choice. Selenium out-of-the-box is designed to be agnostic to what you use. Get creative.
如果断言失败,它将使测试脚本失败。这只是一个小例子。我当然会对如何使用您选择的测试框架进行一些研究。Selenium 开箱即用的设计与您使用的内容无关。发挥创意。
回答by Aritro Sen
At first you need to check wheather that button is displayed in the webpage. You can do that by this -
首先,您需要检查该按钮是否显示在网页中。你可以这样做 -
WebElement web=driver.findElement(By.xpath("//*[@id='myModal']/div/div/div[2]/form/div/div[10]/div/button[1]"));
boolean bool=web.isDisplayed();
Assert.assertEquals(true, bool); //here it will match that if that button is present or not. If present it will continue execution from the next line or if it is not present the execution stops there and your test case fails.
回答by Akhil K
You could avoid try catch by
你可以避免 try catch by
List <WebElement> editButton= driver.findElements(By.xpath("//*[@id='main']/div[1]/table/tbody/tr[2]/td[6]/button"));
if(editButton.size()>0)
{
editButton.get(0).click();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.findElement(By.xpath("//*[@id='myModal']/div/div/div[2]/form/div/div[10]/div/button[1]")).click();
//Close the new window, if that window no more required
//driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
}
else
{
driver.findElement((By.xpath("//*[@id='logoutForm']/ul/li[2]/a"))).click();
}
回答by Abhinav
Try below code:
试试下面的代码:
Boolean isPresent = driver.findElements(By.yourLocator).size() > 0
if(isPresent == true)
{
//code
}