java - 如何使用 Cucumber/Selenium/Java 比较场景大纲示例中的值与实际页面上的实际值

标签 java selenium automation cucumber

给定:打开应用程序

时间:输入用户名和密码

然后:用户可以登录

并验证实际价格

并与预期价格进行比较

示例:

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

最佳答案

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

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

根据您的声明String effectiveprice= driver.findElement(By.xpath(actualprice)).getText();,我假设您正在尝试从xpath获取实际价格的值然后尝试将其与您在示例部分中传递的预期价格进行比较。如果这是正确的,那么您需要重写代码

您的功能文件

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");
}

关于java - 如何使用 Cucumber/Selenium/Java 比较场景大纲示例中的值与实际页面上的实际值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45026075/

相关文章:

javascript - Selenium RC : How to click link appears in grid?

python - 如何在/etc/cron.hourly中执行python脚本?

java - 如何在 Selenium webdriver 中将 "unable to locate element"更改为另一条消息,如 "Element doesn' t exist"?

java.lang.RuntimeException : Unable to resume activity with java. lang.IllegalArgumentException

java - 加快 Android 构建过程

java - 从 JFileChooser 打开按钮关闭上一个窗口

java - 捕获 header 参数信息 selenium java

javascript - 如何在 Node Js 中为最终用户构建简单的 UI 屏幕以实现自动化

vba - vba中是否有任何关键字可以清除网站中的默认值以替换为新值?

java - 在后台运行应用程序并检查新数据