io.cucumber.junit.UndefinedStepException: The step ''.... are undefined - java

Been trying to learn/perform some automated testing using Selenium + JUnit + Cucumber and I've spent hours browsing through the internet trying to resolve this with no success. I've written my feature, steps and runner code and the 'glue' is mapped to the correct package too. However, I run into the topic error when I try to run my Runner.java file.
Project Structure
Feature file:
Feature: Admin login
Scenario: Logging into Fleet Portal
Given user navigates to Fleet Portal
When enter credentials
Then user had logged
Runner file:
package Runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.*;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Features"
,glue="Steps"
)
public class TestRunner {}
Step file:
package Steps;
import java.util.ArrayList;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
class App {
private static final WebDriver driver = new ChromeDriver();
public void myMethod(String flag) {
System.out.println(flag);
}
#Given("user navigates to Fleet Portal")
public void user_navigates_to_fleet_portal() {
throw new io.cucumber.java.PendingException();
}
#When("enter credentials")
public void enter_credentials() throws InterruptedException {
throw new io.cucumber.java.PendingException();
}
#Then("user had logged in successfully")
public void user_had_logged_in_successfully() {
myMethod("yes");
throw new io.cucumber.java.PendingException();
}
}
I've tried to take JUnit out of the picture by simply running the feature file as a cucumber test but it still results in the same error, which is weird because the cucumber plugin confirms that the steps are mapped to the features -> feature mapping
POM file
Any and all help is appreciated.

Related

#Given Glue not executed from Stepdef in cucumber but #Before setup() method is executed

In cucumber maven project.
Feature file :
Feature: Smoke Test for Home
Scenario: positive scenario
Given Open "JTMS_PORTAL"
#When application open successfully
#Then validate title as "Welcome: Mercury Tours"
#And Close Application
StepDef File :
package jtms.stepdef;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
public class LoginStepDef {
#Before
public void setup() {
System.out.println("In login stepdef");
}
#Given("^Open \"([^\"]*)\"$")
public void open(String arg1) throws Throwable {
System.out.println("In login stepdef2 with Given annotation");
}
}
TestRunner :
package jtms.testrunner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features = "features/jtms/home.feature",
glue="jtms.stepdef",
dryRun=false
)
public class TestRunner {
}
Cosole output:
In login stepdef
Project structure:
Issue:
#Given from stepdef not print the print statement, but #Before is executed and print the output 'In login stepdef'. please help, thank you in advance.
can you change the value of the feature in cucumber options to below
features = {"classpath:features"}

"No definition found for `User is on NetBanking Login Screen`" warning shown in feature file of Cucumber

Below are my feature, Step definition and test runner files. step definition file has all keywords as per feature file, it shows warning No definition found is displayed in feature file. I am able to run the code, all print statements from step definition file are printed in console but some error also displayed in red color.
Feature file:
Feature: Application Login
Scenario: Home page default login
Given User is on NetBanking Login Screen
When User login into application with Username and Password
Then Home page is displayed
And Cards are displayed
Step definition:
package StepDefinition;
import org.junit.runner.RunWith;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.cucumber.junit.Cucumber;
#RunWith(Cucumber.class)
public class stepDefinition {
#Given("^User is on NetBanking Login Screen$")
public void user_is_on_netbanking_login_screen() throws Throwable {
System.out.println("Login screen appears");
}
#When("^User login into application with Username and Password$")
public void user_login_into_application_with_username_and_password() throws Throwable {
System.out.println("Login with Creds");
}
#Then("^Home page is displayed$")
public void home_page_is_displayed() throws Throwable {
System.out.println("Home page appears");
}
#And("^Cards are displayed$")
public void cards_are_displayed() throws Throwable {
System.out.println("Cards are displayed");
}
}
Test runner:
package cucumberoptions;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/test/java/features",
glue = "StepDefinition"
)
public class testrunner {
}

Cucumber-Why my step definition is not getting identified

I have written a step definition file but still its not getting identified,below is my feature,step definition,Runner file.
Feature: Google Search feature
Scenario: To verify search bar functionality
Given user is on google search page
When user enters a search keyword in a search bar
And user clicks on Enter keyboard button
Then search results are displayed
Then Close the browser
package com.mavenDemo;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefination {
WebDriver driver;
#Given("^user is on google search page$")
public void user_is_on_google_search_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.setProperty("webdriver.chrome.driver", "F://chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.com");
}
#When("^user enters a search keyword in a search bar$")
public void user_enters_a_search_keyword_in_a_search_bar() throws Throwable {
driver.findElement(By.name("q")).sendKeys("Test");
}
#And("^user clicks on Enter keyboard button$")
public void user_clicks_on_Enter_keyboard_button() throws Throwable {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
}
#Then("^search results are displayed$")
public void search_results_are_displayed() throws Throwable {
boolean searchText = driver.findElement(By.xpath("//span[#class='wUrVib']")).getText().contains("Test");
Assert.assertTrue(searchText);
}
#Then("^Close the browser$")
public void close_the_browser() throws Throwable {
driver.quit();
}
}
package com.mavenDemo;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features =
"C:/Users/BR/workspace/com.mavenDemo/src/main/java/Login.features",
glue = {
"/com.mavenDemo/src/main/java/com/mavenDemo/StepDefination" })
public class TestRunner {
}
What Might be the reason to it not to recognize step definition file?
I have double checked the code but I am unable to find the solution also tried solution provided for similar problem but even that didn't work
Please help me in resolving this.
Thanks
I have found an answer!!
mistake was in a test runner file,while specifying a step definition path in glue we need to specify folder name not the test runner path,
Changed glue to
glue = {"com.mavenDemo" }
Thanks.
Please remove /com.mavenDemo from glue path

Cucumber Java program does not pick up step definition

I'm new with cucumber and learned this doc carefully to understand how could I implement my first cucumber java project. I have done a lot of analysis and gone through almost all the articles related to it over internet, why its not picking up step definition but could not find the cause. However, everything seems to be OK as per my understanding, I have great expectation that you guys can find my fault at one go.
Looking forward for a +ve response.
Hence I'm sharing the code, message(on console window) and folder structure.
Thanks
Rafi
Feature file:
#MyApplication
Feature: Post text Hello Rafi on Rafi facebook account
Scenario: Login successfully on Facebook application
Given Open Facebook application
When Enter valid id and password
And Click on Login button
Then Facebook home page should open
TestRunner class:
package test.java.runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
//glue = {"helpers", "src.test.java.steps"},
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"src/features"},
glue = {"helpers", "src.test.java.steps"},
plugin = {"pretty","html:target/cucumber-html-report"},
dryRun = true,
monochrome = true,
tags="#MyApplication",
strict=false)
public class TestRunner {
}
StepDefinition class:
package test.java.steps;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinition {
private static WebDriver driver = null ;
private static String password = "*********";
WebDriverWait wait=new WebDriverWait(driver, 30);
WebElement waitElement;
#BeforeClass
public void setup() throws Throwable
{
System.setProperty("webdriver.gecko.driver", "C:\\Selenium Automation\\selenium\\Selenium 3 and Firefox Geckodriver\\geckodriver.exe");
driver = new FirefoxDriver ();
driver.manage().window().maximize();
}
#AfterClass
public void teardown() throws Throwable
{driver.quit();}
// First scenario
#Given("^Open Facebook application$")
public void open_Facebook_application() throws Throwable{
System.out.println("this is not working");
driver.navigate().to("https://www.facebook.com/");
}
#When("^Enter valid id and password$")
public void enter_valid_id_and_password() throws Throwable{
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys("rafiras16#gmail.com");
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(password);
}
#When("^Click on Login button$")
public void click_on_Login_button() throws Throwable {
driver.findElement(By.xpath("//input[starts-with(#id,'u_0')]")).click();
}
#Then("^Facebook home page should open$")
public void facebook_home_page_should_open() throws Throwable{
String strTitle = driver.getTitle();
System.out.print(strTitle);
}
}
image for message on console window and folder structure
BuildPath details
I think the glue attribute on the CucumberOptions class is wrong. If you want to refer to the step definitions using the file path then you need to change that to:
glue = {"helpers", "src/test/java/steps"},
If you want to refer them by package then you need to remove the "src." prefix:
glue = {"helpers", "test.java.steps"},
Try changing,
glue = {"helpers", "src.test.java.steps"}
to
glue = {"test.java.steps"}
And what is this helpers package i don't see it in the screenshot.
The #AfterClass and #BeforeClass annotations are useful only if the class is a junit test class. You have placed them in a normal class, thus they will not be run. This leads to the driver remaining uninitaialized.
The easy way out is to use the cucumber #Before and #After annotation. Change the #BeforeClass to #Before and #AfterClass to #After.

Jenkins Build is not stopped after successfully passing the Integration test

Hi I am new to Jenkins.
I have configure the Jenkins locally on my machine and Its running fine.
I need to ask whenever my Integration tests (written in Junit) are passed, Jenkin doesn't stops the build and its continue.
But in logs It displays the test cases are passed and no errors are found.
Could some one please suggest any solution how to stop jenkins build?
My Code:
package com.workshop.airport.workshop.airport;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.After;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
public class PageStepsDefs {
public String ChromeDriverPath="C:\\Users\\zain.jamshaid\\Desktop\\chromedriver.exe";
public WebDriver webdriver;
String localhost="http://www.google.com";
public PageStepsDefs (){
System.setProperty("webdriver.chrome.driver",ChromeDriverPath);
webdriver = new ChromeDriver();
}
#Given("^I browse to the (.+) page$")
public void open_page(String url)
{
webdriver.get(localhost+url);
System.out.println(localhost+url);
}
#When("^I click on the button (.+)$")
public void click_On_Menu(String Id)
{
webdriver.findElement(By.id(Id)).click();
System.out.print(Id);
}
#After
public void close_browser(){
webdriver.close();
}
}
I have also attached the screenshot of jenkins console logs
Any help will be awesome.
Thanks!
I was using webDriver.close();
but
webDriver.quit();
fix my problem
This fixed the problem

Categories

Resources