Reputation: 317
I am trying to run cucumber test written in java.
Everything seems to run fine except after I implement the feature file I start getting this error : cucumber.runtime.CucumberException: Failed to instantiate stepDefinitions class.
package runners;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/FeatureFile",
glue = {"stepDefinitions"}
)
public class TestRunner {
}
Project Structure is as follows:
StepDefinitons class is as follows. First I run the feature file, after which I got methods which I need to implement in the stepDefinitons class.
package stepDefinitions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
public class Steps {
private static WebDriver driver;
WebDriverWait wait=new WebDriverWait(driver, 20);
@Given("^I naviagte to login page$")
public void i_naviagte_to_login_page() {
System.setProperty("webdriver.chrome.driver", "E:\\TestingJAR\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.facebook.com");
}
@When("^I enter username as operqa(\\d+)$")
public void i_enter_username_as_operqa(String usr) {
WebElement username = driver.findElement(By.name("username"));
username.sendKeys(usr);
username.sendKeys(Keys.TAB);
}
@When("^I enter password as (\\d+)$")
public void i_enter_password_as(String pass) {
WebElement tipoValidacion;
tipoValidacion = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"tipoValidacionDiv\"]/div/div/div/span[3]")));
tipoValidacion.click();
driver.findElement(By.xpath("//*[@id=\"tipoValidacionDiv\"]/div/div/div/span[1]")).click();
WebElement password;
password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("password")));
password.sendKeys(pass);
}
@When("^I press Ingresar button$")
public void i_press_Ingresar_button() {
driver.findElement(By.xpath("//BUTTON[@type='submit']")).click();
}
@Then("^I should be able to login successfully$")
public void i_should_be_able_to_login_successfully() {
WebElement loginSuccess = driver.findElement(By.xpath("//H1[@class=''][text()='Home']"));
Assert.assertEquals(loginSuccess.getText(),"Home");
}
}
Upvotes: 0
Views: 9858
Reputation: 22973
The instantiation fails as it's not possible to create a new object of Steps
with the default constructor.
public class Steps {
private static WebDriver driver;
WebDriverWait wait=new WebDriverWait(driver, 20);
...
because driver
is not yet initialized at new WebDriverWait(driver, 20)
.
To initialize driver
you could use a @Before
(executed before scenario) or @BeforeStep
(executed before step) annotated method.
Have a look at the related documentation
https://docs.cucumber.io/cucumber/api/#scenario-hooks
https://docs.cucumber.io/cucumber/api/#step-hooks
Upvotes: 3