Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Getting Started

Michał Krzyżanowski edited this page Apr 13, 2018 · 22 revisions

How to write 'hello world' test case?

Do you want to start playing with Bobcat framework? There is no easier way than starting by example.

Prerequisites

Before you start, make sure you have installed:

  • JDK 1.8
  • Maven 3.3.3 or later

Generating Bobcat new project

We provide a handy Bobcat project template, which can generate your desired flavor of Bobcat configuration. Follow the instructions in the template's README: https://github.com/Cognifide/bobcat-gradle-template

Configuring Bobcat

Please refer to Configuring Bobcat.

First test case

Depends on which archetype you selected, you can develop your test cases in pure JUnit or in Cucumber. Below tutorial describes both approaches. You can also switch from one approach to another using adding Cucumber or adding JUnit instructions.

JUnit

Let's write your first test case in Bobcat! The test will open Wikipedia and search for 'hello world'.

Page Object

Productive Developers are Smart and Lazy! That is why we created the @PageObject annotation. Use it to initialize your Page Factory.

For our 'hello world' test case we will create three page object classes:

  • Wikipedia home page
package com.hello.world.wikipedia;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;

import com.cognifide.qa.bb.qualifier.PageObject;
import com.google.inject.Inject;

@PageObject
public class HomePage {

  private static final String URL =  "https://en.wikipedia.org";

  @Inject
  private WebDriver webDriver;

  @FindBy(id = "p-search")
  private SearchComponent searchComponent;

  public SearchComponent getSearchComponent() {
    return searchComponent;
  }

  public HomePage open() {
    webDriver.get(URL);
    return this;
  }
}

look again at this piece of code:

  @FindBy(id = "p-search")
  private SearchComponent searchComponent;

In Bobcat you can put one page object inside another, using @FindBy annotation! Works with Lists also - proven experimentally!

  • search component
package com.hello.world.wikipedia;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.cognifide.qa.bb.qualifier.PageObject;

@PageObject
public class SearchComponent {

  @FindBy(css = "input[type=search]")
  private WebElement searchField;

  public void searchForQuery(String query) {
    searchField.sendKeys(query);
    searchField.submit();
  }
}
  • definition page
package com.hello.world.wikipedia;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.cognifide.qa.bb.qualifier.PageObject;

@PageObject
public class DefinitionPage {

  @FindBy(id = "firstHeading")
  private WebElement heading;

  public String getHeading() {
    return heading.getText();
  }
}

Test case

Let's write first test case

package com.hello.world.wikipedia;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.cognifide.qa.bb.junit.Modules;
import com.cognifide.qa.bb.junit.TestRunner;
import com.google.inject.Inject;
import com.hello.world.GuiceModule;

@RunWith(TestRunner.class)
@Modules(GuiceModule.class)
public class WikipediaTest {

  private static final String SEARCH_QUERY = "hello world";
  private static final String HEADING = "\"Hello, World!\" program";

  @Inject
  private HomePage homePage;

  @Inject
  private DefinitionPage definitionPage;

  @Test
  public void wikipediaSearchTest() {
    homePage.open().getSearchComponent().searchForQuery(SEARCH_QUERY);
    assertThat(definitionPage.getHeading(), is(HEADING));
  }
}

BDD

In BDD example we will use the same page objects classes: HomePage, SearchComponent, DefinitionPage, but another layer of abstraction will be added - test case will be written in Gherkin.

Scenario

Let's create a helloworld.feature file in features directory:

@hello-world
Feature: As a user I want to search for query on wikipedia page

  Scenario: Search for 'hello-world' query
    Given I open wikipedia homepage
    When I search for "hello world" query
    Then definition page with header "\"Hello, World!\" program" is displayed

Steps implementation

Now it's time to generate define each step in the code. For more information about steps generation see Best Practices. Create WikipediaSteps class in com.hello.world.steps package.

package com.hello.world.steps;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import com.google.inject.Inject;
import com.hello.world.pageobjects.wikipedia.DefinitionPage;
import com.hello.world.pageobjects.wikipedia.HomePage;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class WikipediaSteps {

  @Inject
  private HomePage homePage;

  @Inject
  private DefinitionPage definitionPage;

  @Given("^I open wikipedia homepage$")
  public void iOpenWikipediaHomepage() {
    homePage.open();
  }

  @When("^I search for \"([^\"]*)\" query$")
  public void iSearchForQuery(String query) {
    homePage.getSearchComponent().searchForQuery(query);
  }

  @Then("^definition page with header \"(.*)\" is displayed$")
  public void followingPageHeaderIsDisplayed(String headerText) {
    assertThat("Definition page for " + headerText + " is not displayed",
            definitionPage.getHeading(), is(headerText));
  }
}

Executing scenarios

There is only one thing that is missing - test runner:

package com.hello.world;

import org.junit.runner.RunWith;

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

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/main/features/",
    plugin = {"pretty", "html:target/cucumber-html-report/example",
        "json:target/example.json"},
    tags = {"@hello-world"},
    glue = "com.hello.world"
)
public class WikipediaTest {
  // This class is empty on purpose - it's only a runner for cucumber tests.
}

Now you're ready to roll - just execute the following command from the command line!

mvn clean test -Dtest=WikipediaTest -Pintegration

Getting started with Bobcat

  1. Getting started

AEM Related Features

  1. Authoring tutorial - Classic
  1. AEM Classic Authoring Advanced usage
  1. Authoring tutorial - Touch UI
Clone this wiki locally