Getting started with: JGiven

This is the eighth article in our series on new, popular or otherwise interesting tools used in test automation. You can read all posts within this series by clicking here.

What is JGiven?
From the website: JGiven is a developer-friendly and pragmatic BDD tool for Java. Developers write scenarios in plain Java using a fluent, domain-specific API, JGiven generates reports that are readable by domain experts.

In short, JGiven can be used as an alternative to other BDD frameworks, such as JBehave and Cucumber. Where the latter two separate the features from the test code, JGiven does not and might therefore be considered more suitable to unit and integration tests rather than automated system and regression tests.

Where can I get JGiven?
The JGiven sources can be downloaded from the JGiven GitHub site.

How do I install and configure JGiven?
The easiest way to get started with JGiven is by creating a new Maven project and including the following dependency:


This is for use in combination with TestNG, which I will do in the rest of this post. If you prefer to user JUnit, use jgiven-junit as your artifactId. Please note that this dependency does not include TestNG (or JUnit) itself, so be sure to include it separately in your pom.xml.

Creating and running a first JGiven test
Taking my trusted ParaBank application as an example, I want to perform a test determining whether I can login successfully using a JGiven scenario. I.e., I want to execute the following scenario:

Given I am at the login page
When I login as john with password demo
Then the login action will be successful

The implementation of this scenario as a TestNG-based test in JGiven is pretty straightforward:

public class LoginTest extends ScenarioTest<GivenIAmAtTheLoginPage, WhenILoginAsJohnWithPasswordDemo, ThenTheLoginActionWillBeSuccessful> {
	public void aFirstLoginTest() {

The ScenarioTest class requires three parameters, each representing a stage in the Given-When-Then scenario. To make this compile, we also need to implement each of the three classes that are used as a parameter. As an example, this is what the implementation of the GivenIAmAtTheLoginPage class looks like:

public class GivenIAmAtTheLoginPage extends Stage<GivenIAmAtTheLoginPage>{
	public GivenIAmAtTheLoginPage I_am_at_the_login_page() {
		return self();

The other two classes (stages) are implemented in a similar way. Now that this is done, we can run our test, which lads to the following output in the console:

Test Class: com.ontestautomation.jgiven.tests.LoginTest

 Scenario: A first login test

   Given I am at the login page
    When I login as john with password demo
    Then the login action will be successful

PASSED: aFirstLoginTest

We can see that our test passes, so we have something to build upon. No actual test actions are performed yet, so we are going to add these next. Since we are performing a Selenium WebDriver test, it’s required that we pass along the browser instance as a parameter for each of the steps. This also means we can initialize it before the test is run (using @BeforeTest) and destroy it afterwards (using @AfterTest):

public class LoginTest extends ScenarioTest<GivenIAmAtTheLoginPage, WhenILoginAsJohnWithPasswordDemo, ThenTheLoginActionWillBeSuccessful> {
	WebDriver driver;
	public void initBrowser() {

		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	public void aFirstLoginTest() {
	public void tearDown() {

The WebDriver actions associated with the Given, When and Then steps can now be added to the respective classes as well, for example for GivenIAmAtTheLoginPage this results in:

public class GivenIAmAtTheLoginPage extends Stage<GivenIAmAtTheLoginPage>{
	WebDriver _driver;
	public GivenIAmAtTheLoginPage I_am_at_the_login_page(WebDriver driver) {
		_driver = driver;
		return self();

If we implement the other two Stage classes in a similar vein – where the Then stage should include an actual TestNG assertion to make it a proper test case – and rerun our test, we can see that it passes again:

Test Class: com.ontestautomation.jgiven.tests.LoginTest

 Scenario: A first login test

   Given I am at the login page FirefoxDriver: firefox on WINDOWS (6bccf261-5ce0-4378-8118-b545b6c82eca)
    When I login as john with password demo FirefoxDriver: firefox on WINDOWS (6bccf261-5ce0-4378-8118-b545b6c82eca)
    Then the login action will be successful FirefoxDriver: firefox on WINDOWS (6bccf261-5ce0-4378-8118-b545b6c82eca)

PASSED: aFirstLoginTest

Note that JGiven automatically adds a bit of information on the browser instance used to the console output.

Useful features
Some useful additional features of JGiven are:

  • Several types of test execution and results reporting, including JSON and HTML
  • Support for parameterized steps (in the above example, you could for instance parameterize the username and password in the When step)
  • Support for tags to organize scenarios

Further reading
Apart from the features mentioned above, JGiven provides some more useful features for creating useful BDD-style Java-based tests. A complete reference guide can be found on the JGiven website.

An Eclipse Maven project including the tests and test classes I’ve used in this post can be downloaded here.

34 thoughts on “Getting started with: JGiven

  1. Hi Bas,

    I think this is the first blog post on JGiven, so thank you very much 🙂
    In your example, you could make use of the injection feature of JGiven to avoid having to pass the webDriver to each step method. Just annotate the driver field in the test class with @ScenarioState and add a corresponding field to the stage class and the driver will be directly injected into the stage. As an alternative you could also make use of the @Hidden annotation on the step parameters to avoid that the webdriver instance appears in the report.

  2. Pingback: Testing Bits – 11/15/15 – 11/21/15 | Testing Curator Blog

  3. Hi Bas,
    I did not download the jgiven sources from the github site link you gave.But your eclipse maven project i downloaded worked fine.How?

    • That’s the beauty of Maven for you, it automatically resolves the dependencies that are referenced in the pom.xml file (which is in the root of the project). Very handy.

  4. Hi Bas,
    I am now really careful on selecting topics which is useful for me in your blog.To say it in simple words,
    i am careful not to take things inside my head which
    i cannot understand.But this looks to me as a very simple and helpful topic.But the problem i need to first learn what is Maven and what is BDD,what is jbehave,what is cuccumber chew this correct?
    one more thing what i have in mind is to study things only related to selenium webdriver.i don’t know if that is a good idea to get a testing job on selenium webdriver in a good company.Anyway is this jgiven somehow related to selenium webdriver or this BDD is something else which has no connection to selenium webdriver at all?

  5. I understood why you didn’t reply.i have asked something similar before and you have said i need to understand them all.ok leave that part to me.
    Maybe for few more months i will stay in this company. until then i want to bring new ideas,innovations etc.
    in automation since they offer all support.That is the fun in testing not salary right?
    so if some idea comes to my mind or you mind just let’s share it ok through this blog or mail ok?i hope you won’t stop this blog soon.Right?

    • I just missed that comment, sorry, I never meant not to reply to it.. Anyway, JGiven is not related to WebDriver, you can combine them in your tests but they are separate libraries with separate purposes.

      And I don’t plan on quitting this blog any time soon, I am just a little short on time at the moment.. Between work, preparing a workshop on API testing and teaching software testing to university students as a side gig there’s not much left for blogging, unfortunately. It will get better.

      • Oh,that is fine sir 🙂 .
        Talking about innovation here is an interesting post my boss shared on facebook.

  6. Hi Bas,
    Testing a website which does so many things comes under API Testing,Software Testing or Simply Functional Testing?what is the difference between Api testing and software testing?
    ExtentReport actually cannot be said as a reporting tool,rather it is a jar file or library which we can include in eclipse for writing output using its log class right?But can everyone develop such jar files of their own?i mean can only a person like Anshoo Arora,i guess he must have lot of experience and incredible knowledge can only develop such things?

    • API testing is a form of software testing, as an API is some sort of software.

      Oh, and it takes a bit of skill to write such a library, yes, I don’t think I could do it.

  7. one more thing.can you do me a favour.when you get time can you please send that HAR file you generated which contains all the httpresponse sent from a webpage when it is loaded?my friend said he will give it a try for converting that har file.send it to

      • it is ok bas.i will look for another way.suppose next weak i have to test an order processing page,and if it can be automated can you share some ideas or tips on how?only when you get time ok?

          • Hi Bas,
            you have any idea how to fix the following error-
            unknown error: unhandled inspector error: {“code”:-32603,”message”:”Cannot navigate to invalid URL”}

          • Seems like you specified an incorrect URL in your navigation action. What does your driver.navigate() statement look like?

          • Bas,
            I have an array that removes duplicate urls and store the rest.
            so inside the loop while reading the above array i have given,
            where s is the url.
            i used System.out.println(s); to check and this was the result-

            I can’t find anything wrong with these urls.If the wesite i tested contain Ajax,will the above error gets displayed?

          • No, having Ajax won’t be the reason, it’s more likely that your array contains an invalid value. You might want to try something like this to validate whether your entry is a valid URL before navigating to it. Just skip the ones that aren’t real URLs.

          • Hi Bas,
            You are 100% entry in the array was entering as was tricky to find out from the console 🙂 .
            Finally i think we have a program that is useful.
            Before uploading to my blog i want to ask you something.can you say how actually the below piece of code works?
            public void ExtractJSLogs() {
            LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
            for (LogEntry entry : logEntries) {
            System.out.println(new Date(entry.getTimestamp()) + ” ” + entry.getLevel() + ” ” + entry.getMessage());
            The thing i understood is-this program finds errors using a feature that is in the browser we have to use is googlechrome for testing.The best part is it finds jserrors also.

  8. Bas,
    If we consider your parabank demo website there are so many links like About Us, Services ,Products, Locations,Admin Page in simple words, testing (manually or automated) each link is called unit testing and combined-integration testing and if we do it continuously continuous integration testing?

Leave a Reply

Your email address will not be published. Required fields are marked *