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

Getting Started

kkarolk edited this page Aug 11, 2016 · 22 revisions

How to write 'hello world' test case?

Do you want to start playing with Bobcat framework? There is no easiest way as start by example. Let’s start and generate project from archetype.

Developer setup guide

Before you start, make sure you have installed:

  • JDK 1.8
  • Maven 3.3.3 or later

Archetype

Selecting version of Archetype

Bobcat comes with four archetype versions:

| Archetypes | Description| |---|---|---| | Bobcat JUnit | Bobcat Archetype with JUnit support | | Bobcat BDD | Bobcat Archetype with Cucumber support | | Bobcat AEM JUnit | Bobcat JUnit archetype enriched with AEM support | | Bobcat AEM BDD | Bobcat BDD archetype enriched with AEM support |

You can select an archetype that suits your project and process best.

Generating new project from archetype

In order to generate new project from archetype please type following command in the command line:

mvn archetype:generate \
        -DarchetypeGroupId=com.cognifide.qa.bb \
        -Dfilter=bobcat

Select an archetype you want to generate:

Choose archetype:
1: remote -> com.cognifide.qa.bb:bobcat-archetype (Basic Bobcat archetype.)
2: remote -> com.cognifide.qa.bb:bobcat-archetype-aem (Basic AEM Bobcat archetype)
3: remote -> com.cognifide.qa.bb:bobcat-archetype-aem-bdd (AEM BDD Bobcat archetype)
4: remote -> com.cognifide.qa.bb:bobcat-archetype-bdd (BDD Bobcat archetype)

Select archetype version and define following properties:

Define value for property 'groupId': : com.cognifde.qa.bb
Define value for property 'artifactId': : hello-world
Define value for property 'version':  1.0-SNAPSHOT: : 1.0.0
Define value for property 'package':  com.cognifde.qa.bb: : com.hello.world

When the project is successfully created your console should display something like:

[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: bobcat-archetype:1.0.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.cognifde.qa.bb
[INFO] Parameter: artifactId, Value: hello-world
[INFO] Parameter: version, Value: 1.0.0
[INFO] Parameter: package, Value: com.hello.world
[INFO] Parameter: packageInPathFormat, Value: com/hello/world
[INFO] Parameter: package, Value: com.hello.world
[INFO] Parameter: version, Value: 1.0.0
[INFO] Parameter: groupId, Value: com.cognifde.qa.bb
[INFO] Parameter: artifactId, Value: hello-world
[INFO] project created from Archetype in dir: /bobcat-arch-testing/hello-world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16:56 h
[INFO] Finished at: 2016-07-06T08:50:28+01:00
[INFO] Final Memory: 12M/81M
[INFO] ------------------------------------------------------------------------

Project structure

Project structures for each archetype can be found in archetype README file.

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("^following page 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 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