0
votes

I have a cucumber framework where the data is being sent as expected but the result is not being read correctly. I suspect because the answer is not appearing on the text box of the calculator to be read:

org.junit.ComparisonFailure: Expected :3 Actual :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)

Feature File Feature: Google Calculator Calculator should calculate correct calculations

Scenario Outline: Add numbers Given I am on google calculator page When I add number "" to number "" Then I should get an answer of ""

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

Scenario Outline: Subtract numbers Given I am on google calculator page When I subtract number "" to number "" Then I should get an answer of ""

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

Steps:

@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());

}}

Page Object @PageObject public class GoogleCalcPage { @FindBy(id = "cwbt33") private WebElement btn1; @FindBy(id = "cwbt34") private WebElement btn2; @FindBy(id = "cwbt35") private WebElement btn3; @FindBy(id="cwtltblr") private WebElement result;

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

}

Wondering if anyone has any ideas?

1

1 Answers

0
votes

If the result isn’t displayed, it’s very hard for your page object to pick it up and the failure is not surprising.

Do you have a timing problem? Are you looking for the value to early? If you run in a graphical browser, say Firefox, do you see a value on the result box?

To me it seems as you need to trouble shoot your page object rather than anything else.