Automated testing with Cucumber (Part 3)

Tram Ho

To deploy Cucumber, you need to create 4 packages to store files:

  • Feature : to contain test cases written in Gherkin language.
  • Page objects: to define the elements on the web / app.
  • Step definition : Leave the scripts corresponding to the test case.
  • Test runner : to run the script.

1. Scenes

Test with the function to log in to the system successfully, after logging in successfully displays the message: “WELCOME ? “.

Link website: http://testing-ground.scraping.pro/login , username / password: admin / 12345.

2. Write a scenario

Define the basic syntax in BDD:

Feature: A short description of the function.

Background: Allows adding some context to all Scenario in the feature. Contains a number of steps to be run before each Scenario Can be understood simply as a prerequisite for implementing all Scenario in the feature Declared later Keyword “Feature”.

Scenario : The keyword begins before each scenario, followed by the title of the scenario that will be executed. Each scenario consists of one or more steps.

Given : Describes the prerequisites for performing a Scenario.

When : Describes the main actions (Steps) that a user takes.

Then : Describe the desired output of Scenario.

And / But : Substitute Given / When / Then keywords to make the program more coherent.

Create a file ending in .feature (in Feature folder) to contain scenarios (scripts).

3. Create the test runner file

Create LoginTestRunner files (in directory runner), here her to run in files Login.feature path.

Inside

  • @RunWith : is the test run class to start the implementation of the test.
  • @CucumberOptions : to set some properties for Cucumber test such as: future file is run, ignores tagged senario, …

4. Make page objects

Create the LoginPage file (in the pageOjects directory) to contain the identifiers of the login page.

5. Create a method to open / close the browser

The following example is to create a controller on Chrome, you can also create on any browser supported by Selenium.

First, you must download ChromeDriver here: https://chromedriver.chromium.org/downloads (the version of chomedriver must be less than or equal to the chrome version installed on the machine).

Next, create the BrowserFactory file

And the Hooks file (created in the Step definition folder) allows us to be able to perform certain tasks at a time of executing test cases as before starting a Scenario or after finishing a Scenario.

And 1 TestBase file

Note : File BrowserFactory and TestBase must be different from the folder with Hooks file.

6. Write scripts for each scenario

Create a LoginSteps file (in the Step definition folder).

And finally, you go to the file testRuner to run the login function, when run successfully will display as shown below.

(continue)

Share the news now

Source : Viblo