0

我正在为我的 SpringBoot 应用程序开发一个 selenium 测试,我已经成功地创建了一个新帐户,在该帐户上创建了一个新项目并将其值设置为 funds=0,然后我向该项目捐款,并重定向到页面它显示了一个用户的贡献,包括 'amountContributed' 元素,其值 > 0 作为捐赠。

如何在 Selenium 测试中检查元素的值?我的尝试在下面,我得到的错误在下面:

//重定向到用户贡献页面。assertEquals("你的贡献", this.webDriver.getTitle());

    //Assert that the total contributed amount for the project is 2000 as it was 0 prior to contribution.
    assertEquals("2000", this.webDriver.findElement(By.id("amountContributed")));


expected: <2000> but was: <[[ChromeDriver: chrome on WINDOWS (60e2cb3895ae1f8e300b2068bd0f46c7)] -> id: amountContributed]>

HTML:

<span>Amount Contributed: </span>
<li th:text="${project.amountContributed}" id="amountContributed">Amount Contributed</li>
4

1 回答 1

0

看到这个

this.webDriver.findElement(By.id("amountContributed"))

将返回一个 web 元素,并且您正在将一个web 元素与字符串"2000"进行比较。这就是您收到以下异常的原因:

预期:<2000> 但为:<[[ChromeDriver: WINDOWS 上的 chrome (60e2cb3895ae1f8e300b2068bd0f46c7)] -> id: amountContributed]>

试试这个:

assertEquals("2000", this.webDriver.findElement(By.id("amountContributed")).getText());

但是您共享的 HTML :

<span>Amount Contributed: </span>
<li th:text="${project.amountContributed}" id="amountContributed">Amount Contributed</li>

表明如果您amountContributed用作 id,.getText()将返回

Amount Contributed

字符串,您的断言将再次失败。

于 2021-08-12T12:26:26.473 回答