Automation QA Interview Questions

Making the jump from a Manual and into a Technical QA role can be difficult and challenging. Here are some potential automation QA interview questions which you may be asked during an interview.

Automation QA Interview Questions

What are the core positive features of an Automation Framework?

Building an automation framework can become a dangerious journey if key features are not considered before putting pen to paper. Before buliding a framework, some of the most important factors to consider include:

  • Speed
  • Maintenance
  • Modular
  • Debug / Error
  • Configurable

Speed
It is important that you are able to run all your tests quickly. Some solutions include running tests in parallel or running tests against different browsers.

Maintenance
Being able to maintain a framework can mean the difference between extending it or removing it. If it becomes difficult to maintain a framework, it then has the potential to become an overhead instead of adding value.

Modular
Switching out technologies and replacing them in a framework provides the flexibility to update frameworks more easily. For instance, being able to switch out testing libraries makes a framework more stable. Other examples include being able to change reporting of results.

Debug / Error
A automation framework is next to useless if you are not able to debug the code or tell what the errors are if a test fails. It help debug, writing clean and understandable code helps. Writing meaningful assertions also helps to identify sources for failed tests.

Configurable
A given test becomes more versatile if it is possible to run a given test against different environments. However, the control of where the test run’s should not be governed in the test itself. Being able to change the test run location, the host to talk to etc via configurations helps to configure tests. This also can helps to scale an automation framework.

Can you name any disadvantages to E2E Automation Testing?

E2E Automation testing is great to check functionality. It is however poor in the following areas:

  • UI changes
  • Exploratory Cases

UI Changes
An automated test will not be able to test to see that the color of a button is correct, the placement of a field is correct, animations are correct etc. Features such as this can not be tested through traditional E2E Automate.

Exploratory Cases
This is a given, an automated test will only run the scenario which you have written. This means that it will not be able to explore your application and randomly test scenarios. This is where Manual Testing plays a big part. I mention this as there are a number of people who assume automated tests are the replacements to Manual testing.

How do you decide what manual test to automate?

The answer to this can be simple. A manual test becomes a candidate for automation when:

  • It does not have any external dependencies
  • It needs to run very often

Is Automation a replacements for Manual Testing?

No. An automated test simply replaces a scenario which would have been executed manually. An automated test however does not have the ability to explore an application and find bugs outside of automated scenarios. For this reason, automated tests are not a replacement for manual testing.

What tests should not be automated?

Here is a list of tests which should not be automated:

  • Test’s which only run once
  • UI based tests to check for colors, element placements, element size etc

How do you mark the success of an Automated test?

In order to mark an automated test as success, you can apply the following metrics:

  • High success rate of passing (95%+), low failure rate
  • Captured valid bugs

WebDriverJS Dropdown

Do you know how to use WebDriverJS to interact with a Dropdown field? Let’s use WebDriverJS dropdown to select a value?

It is important to be able to select a value from a dropdown box. Let’s have a look at how we can use WebDriverJS to select an item from a dropdown box.

WebDriverJS Dropdown

In the code snippet below, we navigate to a test page which contains a dropdown box. Examine the code below:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get('http://www.thetestroom.com/webapp');
driver.findElement(webdriver.By.id('adoption_link')).click();

The code above navigates to www.TheTestRoom.com, goes to the test site (webapp) and then clicks on the ‘Adoption’ link. Now let’s try to see how we can interact with the dropdown box on that page.

Select from Dropdown field

The code below introduces a selectFromDropdown function.

SelectFromDropdown Function
In order to select a value from the dropdown field, we will introduce a new function.

function selectFromDropdown(value){const valueSelected = webdriver.By.css('[id="start_select"]' + ' option[value="' + value + '"]');
    driver.findElement(webdriver.By.css('[id="start_select"]')).click();
    driver.findElement(valueSelected).click();
}

In the ‘selectFromDropdown’ function, we firstly identify the id of the dropdown field as well as capturing a possible value from the dropdown. We then assign this to a constant variable called ‘valueSelected’.

Finally we click on the dropdown field itself which displays all the options. This then allows us to see all possible options which we are then able to click on.

Let’s have a look at how the script now looks:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
 
driver.get('http://www.thetestroom.com/webapp');
driver.findElement(webdriver.By.id('adoption_link')).click();
selectFromDropdown('today');
driver.quit();
 
function selectFromDropdown(value){const valueSelected = webdriver.By.css('[id="start_select"]' + ' option[value="' + value + '"]');
    driver.findElement(webdriver.By.css('[id="start_select"]')).click();
    driver.findElement(valueSelected).click();
}

Using the function above, you should now be able to change it to help select a value from a dropdown box.

WebDriverJS Switching Windows

When writing an automated test, you may encounter scenarios where you might have to switch browser windows. In this post we will look at how we can switch windows.

WebDriverJS Switching Windows

Firstly, let’s write out in comments what we would like to achieve.

// Save location to parent window
// Print parent window title
// Switch to new window
// Print text in newly switched window
// Switch back to parent window
// Print parent window title

Assuming we are able to achieve the above scenario, we should be able to figure out how to switch windows.

Switching Windows

In order to switch windows, we need to use the ‘getAllWindowHandles()’ command. Let’s have a look below:

driver.getAllWindowHandles().then(function gotWindowHandles(allhandles) {
    // Save location to parent window
    // Print parent window title
    // Switch to new window
    // Print text in newly switched window
    // Switch back to parent window
    // Print parent window title
});

Since ‘getAllWindowHandles()’ returns a promise, we need to wrap it in a ‘then’.

Further Switching Windows

Let’s take this a step further and try to address each of the comments:

driver.getAllWindowHandles().then(function gotWindowHandles(allhandles) {
    // Save location to parent window
    var parent = driver.getWindowHandle();
 
    // Print parent window title
    driver.getTitle().then(function (text) {
        console.log('Parrent Windows: ' + text);
    });
 
    // Switch to new window
    driver.switchTo().window(allhandles[allhandles.length - 1]);
 
    // Print text in newly switched window
    driver.findElement(webdriver.By.css('h1')).then(function (heading) {
        heading.getText().then(function (text) {
            console.log('Child Windows: ' + text);
        });
    });
 
    // Switch back to parent window
    driver.switchTo().window(parent);
 
    // Print parent window title
    driver.getTitle().then(function (text) {
        console.log('Parrent Windows: ' + text);
    });
});

The main things to note here are the variable to capture the parent window and how we switch back and forth between the parent and child windows. It is also important to note that when switching windows, the driver simple gains control over eachwindow. We do not need to use a different driver instance.

Full Example

Script.js

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get('http://www.thetestroom.com/webapp');
driver.findElement(webdriver.By.id('footer_term')).click();
 
driver.getAllWindowHandles().then(function gotWindowHandles(allhandles) {
    // Save location to parent window
    var parent = driver.getWindowHandle();
 
    // Print parent window title
    driver.getTitle().then(function (text) {
        console.log('Parrent Windows: ' + text);
    });
 
    // Switch to new window
    driver.switchTo().window(allhandles[allhandles.length - 1]);
 
    // Print text in newly switched window
    driver.findElement(webdriver.By.css('h1')).then(function (heading) {
        heading.getText().then(function (text) {
            console.log('Child Windows: ' + text);
        });
    });
 
    // Switch back to parent window
    driver.switchTo().window(parent);
 
    // Print parent window title
    driver.getTitle().then(function (text) {
        console.log('Parrent Windows: ' + text);
    });
});

The should output the following:

Expected Output

Parrent Windows: Zoo Adoption | Home
Child Windows: Terms of the site
Parrent Windows: Zoo Adoption | Home

WebDriverJS Properties File

The concept of Properties is a great way to control test configuration. WebDriverJs Properties can help to scale automated tests.

When writing scripts using WebDriverJS, it is important to consider the scale of the test. Being able to change the parameters of multiple tests with a single change can helps scale tests.

WebDriverJS Properties without Properties

Let’s take a simple script. See below:

script.js

var webdriver = require('selenium-webdriver’);
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get('http://www.thetestroom.com');

The script above is very simple. It opens a Chrome browser and navigates to www.TheTestRoom.com. Assume for a moment that the URL for TheTestRoom changes to something else. If the URL changed, we would have to update the script. Updating a single script is not a problem, in fact it is very straight forward. However, what if we have 1000’s of scripts, all which used the URL. With our current approach we would have to update each script individually.

Introducing Properties

Now let’s take a look at the same file, this time however the URL is coming from a properties file.

script.js

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
var prop = require('./proptest');
driver.get(prop.TTL);

properties.js

module.exports = {
    TTL: 'http://www.thetestroom.com'
};

The same URL is now being set in a properties file. The properties file is exported via module.exports. This allows us to use the concept of creating variables in one file and consuming it in many other places.

If the URL changes, we only need to update the properties file ie. in one place. With this approach, we do not need to care about how many files are using the URL. This is because a single change would effect all files automatically.

Conclusion

To wrap this up, what advantage does this actually bring?

Well:
* A single change will effect all files
* Test files becomes easier to manage
* There should not be an instance of ‘accidentally forgetting to update a URL’
* Increase variable names readability

You can apply the same mechanics to help configure what environments to run against, when to run, test priority changes, test suites etc.

Installing WebDriverJS

Welcome to an easy and simple guide to installing WebDriverJS. WebDriverJS is a Web Automation framework which we can use to emulate user actions in a web browser such as Chrome or Firefox. It is a great tool when used in conjunction with NodeJS. In this tutorial we will firstly ensure we have all the required tools in place to eventually write a basic script which will open a Chrome browser and navigate to www.thetestroom.com, let’s begin.

Installing WebDriverJS – Installing NodeJS

There are a number of ways to install WebDriverJS but perhaps the most efficient and simplest way is to do it through ‘npm’. ‘npm’ is NodeJS’s package manager and it really helps to download modules and dependency through the command line very easily. We will see how to use ‘npm’ soon(ish), firstly we need to install NodeJS.

Navigate to NodeJS.org and download the LTS version. This is assumed to be the most stable version and should be very comfortable to use. Once downloaded simply run the installer.

Now that you have installed NodeJS on your machine lets check if the installation succeeded. Open a console window and type in the following:

node -v

This should return the version of NodeJS which you downloaded. With this done now let’s check to see that you have ‘npm’ installed. Type the following in the console window:

npm -v

This should return the version of ‘npm’ you have. Great, we now have both NodeJS and ‘npm’ installed.

Project Setup

With NodeJS now ready, let’s go ahead and start setting up our project directory. Navigate to a directory of your choice and create a new folder called ‘webdriver-js-project’. Before we actually begin to use ‘npm’ to install WebDriverJS we should spend a second or two deciding on which web browser to run our WebDriverJS scripts against? There are a number of options but for the moment (and to keep things a little simple) I would suggest we roll with ChromeDriver. ChromeDriver is an executable file which is used to control Chrome. This means you will need to have Chrome installed on your machine. Ensure that you have Google Chrome installed and once confirmed navigate to ChromeDriver and download the latest ChromeDriver. Once Downloaded simply extract the driver and place the exe file in the ‘webdriver-js-project’ folder.

Great stuff, you now have both Google Chrome as well as the driver ready on your machine. The final step of the setup phase is to download WebDriverJS via ‘npm’. Open a command windows (or terminal) and ensure you are in ‘webdriver-js-project’ and run the following command:

npm install --save--dev selenium-webdriver

This will install WebDriverJS locally in ‘webdriver-js-project’. If you have a quick look now at your project then you will notice that now there is a new directory called ‘node_modules’. If you dig into that folder then you will also notice that it contains a ‘selenium’ folder. Congratulations, we just used ‘npm’ to install WebDriverJS.

We now have all the required components to start writing our first script in WebDriverJS.

Writing A Basic WebDriverJS Script

Finally, let’s write our script. Let’s begin by creating a JavaScript file. Navigate to ‘webdriver-js-project’ and create a new file called test.js. With the test file now created add this line of code:

var webdriver = require('selenium-webdriver');

Let’s discuss what is happening in the line above. The line above is declaring a new variable called webdriver. We then assigned selenium-webdriver (the same module which we installed via ‘npm’) to that variable we gained access to the building methods inside Selenium. With this said let’s create our driver variable:

var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();

As you can see we are now able to use our webdriver variable to identify the browser we want to use and build it as part of a driver instance. Once we build the instance we are able to assign this to a driver variable. The driver variable now contains all the methods which can now allow us to interact with the chrome browser. With this said lets actually do something with the browser, let’s try to navigate to a URL:

driver.get('http://www.thetestroom.com');

The line above uses the driver variable that we created and makes a call to the ‘get’ method. This method is used to navigate to a URL. Your test.js file should now look like this:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get('http://www.thetestroom.com');

With the script now ready let’s actually run the file. Using command line navigate to ‘webdriver-js/project’ directory and run the following command:

node test.js

This should have now opened a Chrome browser and navigated to www.thetestroom.com. Congratulations as you have just written your test script using WebDriverJS.

Cucumber Background Tutorial

When writing multiple scenarios in a Feature file we can easily end up writing the same ‘introductory’. However we can reduce the duplication using Cucumber Background. Let’s have a look at how we can use Cucumber Background to run common steps.

Cucumber Background

Lets have a look at a Feature file with multiple Scenarios which use similar steps:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Scenario: To check that the Java Cucumber tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java Cucumber Tutorial" page
Then the page title should be visible

Scenario: To check that the Git tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Git Tutorial" page
Then the page title should be visible

If you have a look at the above Scenarios then you can see that we repeat the same ‘Given’ step across all Scenarios. This means that if we have to write more Scenarios then we will end up writing the same test step for each Scenario. This is where we can reduce running the same test steps via Cucumber Background. Lets have a look at how we can use Cucumber Background to help us:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Background:
Given I navigate to TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Scenario: To check that the Java Cucumber tutorial main page has loaded
Given I navigate to "Java Cucumber Tutorial" page
Then the page title should be visible

Scenario: To check that the Git tutorial main page has loaded
Given I navigate to "Git Tutorial" page
Then the page title should be visible

The Background section in a Feature file is designed to run test steps before each Scenario in the Feature file. For instance in the above Feature file the Background section will run before every Scenario. This means every Scenario will automatically run the ‘Given’ step for each Scenario.

And that’s it. In this tutorial we have learned how to use Cucumber Background and apply it to a Feature file. We have seen how the Background script can be used to reduce test step duplication and also increase Feature file management.

Video Tutorial on YouTube

cucumber background

Direct Video Link

Cucumber Datatable Tutorial

When we pass in parameters into a Step Definition then we can only pass in a single value however we can overcome this issue and pass in chunks of data via Cucumber DataTable. We can use Cucumber DataTable to pass in an array of information in a single step.

Using Cucumber Datatable

Let’s have a quick look at how we can pass in a single value via a test step in a Feature file and into a Step Definition method:

Feature File:

When I click on "Cucumber Java Tutorial"

Step Definition:

@When("^When I click on \"([^\"]*)\"$")
public void iClickOn(String link){
//code here
}

We can see that in the above Feature file we are passing in a single String value from the Feature file and into the Step Definition method. However in this current context we are not able to pass in a chunk of data. To put it slightly differently when using Cucumber Parameters it is not possible to pass in ‘arrays’ of information. This is where we can apply Cucumber DataTable policy. Cucumber DataTable can be used to pass in arrays of information in a single step. Here is an example of where I use Cucumber DataTable:

Feature File:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com
  Scenario: To check that tutorial pages have loaded
    Given I navigate to TheTestRoom.com
    When I navigate to "Java WebDriver Tutorial" page
    Then the following links should be visible
      | Java Cucumber Tutorial |
      | Git Tutorial           |

Step Definition:

    @Then("^the following links should be visible$")
    public void the_following_links_should_be_visible(List linkData) throws Throwable {
        // List, List>, List> or Map.
        for (int i = 0; i < linkData.size(); i++) {
            System.out.println(linkData.get(i));
        }
    }

Looking at the above Feature file you can see that we are now passing in a table of data into a single step. If you look at the matching Step Definition then you can see that we are capturing that information in a List i.e. an array. When we pass in data in a Feature file in the above format then this allows us to pass in rows of information which in turn can be captured in an array in a Step Definition. This data in a Step Definition method is actually captured as a DataTable (a collection container in Cucumber) however we can cast it to be captured as a List or Map instead as part of the method signature. After we cast it to either a List of a Map, this makes it easier to work with in our Step Definition methods.

And that's it folks. In this tutorial we have learned how to pass in arrays of data from a Feature file and into a Step Definition method. We have seen how powerful this simple technique is as this gives another option to passing data in 'bulks' as opposed to single values.

Video Tutorial on YouTube

cucumber datatable

Direct Video Link

Cucumber Scenario Outline Tutorial

Passing parameters in Cucumber is a powerful feature but we can extend this by utilizing ‘Cucumber Scenario Outline’.

Cucumber Scenario Outline

When writing Scenarios in Feature files it is always ideal to write specific Scenarios as to ensure that they make sense and bring value. For instance if we take the following example:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Then we can see that the Scenario is very specific. If you want to write more Scenarios around checking ‘page load’ for other pages then you should have a Scenario for each page:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java WebDriver Tutorial" page
Then the page title should be visible

Scenario: To check that the Java Cucumber tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Java Cucumber Tutorial" page
Then the page title should be visible

Scenario: To check that the Git tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "Git Tutorial" page
Then the page title should be visible

Looking at the above Feature file you can easily see that the Feature file contains three Scenarios. Although there are multiple Scenarios we also take advantage of passing in parameters as to reduce the number of step definition methods that we need to write. If you look through all three Scenarios then you should be able to see that the test steps between all three Scenarios are very similar, the only difference being the parameter. With this in mind it does feel like we are repeating ourselves a lot here since the Scenarios are pretty much the same. For instances such as this were the Scenarios are very similar with the exception being the parameters that are passed in, we can use Cucumber Scenario Outline to pass in multiple parameters into a single Scenario.

Using Cucumber Scenario Outline

Cucumber Scenario Outline look very similar to Scenarios but the only different is that the parameter which we pass in don’t sit in the Scenario itself but instead in an ‘Examples’ table directly below the Scenario. If we convert the above three Scenarios to instead use a Scenario Outline then this is what it would look like:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario Outline: To check that the Java WebDriver tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to "<page>" page
Then the page title should be visible
Examples:
|page|
|Java WebDriver Tutorial|
|Java Cucumber Tutorial|
|Git Tutorial|

So what is happening in the Feature file above? There are a number of differences in the Scenario Outline Feature file above so lets go through them. If you notice instead of passing in an actual value in the ‘When’ step we are now passing in a reference to a ‘page’ parameter. The value of the ‘page’ is obtained from the ‘Examples’ table. In the previous Feature file we had three Scenarios but this has been condensed into a single Scenario Outline in this Feature file. Cucumber Scenario Outline works slightly differently as they run multiple times as opposed to Scenarios which run only the one time. When looking at the ‘Examples’ table the first row is used to identify the name of the parameter reference which we will use in the Scenario Outline. This means for each row other than the first row in an Examples table a Scenario Outline will run itself. This means since there are three data rows in the ‘Examples’ table the Scenario Outline will run three times where each time it run’s it will substitute the value of ‘page’ from the ‘Examples’ table.

Cucumber Scenario Outline really help to slim down having to write similar Scenarios and also help us to run the same Scenario Outline using multiple parameters. This is a great way of reducing Scenario duplication and also a way of managing better Feature files.

And that’s it for this tutorial folks.

Video Tutorial on YouTube

cucumber scenario outline

Direct Video Link

Cucumber Parameter Tutorial

Now that we have setup an IDE we can start to look at passing Cucumber parameters from a Feature file and into a step definition class. One of the features of a Feature file is the ability to pass a Cucumber Parameter to a step definition class. We can use this ability to help write better step definition methods which can cater for multiple test steps in a Feature file. What this means is that a number of test steps can now reference the same step definition method. Lets have a look at how we can achieve this.

How does a Test Step find a Step Definition

We have talked about how a Feature file actually finds a step definition class (via the glue parameter in Cucumber) but we have not talked about how a test step in a Feature file actually finds a matching step definition in a step definition class. When a Feature file has located a step definition class it uses Regex to match a test step in a Feature file to a step definition method in a step definition class. For the following test step ‘Given I navigate to TheTestRoom.com’ the text after the Gherkin keyword i.e. ‘I navigate to TheTestRoom.com’ is used as the String to match the Regex pattern in a step definition annotation. For instance:

Feature File:
Scenario: Check Cucumber Tutorial page has loaded
Given I navigate to thetestroom.com

And now the step definition:

@Given("^I navigate to thetestroom\\.com$")
public void navigateToTheTestRoom() {
// code here
}

If look at the text for the test step in the Feature file and the expected Regex pattern in the step definition, then you can see that they match. It is the line of text (or String) in the Feature file which is used by Cucumber to actually match a test step to a step definition method.

Regex Patterns to understand

Before we go into detail about how we actually match the text of a test step to a step definition, we need to gain a small understand of Regex patterns used by Cucumber. First of all what is Regex? Regex is a pattern tool which is used to match Strings across any text based documents. We can use Regex to define patterns which we can then use to scan entire documents to help find matching strings. Regex has a number of ways to match Strings but the functions which we are most interested in are listed below:

^ - This marks the beginning of the string
$ - This marks the end of the string
\ - This is used to allow using special characters in a Regex pattern such as " and .
* - This is used to mean either zero, one of more
( ) - The text in these brackets are used to identify grouped characters
[ ] - This text is in these square brackets are used to match a single character using an or expression
. - This text will be used represent any character

Using the above we can combine some of the syntax to get certain types of results. For instance:

^Navigate to thetestroom$ - This will look for an exact match of the String 'Navigate to thetestroom'
^I navigate to thetestroom\\.com$ - This will look for an exact match of the String 'Navigate to thetestroom.com'
^I .* to thetestroom\\.com$- This will look for an exact match of the String 'Navigate to thetestroom.com'

Regex has many more features and methods which allow someone to match Strings but the functions above are enough to get us started with Cucumber. We will be using the above Regex functions to help us match the text in a test step against a step definition method.

Passing Cucumber Parameter

Now that we have gained a simple understand of Regex, lets have a look at how we can pass a Cucumber Parameter from a Feature file and into a step definition class.

We can capture specific sections in a test step to pass as a Cucumber parameter in a a Feature file. I usually do this using double quotation marks in a test step to make it easier for me to capture the parameter which I am about to send. This is also good practice as Cucumber Feature files highlight this as a parameter:

When I click on "Cucumber Java Tutorial"

And in the step definition method we can use Regex to capture all the text in the double quotation marks which we pass from the Feature file like so and capture in a step definition method:

@When("^When I click on \"([^\"]*)\"$")
public void iClickOn(String link){
//code here
}

If you have a look at the above step definition method then you should be able to see that we are able to capture any parameter that we pass from a Feature file test step and into a Step Definition class step definition method. We can then use these parameters to create step definitions which we can use for multiple test steps. This means we can now do the following:

When I click on "Cucumber Java Tutorial"
When I click on "WebDriver Java Tutorial"
When I click on "AngularJS Java Tutorial"

All three above steps can now be used to refer to the same step definition as all the text in each test step now matches the regex pattern in the same step definition method.

And that’s it folks. In this tutorial we have looked at how to pass a Cucumber Parameter from a Feature file and into a step definition class. Using this information we can now easily pass a Cucumber Parameter and really start to take advantage of the power of Cucumber.

Video Tutorial on YouTube

cucumber parameter

Direct Video Link

Cucumber Maven Eclipse Setup Tutorial

With basic WebDriver and Cucumber script writing tutorials now covered we will look at Cucumber Maven Eclipse setup. We will look at setting up Cucumber using Maven and we will do this through an Eclipse. Cucumber Maven Eclipse setup should lay foundations for many blog tutorials in this course.

01 Cucumber Maven Eclipse Setup

What is Maven? Maven is a build tool which can be used to build projects and manage their dependencies. What this means is that Maven can be used to automatically download jars for a given Java project without the user having to manually download any. It makes it more easier and manageable when building and maintaining projects. To date we have been manually download our jars from websites such as Selenium and Maven Repository but with Maven all of that is taken care of automatically.

02 IDE Installation

What is an IDE? An IDE (Integrated Development Environment) is the technical term given to a product which allows someone to write code or scripts through a user interface. An IDE can also allow the same user to download plugins which allows further customization, for instance some IDE’s do not support Cucumber straight out of the box but with the power of a plugins this can easily be remedied.

Eclipse is perhaps one of the most common IDE’s available to use, it is open source and very famous among both new and senior developers. There are other options such as IntelliJ but in this blog I am picking Eclipse simply because it’s simplicity and ease of use. Let go ahead and download Eclipse from https://eclipse.org/downloads/.

I am going to download ‘Eclipse IDE for Java Developers’. After your download completes then you should notice that the fully downloaded file is a zipped file. This means you will need an ‘unzipping’ program to unzip the file. Eclipse downloads are a little different as the file you have just downloaded is the whole applcation. Sorry if you were expecting an installer but luckily with Eclipse once you unzip the folder all you have to do is copy the unzipped folder to a location of your choosing. I personally will move it to my C: driver. Which ever location you chose, open the folder and start Eclipse by double clicking on the Eclipse application icon.

If Eclipse opens then you have completed your IDE installation. At the time of writing this blog the version of Eclipse IDE for Java Developers is Mars.2 Release (4.5.2).

03 Getting Cucumber to work with Eclipse

The first thing we need to do is install a Cucumber plugin to help us write Feature files for Eclipse although a plugin is strictly not needed to write Feature Files in Eclipse. To add the plugin select ‘Help’ from the main menu and select ‘Install New Software’. Once the window opens click on the ‘Add’ button from the top right and enter the following:

Name: cucumber
Location:  http://cucumber.github.com/cucumber-eclipse/update-site

Click ‘OK’ to add the plugin to your plugin list. Select ‘Cucumber Eclipse Plugin’ from the newly generated list and hit the ‘Next’ button until the plugin is installed. Accept any warnings which may display and once the plugin is installed Eclipse will perform a restart. The version of the plugin at the point of time when writing this blog is ‘0.0.15.201605080636’. This is one of the first steps towards Cucumber Maven Eclipse setup.

With the plugin installed lets go ahead and create a brand new Maven project. To do this select ‘New’ from the top bar menu and select ‘Other’. When the ‘New Wizard’ window opens type in Maven into the filter box and select ‘Maven Project’. This will display the New Maven project windows. Lets go with the default settings for now. When Group Id and Artifact Id are displayed type in com.thetestroom for the Group Id and cucumber_project for the Artifact Id and click ‘Finish’. This should have now created your Maven project.

With the project window opened (if it is not then ensure all other Eclispe windows are closed such as the ‘Welcome’ window) right click on src/test/java/com.thetestroom/cucumber_project and select ‘new’ and then ‘file’. In the file dialog box set the file name to myFirstFeature.feature. Notice that this creates a Feature File which Eclipse recognizes. Copy in the following contents into the Feature File:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the WebDriver Cucumber tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to Cucumber Tutorial page
Then the page title should be visible

Once done right click inside the Feature File and select ‘Run As’ and then ‘Cucumber Feature’. As you are able to see the option to run a Feature File as Cucumber Feature then this proves that your Eclipse is now configured to run Feature Files. This is great. After running your Feature file you should see the output below:

Error: Could not find or load main class cucumber.api.cli.Main

This means that you don’t have the required files yet for this Cucumber Maven Eclipse setup. To do this we need to add dependencies into the pom.xml file. In previous tutorials we used to copy in the Jars manually but with Maven we don’t have to do any of this since Maven will automatically download the jars and place them in the Classpath. This is what makes Maven so powerful. Copy in the following dependencies below into your pom.xml file:


     info.cukes
     cucumber-jvm-deps
     1.0.5


     info.cukes
     gherking
     2.12.2


     info.cukes
     cucumber-core
     1.2.4


     info.cukes
     cucumber-java
     1.2.4

Now right click on your Featiure file and run it as a Cucumber Feature and the output should be something like this:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the WebDriver Cucumber tutorial main page has loaded # C:/Users/MohammedShahin/workspace/cucumber_project/src/test/java/com/thetestroom/cucumber_project/myFirstFeature.feature:3
    Given I navigate to TheTestRoom.com
    When I navigate to Cucumber Tutorial page
    Then the page title should be visible

1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s

You can implement missing steps with the snippets below:

@Given("^I navigate to TheTestRoom\\.com$")
public void i_navigate_to_TheTestRoom_com() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}
@When("^I navigate to Cucumber Tutorial page$")
public void i_navigate_to_Cucumber_Tutorial_page() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}
@Then("^the page title should be visible$")
public void the_page_title_should_be_visible() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}  

Great, it looks like we have been able to get Cucumber to now fully work via Eclipse. Only a handful of tasks remain now to complete this Cucumber Maven Eclipse setup. First we need to create the Step Definition file so lets create a new Java class called myFirstStepDefinition under src/test/java/com.thetestroom/cucumber_project/ and copy in the contents as shown below:

package com.thetestroom.cucumber_project;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.java.en.*;

public class myFirstStepDefinition {
    WebDriver driver;

    @Given("^I navigate to TheTestRoom\\.com$")
    public void i_navigate_to_TheTestRoom_com() throws Throwable {
        System. out .println("Going to TheTestRoom.com" );
        driver = new FirefoxDriver();
        driver .navigate().to("http://www.thetestroom.com" );
    }
    @When("^I navigate to Cucumber Tutorial page$")
    public void i_navigate_to_Cucumber_Tutorial_page() throws Throwable {
        System. out .println("Clicking on Cucumber Tutorial Page" );
        driver .findElement(By.linkText( "Java Cucumber Tutorial")).click();
    }
    @Then("^the page title should be visible$")
    public void the_page_title_should_be_visible() throws Throwable {
        System. out .println("Checking page title" );
        System. out .println(driver .getTitle());
        driver .quit();
    }
}

We also need to add in WebDriver (Selenium) as a dependency in our pom.xml file. Add in the dependency below:


     org.seleniumhq.selenium
     selenium-java
     2.53.0

And that’s it. Now run the Feature file on more time. Right click on it and select ‘Run As’ and then ‘Cucumber Feature’. You should now see a Firefox web browser open, navigate to TheTestRoom.com, click on the Cucumber Tutorial link and print out the title of the page.

And that’s it folks, we now have a working Eclipse Cucumber Project. In this tutorial we have learnt how to use Maven to help manage our dependencies and as well as learning how to run Cucumber Features via Eclipse. We have now covered a full tutorial on Cucumber Maven Eclipse setup.

Video Tutorial on YouTube

cucumber maven eclipse setup

Direct Video Link