提问者:小点点

如何使用Cucumber/Selenium/Java将Scenario Outline上的Examples中的值与Actual页面上的Actual值进行比较


给定:打开应用程序

时间:输入用户名和密码

然后:用户能够登录

并验证实际价格

并与预期价格进行比较

例子:

|Actual Price                           | Expected Price|

|"//div[@class='actual_price']//span[2]"|    USD 100.00| 

|"//div[@class='actual_price']//span[2]"|    USD 200.00|

步骤def获取实际价格是:

@And("^verify the \"([^\"]*)\"$")
public void gettxt(String expectedPrice) throws Throwable {
    String actualprice= driver.findElement(By.xpath(actualprice)).getText();

    try{
    if(expectedPrice.equals(actualPrice)){
        System.out.println("Price is correct");
        System.out.println("Expected Price: " + expectedPrice);
        System.out.println("Actual Price: " + actualPrice);
    }else{
        System.out.println("Price is not correct");
        System.out.println("Expected Price: " + expectedPrice);
        System.out.println("Actual Price: " + actualPrice);
    }
}catch (Exception e){
    return;
    }

问题是我如何比较实际价格和预期价格。谢谢你的帮助..示例中的实际价格列是获取UI上显示的实际值的文本的xpath,那么预期价格列就是预期值。任何帮助都将不胜感激...


共1个答案

匿名用户

你在这里输入的代码似乎不正确。

在步骤定义中匹配Gherkin的正则表达式将采用示例中的xpath。此xpath字符串将根据您定义的函数存储在变量预期价格中。

By Your语句<code>字符串actualprice=driver.findElement(By.xpath(actualprice)).getText(),我假设,您试图从xpath中获得实际价格的值,然后尝试将其与您在Examples部分中传递的expectedPrice进行比较。如果这是正确的,那么您需要重写代码

您的功能文件

Given: Open the app
When: Enter username and password
Then: user is able to login
And verify the Actual Price
And compare to the Expected Price

Examples:
|Actual Price                           | Expected Price|
|"//div[@class='actual_price']//span[2]"|    USD 100.00 | 
|"//div[@class='actual_price']//span[2]"|    USD 200.00 |

您的步骤定义

String actualPrice = null;
@And("^verify the \"([^\"]*)\"$")
public void gettxt(String actualPricePath) throws Throwable {
            actualPrice= driver.findElement(By.xpath(actualpricePath)).getText();
}

@And("^Compare to the \"([^\"]*)\"$")
public void comparePrices(String expectedPrice){
    Assert.assertEquals(expectedPrice, actualPrice, "The actual price is not equal to expected price");
}