问题描述
Feature: Login to Website
#Login in Chrome
Scenario: Login using Chrome
Given I open Chrome
When I browse to Website
Then I login to Website using "user1" and "password1"
全局基类
public class base {
public WebDriver driver;
public Properties prop;
public ChromeOptions coptions;
public WebDriver initializeDriver() throws IOException
{
String browserName= "chrome";
System.out.println(browserName);
String pathToDriver = "";
if(browserName.equals("chrome"))
{
pathToDriver = "C:\\Repositories\\automationProject\\webDrivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",pathToDriver);
coptions.addArguments("disable-infobars");
coptions.addArguments("--start-maximized");
driver= new ChromeDriver(coptions);
//execute in chrome driver
}
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
return driver;
}
登录
public class login extends base {
@Given("^I open Chrome$")
public void iOpenChrome() throws Throwable {
driver = initializeDriver();
}
@When("^I browse to Website$")
public void iBrowseToWebsite() {
driver.get("https://www.website.com/");
}
@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"£\"$")
public void iLoginToWebsiteUsingAnd£(String username,String password) throws Throwable {
driver.findElement(By.id("UserName")).sendKeys(username);
driver.findElement(By.id("Password")).sendKeys(password);
driver.findElement(By.id("btnLogin")).click();
}
}
问题是,运行此功能时,出现以下错误,我无法理解为什么会发生此错误,因为它对错误的位置没有帮助。
未定义步骤:打开Chrome浏览器
未定义的步骤:当我浏览到网站时
未定义的步骤:然后我使用“ user1”和“ password1”登录网站
解决方法
您的步骤功能名称错误。您正在遵循camelCase方法,这种方法在黄瓜情况下无效。您已将@Given("^I open Chrome$")
步骤功能名称写为i_open_chrome()
,而不是iOpenChrome()
。您可以使用Tidy Gherkin
chrome扩展名生成步骤定义。
有效的示例步骤。
@Given("^I open Chrome$")
public void i_open_chrome() throws Throwable {
throw new PendingException();
}
@When("^I browse to Website$")
public void i_browse_to_website() throws Throwable {
throw new PendingException();
}
@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_login_to_website_using_something_and_something(String strArg1,String strArg2) throws Throwable {
throw new PendingException();
}
,
您应该通过右键单击将黄瓜特征文件转换为Stepdefenition文件,不要键入代码行
,可能您的“胶水”与测试运行器或配置未正确对齐。
尝试使用步骤定义的路径编辑“运行”>“编辑配置”中的“胶水”选项。
或者,如果您正在使用测试运行程序类,请确保获得以下内容:
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "path\\to\\features",glue = "stepdefs"
)
public class RunTest {
}
其中stepdefs
是带有步骤定义文件的程序包名称。