【问题标题】:Cucumber fails on @Then step on Spring Framework, cannot read resultCucumber 在 @Then 上失败了 Spring Framework,无法读取结果
【发布时间】:2017-10-27 13:43:48
【问题描述】:

我有一个黄瓜框架,其中数据按预期发送,但结果未正确读取。我怀疑是因为答案没有出现在要阅读的计算器的文本框中:

org.junit.ComparisonFailure: 预期:3 实际:0

at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at io.autotest.steps.GivenSteps.i_should_get_an_answer_of(GivenSteps.java:48)
at ✽.Then I should get an answer of "3"(cucumber-feature/google-calc.feature:7)

功能文件 功能:谷歌计算器 计算器应该计算正确的计算

场景大纲:添加数字 鉴于我在谷歌计算器页面 当我将数字“”添加到数字“”时 那么我应该得到“”的答案

Examples: 
    | number1 | number2 | answer |
    | 1       | 2       | 3      |
    | 4       | 5       | 9      |

场景大纲:减去数字 鉴于我在谷歌计算器页面 当我将数字“”减去数字“” 那么我应该得到“”的答案

Examples: 
    | number1 | number2 | answer |
    | 10      | 5       | 5      |
    | 15      | 10      | 5     |

步骤:

@Autowired
private GoogleCalcPage googleCalcPage;

@Given("^I am on google calculator page$")
public void iAmOnGoogleCalculatorPage() throws Throwable {
    webDriver.get("https://www.google.ie/search?q=calculator");
    Thread.sleep(3000);
}

@When("^I add number \"([^\"]*)\" to number \"([^\"]*)\"$")
public void i_add_number_to_number(String arg1, String arg2) {
    googleCalcPage.clickOnGivenButton(webDriver, arg1);
    googleCalcPage.clickOnGivenButton(webDriver, "+");
    googleCalcPage.clickOnGivenButton(webDriver, arg2);
    googleCalcPage.clickOnGivenButton(webDriver, "=");
}

@When("^I subtract number \"([^\"]*)\" to number \"([^\"]*)\"$")
public void i_subtract_number_to_number(String arg1, String arg2) {
    googleCalcPage.clickOnGivenButton(webDriver, arg1);
    googleCalcPage.clickOnGivenButton(webDriver, "−");
    googleCalcPage.clickOnGivenButton(webDriver, arg2);
    googleCalcPage.clickOnGivenButton(webDriver, "=");
}

@Then("^I should get an answer of \"([^\"]*)\"$")
public void i_should_get_an_answer_of(String arg1) {

    Assert.assertEquals(arg1, googleCalcPage.getResult());

}}

页面对象 @PageObject 公共类 GoogleCalcPage { @FindBy(id = "cwbt33") 私有 WebElement btn1; @FindBy(id = "cwbt34") 私有WebElement btn2; @FindBy(id = "cwbt35") 私有 WebElement btn3; @FindBy(id="cwtltblr") 私有 WebElement 结果;

public GoogleCalcPage() {
}

//findElement(By.id("cwtltblr"));

public void clickOnGivenButton(WebDriver driver, String value) {

    for (int i = 0; i < value.length(); i++) {
        driver.findElement(By.id(String.format("cwtltblr", value.charAt(i)))).click();
        //driver.findElement(By.xpath(String.format("//span[text()='%s']", value.charAt(i)))).click();
        synchTime(1000);    
    }
}

private void synchTime(long time) {
    try {
        Thread.sleep(time);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public String getResult() {

    String resultText = result.getText().trim();

    return resultText;
}

}

想知道是否有人有任何想法?

【问题讨论】:

    标签: java spring automation cucumber


    【解决方案1】:

    如果结果没有显示出来,那么你的页面对象就很难捡起来,失败也就不足为奇了。

    你有时间问题吗?您是否正在寻找早期的价值?如果您在图形浏览器(例如 Firefox)中运行,您会在结果框中看到值吗?

    在我看来,您似乎需要对页面对象进行故障排除而不是其他任何事情。

    【讨论】:

    • 是的,我认为这是时间安排,但是一旦选择了对象,页面就会按预期等待。问题是该值没有以视觉方式输入,因此无法读取
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2014-01-09
    相关资源
    最近更新 更多