Cucumber-Why my step definition is not getting identified - java

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

Related

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

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.

"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 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.

How to close pop up in selenium webdriver?

I am trying to close pop up using selenium web driver with java. I have tried different ways, but unable to succeed. Please help me.
package Demo;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class YahooTest {
public static void main(String[] args) throws InterruptedException {
FirefoxDriver obj = new FirefoxDriver();
String url = "https://www.planyourjourney.com/";
obj.get(url);
obj.manage().window().maximize();
obj.findElement(By.xpath("html/body/div[12]/div/div/div[9]")).click();
obj.findElementByClassName("dyna-link-new-registration").click();
obj.findElement(By.linkText("B2B Login")).click();
Thread.sleep(4000);
obj.findElement(By.id("userid")).clear();
obj.findElement(By.id("userid")).sendKeys("1592jet#gmail.com");
obj.findElement(By.id("ulPassword")).clear();
obj.findElement(By.id("ulPassword")).sendKeys("spyj01");
obj.findElement(By.name("Next")).click();
Thread.sleep(2000);
obj.findElement(By.id("affilitetrainadvpage"));
Alert alert = obj.switchTo().alert();
alert.dismiss();
}
}
I have seen the source code of the web page and could not find any alert in it.
The pop-up is actually written inside a div tag.
Hence, remove the codes related to alert and use below code which uses xpath.
obj.findElement(By.xpath("//*[#id='affilitetrainadvpage']/span/a/img")).click();

Making a java/cucumber Test portable from eclipse

I am a bit new to eclipse and the cucumber/selenium combo. I have three files: a feature, a TestRunner, and the stepDefintion file. I would like to be able to run these files outside of the eclipse IDE. I need to be able to run these tests on a different computer that does not have Eclipse or access to external websites, only the one that will be tested. I've included code for the three sample files (this does not include the proper URL or credentials). I would like to make this somehow executable, however I know that lacking a main method, I can't run this as a Jar.
Feature:
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters "maximo" and "source1"
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
TestRunner
package cucumberTest;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
, monochrome = true
, plugin = {"pretty"}
)
public class firstTestCase {
}
And the actual Step Definitions
package stepDefinition;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Test_Steps {
public static WebDriver driver;
private String baseUrl;
static WebDriverWait wait;
#Given("^User is on Home Page$")
public void user_is_on_Home_Page() throws Throwable {
System.setProperty("webdriver.chrome.driver",
"C:\\Selenium\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
baseUrl = "https://maximo-demo76.mro.com/";
wait = new WebDriverWait(driver, 60);
driver.get(baseUrl + "/maximo/webclient/login/login.jsp?welcome=true");
}
// #When("^User Navigate to LogIn Page$")
// public void user_Navigate_to_LogIn_Page() throws Throwable {
// driver.findElement(By.xpath(".//*[#id='account']/a")).click();
// }S
#When("^User enters \"(.*)\" and \"(.*)\"$")
public void user_enters_UserName_and_Password(String realUsername, String realPassword) throws Throwable {
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(realUsername);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(realPassword);
driver.findElement(By.id("loginbutton")).click();
}
#Then("^Message displayed Login Successfully$")
public void message_displayed_Login_Successfully() throws Throwable {
System.out.println("Login Successfully");
}
#When("^User LogOut from the Application$")
public void user_LogOut_from_the_Application() throws Throwable {
WebElement logOut =
driver.findElement(By.id("titlebar_hyperlink_8-lbsignout_image"));
Actions actions = new Actions(driver);
actions.moveToElement(logOut).click().perform();
}
#Then("^Message displayed LogOut Successfully$")
public void message_displayed_LogOut_Successfully() throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println("LogOut Successfully");
}
}

Categories

Resources