2013-01-11 55 views
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语句未黄瓜JVM上运行

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

情景:搜索

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 的。当我搜索计划在谷歌搜索(ORG \测试\启动\ GoogleSearch.feature:6)

回答

0

发现问题:

问题出在@Given语句。

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