0
import package org.test.launch;
import static junit.framework.Assert.*;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.After;
import cucumber.annotation.Before;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

public class GoogleSearch {

private WebDriver driver;

@Before
public void setup(){
    driver = new FirefoxDriver();
}

@After
public void teardown(){
    driver.quit();      
}

@Given("^Search for different inputs in google$")
public void OpenGooglePage(){
    driver.get("http://www.google.com.au");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement(By.id("gbqfq")).sendKeys("Planit");
}

@When("^I search for (.*) in google search$")
public void search(String input){
    WebElement searchfield = driver.findElement(By.id("gbqfq"));
    searchfield.sendKeys(input);
    this.driver.findElement(By.id("gbqfba")).submit();
}

@Then("^I should be able to see (.*) in search results$")
public void result(String output){
    WebElement searchresult = driver.findElement(By.cssSelector(".l"));
    assertEquals(searchresult.getText(), output);
}

}

============
功能文件
============
功能:搜索字符串

场景:搜索

Given Search for any string in google
When I search for stackoverflow in google search
Then I should be able to see stackoverflow in search results

=========
胶水代码
=========

package org.test.launch;

import cucumber.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
public class SearchTest {
}

只有@Given 语句正在运行@When 和@Then 语句没有执行。

在控制台输出上,我可以看到以下错误

java.lang.NullPointerException at ?.当我在谷歌搜索中搜索计划时(org\test\launch\GoogleSearch.feature:6)

4

1 回答 1

0

发现问题:

问题在于@Given 声明。

步骤定义中的功能文件 Given 语句和 @Given 语句不相同,因此代码未运行。

于 2013-01-12T01:49:40.443 回答