简体   繁体   中英

Open Browser & Navigate - Java Automated Testing Selenium

 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"

Global Base Class

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

Login

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

}

The issue is that when running this feature I am getting the below error which I cannot understand why this is happening as it gives me no help in where the error is.

Undefined step: Given I open Chrome

Undefined step: When I browse to Website

Undefined step: Then I login to Website using "user1" and "password1"

Your function name for the steps is wrong. You are following camelCase approach that is not valid in the case of cucumber. You have write your @Given("^I open Chrome$") step function name as i_open_chrome() instead of iOpenChrome() . You can use Tidy Gherkin chrome extension to generate step definitions.

Valid sample steps.

@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 文件,不要输入代码行

Probably your "glue" is not aligned correctly with your test runner or configuration.

Try editing the "glue" option in "Run > Edit Configuration" with the path to your step definitions.

Or, if you are using a test runner class, make sure you get this:

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 {

}

Where stepdefs is a package name with your step definition file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM