本文介绍了在Selenium Webdriver中测试负流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:- webelement(按钮)isdisplayed()在负面情况下不起作用

Issue:- webelement (Button) isdisplayed() doesn't works for negative scenarios

要求:-如果屏幕上未显示按钮,并且该按钮存在,我需要使测试流程失败,然后继续进行流程

Requirement:- I need to fail a test flow in case a button is not displayed on the screen and if its present, then proceed with the flow

代码:-

if (driver.findElement(By.id("button")).isDisplayed() == false) {
System.out.println("The Button isn't present. Exiting!!");
driver.findElement(By.linkText("Logout")).click();
}
else
{
//Proceed with the positive flow
}

在上面的代码中,如果屏幕上根本没有按钮,则测试应该失败(如果应该执行语句,但不是)

In above code, if the button is not present at all on the screen, the test should fail (if statement should be executed, but it's not)

推荐答案

如TestAutomationEngr所述,请确保页面上只有一种这样的按钮...

As TestAutomationEngr has mentioned, make sure there is only one type of such button on the page...

您还可以使用 try and catch .

就您而言,

boolean buttonFound=false;
try
{
    new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.id("button")));

    buttonFound=true;

}catch(Exception e)
{
    System.out.println("The Button isn't present. Exiting!!");
    driver.findElement(By.linkText("Logout")).click();
}

if(buttonFound)
{
  //positive flow
}

在这里等待10秒钟,以使元素可见,

here it'l wait for 10 secs for visibility of element,

  • 如果找到, buttonFound设置为true,则执行正向流程

  • if found, buttonFound is set to true,hence positive flow is executed

如果未找到,则会显示catch子句中的消息,并单击注销链接

if not found, the message in catch clause will be displayed and logout link will be clicked

这篇关于在Selenium Webdriver中测试负流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:49