• +43 660 1453541
  • contact@germaniumhq.com

Getting Started With Germanium I


Getting Started With Germanium I

In this 3 part series we’ll explore how to get started in using germanium. We’ll assume the only thing you have is python and a browser, and we’ll go from writing the first test, to integrating the test suite in a CI/CD system, in our case Jenkins.

This series is broken down into:

  1. Installation and the first test (this article)

  2. Test structure with behave

  3. Integration in CI/CD

Installation

In order to install germanium the simplest way to go about it is just to run pip install:

pip install germanium

The recommended way of installing it though is to create its own virtual environment where you’ll have all your test dependencies.

pip install -U virtualenv
virtualenv /some/path/to/newvenv
cd /some/path/to/newvenv/bin
. activate
pip install germanium

Since we’ll also use behave (an awesome BDD framework) for this, let’s just go and install that as well in this environment:

pip install behave

If everything works, if you should get some similar results when running behave --version and python -m germanium.version:

$ behave --version
behave 1.2.6
$ python -m germanium.version
                                          _
   ____ ____  _________ ___  ____ _____  (_)_  ______ ___
  / __ `/ _ \/ ___/ __ `__ \/ __ `/ __ \/ / / / / __ `__ \
 / /_/ /  __/ /  / / / / / / /_/ / / / / / /_/ / / / / / /
 \__, /\___/_/  /_/ /_/ /_/\__,_/_/ /_/_/\__,_/_/ /_/ /_/
/____/ 2.0.11

The First Test

Our first test will be just the basic test if google comes up, and the title is correct.

We’ll write our first feature file into a new folder for our project:

mkdir germanium-test
cd germanium-test
mkdir features/
${EDITOR} features/GooglePageLoads.feature

In the first feature file we’ll just write our first feature test:

Feature: Just a basic test that google still works.

    Scenario: Test that google comes up.
        Given I open 'http://google.com'
        When I check the page title
        Then the page title contains 'Google'

Of course running this at this stage doesn’t do anything:

$ behave
ConfigError: No steps directory in '/home/raptor/germanium-test/features'

So let’s create some implementation steps:

mkdir feature/steps
${EDITOR} features/steps/basic_steps.py

And it them we’ll just write some basic implementation:

from behave import step, use_step_matcher
from germanium.static import open_browser, go_to, get_germanium

# we tell behave to allow us using regex params
use_step_matcher("re")


@step("I open '(.*?)'")
def i_open_a_page(context, url):
    open_browser("chrome")
    go_to(url)


@step("I check the page title")
def check_page_title(context):
    context.title = get_germanium().title


@step("the page title contains '(.+?)'")
def check_contains(context, contained_text):
    assert contained_text in context.title

If you run now behave, the test should pass successfully:

behave
Feature: Just a basic test that google still works. # features/GooglePageLoads.feature:1

  Scenario: Test that google comes up.    # features/GooglePageLoads.feature:3
    Given I open 'http://google.com/'     # features/steps/basic_steps.py:8 3.689s
    When I check the page title           # features/steps/basic_steps.py:14 0.008s
    Then the page title contains 'Google' # features/steps/basic_steps.py:19 0.000s

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m3.697s

But if you notice the browser remains open. This is because we are not closing the browser. Let’s make sure the browser is closed after each scenario. For this we’ll write a new file features/environment.py with the following content:

from germanium.static import close_browser


def after_scenario(context, scenario):
    close_browser()

This will instruct behave to call close browser after each scenario execution.

Conclusions

In this article we learned how to:

  1. Install germanium

  2. Write a simple test using behave

The full source of this project is available at: https://github.com/germaniumhq/germanium-test.