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

Getting Started

Mikołaj Mański edited this page Jul 7, 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.

Archetype

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

//TODO
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=5-SNAPSHOT

select an archetype you want to generate:

//TODO
1:  -> com.cognifide.qa.bb:bobcat-archetype-bdd (-)
2: -> com.cognifide.qa.bb:bobcat-archetype (-)

select newest available version, e.g. 1.0.0 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

JUnit Archetype

└───hello-world
 |   └───src
 |       ├───main
 |       │   ├───config
 |       │   │   ├───common
 |       │   │   ├───dev
 |       │   │   └───integration
 |       │   └───java
 |       │       └───com
 |       │           └───hello
 |       │               └───world
 |       │                   └───pageobjects
 |       │                       ├───feedback
 |       │                       ├───login
 |       │                       └───summer
 |       └───test
 |           ├───java
 |           │   └───com
 |           │       └───hello
 |           │           └───world
 |           │               ├───feedback
 |           │               ├───login
 |           │               └───summer
 |           └───resources
 └───pom.xml

BDD Archetype

└───hello-world
 |    └───src
 |        ├───main
 |        │   ├───config
 |        │   │   ├───common
 |        │   │   ├───dev
 |        │   │   └───integration
 |        │   ├───features
 |        │   │   ├───components
 |        │   │   └───login
 |        │   ├───java
 |        │   │   └───com
 |        │   │       └───hello
 |        │   │           └───world
 |        │   │               ├───hooks
 |        │   │               ├───pageobjects
 |        │   │               │   ├───feedback
 |        │   │               │   └───login
 |        │   │               └───steps
 |        │   └───resources
 |        └───test
 |            └───java
 |                └───com
 |                    └───hello
 |                        └───world
 └───pom.xml 

Similarities

Configuration

Configurations for different environments are stored in the config directory:

 |    └───src
 |        ├───main
 |        │   ├───config
 |        │   │   ├───common
 |        │   │   ├───dev
 |        │   │   └───integration

Page object classes

Page object classes for has their place in com.hello.world.pageobjects package:

 |       │       └───com
 |       │           └───hello
 |       │               └───world
 |       │                   └───pageobjects

Differences

Test cases
JUnit archetype

Test cases written in Java are in com.hello.world package in test directory:

 |       └───test
 |           ├───java
 |           │   └───com
 |           │       └───hello
 |           │           └───world
 |           │               ├───feedback
 |           │               ├───login
 |           │               └───summer
BDD Archetype

Test cases written in Gherkin are in features in directory:

└───hello-world
 |    └───src
 |        ├───main
 |        │   ├───features

BDD project contains also runners for Gherkin scenarios:

 |        └───test
 |            └───java
 |                └───com
 |                    └───hello
 |                        └───world

Package com.hello.world.steps for steps implementation:

└───hello-world
 |    └───src
 |        │   ├───java
 |        │   │   └───com
 |        │   │       └───hello
 |        │   │           └───world
 |        │   │               └───steps

There is also special place for hooks (com.hello.world.hooks) - actions to be executed before and after tests execution (e.g. browser window maximize):

└───hello-world
 |    └───src
 |        │   ├───java
 |        │   │   └───com
 |        │   │       └───hello
 |        │   │           └───world
 |        │   │               ├───hooks

Running sample AEM test cases

Instruction is the same regardless project archetype:

  1. Edit /src/main/config/integration/instances.properties and provide your AEM instance details,
  2. Edit /src/main/config/common/webdriver.properties and provide your browser details,
  3. Execute following command from the command line:
mvn clean test -Pintegration

First test case

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

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