首页 > 解决方案 > 断言为真,变量来自两种方法

问题描述

网址:http ://automationpractice.com/ 登录:testingTest@gmail.com 通过:testtest

我正在尝试自动化购买衣服的过程。

问题是:

我有标签来完成我需要浏览每个标签的过程。地址|添加产品|条款和条件| 付款方式|付款方式| 总结| 我想从“添加的产品”选项卡中获取总价,然后从摘要中获取总价,并通过断言 True 检查它们是否相同。

@FindBy(id = "total_price")
private WebElement totalPriceOfOrder;


@FindBy(id = "total_price")
private WebElement totalPriceOfOrder;


public void checkTotalValueOfOrderInSummaryTab(){
    String orderPrice = totalPriceOfOrder.getText();
}


@FindBy(xpath = "/html/body/div/div[2]/div/div[3]/div/div/span")
private WebElement getPriceWhenOrderIsPaid;` 

public void getLastPrice(){
    String priceIsPaid = getPriceWhenOrderIsPaid.getText();
}

当我尝试断言此方法时,我收到错误,即无法找到 total_price 元素。当然这是不可能的,因为我在检查时位于不同的选项卡上。

那么如何解决呢?如何将价格从第一种方法分配给变量并从第二种方法将其断言为价格?

标签: javaseleniumautomationautomated-tests

解决方案


您想要做的是让您的 Get 方法返回该值,以便您的脚本可以存储它并在以后使用它。

例如,更改您的getLastPrice()方法以返回价格 ( String)

public String getLastPrice(){
    return getPriceWhenOrderIsPaid.getText();
}

然后在您的脚本中,您将存储返回的值以稍后执行断言,例如

String price = getLastPrice();
Assert.areEqual(totalPriceOfOrder.getText(), price, "Verify price");

注意:我不知道您发布的变量是哪个,我只是抓住了几个并以它们为例。


推荐阅读