资产虚假陈述以检查是否显示 [英] Asset false statement to check if displayed

查看:70
本文介绍了资产虚假陈述以检查是否显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用硒编写测试.我已经在页面对象包中定义了所有Webelements.

I am trying to write a test in selenium. I have defined all my webelements in a page object package.

我正在使用S.getColumn()调用Web元素进行测试.

I am calling the webelement to my test using S.getColumn().

getcolumn()获取Web表中列的Web元素

getcolumn() gets the webelement of the column in the web table

  By column = By.xpath("//id[@type='column']");
  ..
  ..
  public WebElement getcolumn(){
  return driver.findElement(column)
  }

我需要检查测试中是否显示了网络元素

I need to check if the webelement is displayed or not in my test

我正在使用:Assert.assertFalse(S.getColumn().isDisplayed());

但是它抛出错误.我该如何正确写

but it throwing error. How do I write it correctly

推荐答案

断言不存在元素在硒中实际上不起作用,您通常会遇到一个异常,因为它试图与不存在的元素进行交互在页面上.您还没有共享完整的代码或得到的实际错误,但是我猜想您很可能以NoSuchElementException结尾.如果是这种情况,则需要将断言包装在try/中.赶上寻找那个异常.例如

Asserting an element isn't present doesn't really work in selenium, you usually get an exception because it's trying to interact with an element that isn't present on the page. You haven't shared the full code or the actual error you get, but at a guess I'd say you most likely end up with a NoSuchElementException If this is the case you'd need to wrap the assertion in a try/catch looking for that exception. e.g.

try {
        Assert.assertFalse(S.getColumn().isDisplayed())
    } catch(NoSuchElementException e) {
        LOG.info("Element not displayed as expected")
    }

有了这个,如果元素存在,您会断言失败,因为它期望它是假的.如果该元素不存在,则异常会将您带入陷阱,您可以在其中记录/打印所需的任何消息,然后进行测试

With this, you'd get an assertion failure if the element is present, as it's expecting it to be false. If the element isn't present, the exception would drop you into the catch where you can just log/print whatever message you want and the test can carry on

这篇关于资产虚假陈述以检查是否显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆