Junit Runner class doesn't execute @Before for each scenario

31 views
Skip to first unread message

VD

unread,
May 11, 2017, 4:56:56 AM5/11/17
to Cukes
Feature File:

@tag
Feature: Test HDFC

@sceneOne1
Scenario: Test HDFC
Given I open HDFC Bank home page and click on 'Login' button

***********************************************************************************************
Step Definition:

package stepDefinitions;


import pages.MyFirstMethod;

import cucumber.api.java.en.Given;

public class MyFirstStepDefinition {
MyFirstMethod m=new MyFirstMethod();
@Given("^I open HDFC Bank home page and click on 'Login' button$")
public void iOpenHDFCBankHomePageAndClickOnLoginButton() throws Throwable {
m.clickOnElement();
}


}
******************************************************************************************
Before After:

package runnerPackage;

import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import pages.ConfigReadFile;
import pages.PageInstances;




public class BeforeAfter extends PageInstances{

@Before
public  void runBefore()
{
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32_2.26\\chromedriver.exe");
System.out.println("reached here");
driver=new ChromeDriver();
System.out.println(ConfigReadFile.URL);
driver.get(ConfigReadFile.URL);

}
@After
public  void runAfter()
{
System.out.println("do nothing");
}

}

**************************************************************************************************************
Test Runner:

package runnerPackage;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features="classpath:MyFirstApp.feature",glue={"stepDefinitions"},tags={"@sceneOne1"})
public class TestRunner {

}
********************************************************************************************************************
pom.xml:

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.learnautomation</groupId>
  <artifactId>com.learnautomation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.0</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.4</version>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.4</version>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>1.2.4</version>
</dependency>


  
  </dependencies>
</project>
**************************************************************************************************************************
configuration file:

<ConfigurationFile.xml>
</ConfigurationFile.xml>

**********************************************************************************************************************************
config read file:

package pages;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;


public class ConfigReadFile {
public static final String URL;
static {
File f=new File("C:\\Users\\dv00354652\\work\\New folder\\com.learnautomation\\ConfigurationFile.xml");
DocumentBuilderFactory docbuildFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = docbuildFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(f);
} catch(Exception es)
{
}
URL=doc.getElementsByTagName("url").item(0).getTextContent();
System.out.println(URL);
}

}
*******************************************************************
the test runner is able to find feature file and associated hooks class as well but it's unable to execute @before,it's not opening an instance of chrome.

Any help would be appreciated

Björn Rasmusson

unread,
May 11, 2017, 3:05:03 PM5/11/17
to Cukes

I have already answered the same question from you once before: https://groups.google.com/forum/#!topic/cukes/boNdwiPZD9M
The answer is still the same, with glue={"stepDefinitions"} you explicitly specify that hooks and step definitions only should be loaded from the package stepDefinitions. You need either move your hook class to that package (package stepDefinitions), or you need to change the glue option to
glue={"stepDefinitions" ,"runnerPackage"}

/Björn

VD

unread,
May 12, 2017, 12:26:49 AM5/12/17
to Cukes
thank you it's working now.... :)

Tim Walker

unread,
May 12, 2017, 9:32:20 AM5/12/17
to cu...@googlegroups.com
Bjorn, Is it always package name or can it also include path to the glue code because that also seems to work. I will have to look but this "recidivist" solution support makes me wonder if we could improve the online documentation? So that's my new question: Is there a place where Cucumber behavior is fully articulated in Gherkin?  Thanks all for an awesome community. I still earnestly believe this is the holy grail for iterative development. 

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages