我正在创建一个Spring Framework来自动化Google Calculator 我有一个功能文件,其中包含一些如下定义的值
Feature: Google Calculator
Calculator should calculate correct calculations
Scenario Outline: Add numbers
Given I am on google calculator page
When I add number "<number1>" to number "<number2>"
Then I should get an answer of "<answer>"
Examples:
| number1 | number2 | answer |
| 1 | 2 | 3 |
| 4 | 5 | 9 |
我正在尝试使用Given,When,Then创建一个测试,可以在计算器中使用此功能文件中的任何数字 我的步骤如下:
@Scope("test")
@ContextConfiguration("classpath:spring-context/test-context.xml")
public class GivenSteps {
@Autowired
private WebDriver webDriver;
@Given("^I am on google calculator page$")
public void iAmOnGoogleCalculatorPage() throws Throwable {
webDriver.get("https://www.google.ie/search?q=calculator");
}
@When("^I add number \"([^\"]*)\" to number \"([^\"]*)\"$")
public void i_add_number_to_number(Integer number1, Integer number2) throws Throwable {
WebElement googleTextBox = webDriver.findElement(By.id("cwtltblr"));
googleTextBox.sendKeys(Keys.ENTER);
throw new PendingException();
}
@Then("^I should get the correct answer again$")
public void thecorrectanswertest2() throws Throwable{
WebElement calculatorTextBox = webDriver.findElement(By.id("cwtltblr"));
String result = calculatorTextBox.getText();
}}
我的问题是如何对可以选择数字并从功能表中验证答案的作品进行编码?
答案 0 :(得分:0)
您是否尝试使用如下所示的@Then来比较表中的答案? -
@Then("^I should get the correct answer \"([^\"]*)\" again$")
public void thecorrectanswertest2(String answer) throws Throwable{
WebElement calculatorTextBox = webDriver.findElement(By.id("cwtltblr"));
String result = calculatorTextBox.getText();
if(answer.equalsIgnoreCase(result))
System.out.println("Test Passed");
}
尝试一次。它应该工作