【问题标题】:How to compare value in Examples on Scenario Outline versus the Actual value on the Actual page using Cucumber/Selenium/Java如何使用 Cucumber/Selenium/Java 比较场景大纲示例中的值与实际页面上的实际值
【发布时间】:2017-07-11 05:30:42
【问题描述】:

给定:打开应用程序

何时:输入用户名和密码

然后:用户可以登录

并验证实际价格

并与预期价格进行比较

例子:

|Actual Price                           | Expected Price|

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

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

获取实际价格的步骤是:

@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,然后预期价格列是预期值。 任何帮助将不胜感激...

【问题讨论】:

  • 您应该查看 junit github.com/junit-team/junit4/wiki/assertions 甚至 testng 断言。甚至 hamcrest 匹配器......
  • 首先您必须获得实际价格值并将其与预期值进行比较。然后在两个价格值匹配时应用您的条件。
  • 您好,感谢您的回答,是的,我确实得到了实际价格,请参阅我的步骤定义,但我不确定如何将其与示例数据表上的预期价格进行比较。
  • 基本上我的问题是如何比较实际列与预期价格列

标签: java selenium automation cucumber


【解决方案1】:

您在此处输入的代码似乎不正确。

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

根据您的声明String actualprice= driver.findElement(By.xpath(actualprice)).getText();,我假设您正在尝试从 xpath 获取实际价格的值,然后尝试将其与您在示例部分中传递的 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");
}

【讨论】:

  • 是的,你是对的。我已经更新了代码,现在它可以工作了。谢谢。
  • 解决方案看起来很合适。我听说 BDD 中有一种称为 Scenario Context 的方法可以在步骤之间共享值。有空的时候试试运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多