Objective

Using Selenium with Cucumber and Gradle instead of Maven. We are going to use:

  • IntelliJ Ultimate Edition
  • Cucumber
  • Gradle
  • Selenium
  • Java
  • TestNG

Purpose is to move from maven to gradle to get more options and flexibility which is helpful in CI/CD integration.

Creating Project

Lets quickly create a Gradle project in IntelliJ. Open File > New > Project and select Gradle. Check Java under Additional Libraries and Frameworks. If needed you can use Groovy as well. But at the moment, we are just going to stick to Java.

gradle project

Select “Gradle Wrapper” when asked during the process.

build.gradle

Once project is configured, you’ll see build.gradle file along with some other gradle related stuff like gradlew, settings.gradle etc.

build.gradle is same as pom.xml file in maven. Here we make all the configuration related to our project.

Dependencies in build.gradle

Just like we have dependencies in Maven, we need to add the same in our gradle file. Add following dependencies in build.gradle.

dependencies {
    compile 'org.seleniumhq.selenium:selenium-server:2.44.0'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.4.0'
    compile 'org.testng:testng:6.1.1'
    // https://mvnrepository.com/artifact/info.cukes/cucumber-testng
    compile group: 'info.cukes', name: 'cucumber-testng', version: '1.2.5'
    // https://mvnrepository.com/artifact/info.cukes/cucumber-java
    compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'

}

Repositories

We also need repositories to be defined. By default mavencentral is always there, if not then just copy paste the following code in your build.gradle.

repositories {
    mavenCentral()
    jcenter()
}

Configurations

Now we need to add configuration for cucumber so that our corresponding task will be able to execute. Paste the following code in build.gradle.

configurations {
    cucumberRuntime.extendsFrom testRuntime
}

Task

Finally we need to create a gradle task to execute cucumber test cases. Paste the following code into your build.gradle.

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'stepDef', 'src/test/java']
        }
    }
}

Although, this at the moment is not relevant since we haven’t created any directories or packages, but just in case, note that args is the important line in above task.

  • ‘stepDef’ is the package name in our project where we have kept our step definitions for cucumber. You can provide the path according to your project.
  • ‘src/test/java’ is the path for our test cases and feature files. No need to provide any specify package path. It’ll automatically pick all the feature files available in any package/folder in the provided path.

The final build.gradle file will be as below.

group 'com.seleniumtestcucumber.mytest'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'

configurations {
    cucumberRuntime.extendsFrom testRuntime
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'stepDef', 'src/test/java']
        }
    }
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile 'org.seleniumhq.selenium:selenium-server:2.44.0'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.4.0'
    compile 'org.testng:testng:6.1.1'
    // https://mvnrepository.com/artifact/info.cukes/cucumber-testng
    compile group: 'info.cukes', name: 'cucumber-testng', version: '1.2.5'
    // https://mvnrepository.com/artifact/info.cukes/cucumber-java
    compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'

}

Feature File

Now lets start with creating our feature file. In this example I’m creating one in src/test/java location. Create a package features. Create Contact_Form.feature file and paste following code into it.

Feature: Using Contact Form
 To test the functionality of contact form

  Scenario : Filling contact form
   Given I am on Home Page of "http://room5.trivago.com/contact/"
   And Dismiss cookies popup
   When I enter message as "some gibberish"
   And I enter full name as "My Name"
   And I enter email as "ashish@deshmukh.com"
   And I click on Submit button
   Then I see success message

Beauty of BDD is you can quickly grasp what we are going to do.

Step Definations

Now lets create our stepdefinations file. In scr/test/java create a new package stepDef and create Contact_Form.java file. Paste following code into it.

package stepDef;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import pageObjectModels.ContactPageObjectModel;
import pageObjectModels.CookiesNoticePageObjectModel;
import pageObjectModels.FooterPageObjectModel;

public class Contact_Form {
    public static WebDriver driver;

    @Given("^I am on Home Page of \"([^\"]*)\"$")
    public void i_am_on_Home_Page_of(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(arg1);
        objContact = new ContactPageObjectModel(driver);
        objCookies = new CookiesNoticePageObjectModel(driver);
    }

    @Given("^Dismiss cookies popup$")
    public void dismiss_cookies_popup() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        driver.findElement(By.id("cn-accept-cookie")).click();
    }

    @When("^I enter message as \"([^\"]*)\"$")
    public void i_enter_message_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        driver.findElement(By.id("message")).sendKeys(arg1);
    }

    @When("^I enter full name as \"([^\"]*)\"$")
    public void i_enter_full_name_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        driver.findElement(By.id("full_name")).sendKeys(arg1);
    }

    @When("^I enter email as \"([^\"]*)\"$")
    public void i_enter_email_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        driver.findElement(By.id("email")).sendKeys(arg1);
    }

    @When("^I click on Submit button$")
    public void i_click_on_Submit_button() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        driver.findElement(By.id("contactform-submit")).click();
    }

    @Then("^I see success message$")
    public void i_see_success_message() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        String status = driver.findElement(By.xpath("//*[@id='content']/div/div[2]/div/div/div")).getAttribute("class");
        Assert.assertEquals(status, "Success");
        driver.quit();
    }
}

And we are done.

Note that since we are going to run the project via Gradle, there is no need to create a runner class for cucumber. Instead we have already created equivalent of the same in our build.gradle file with task cucumber previously.

Running Gradle Task

Now all that remains is to run the gradle task. This can be done by simply navigating to View > Tool Window > Gradle. You’ll see Gradle window in the IntelliJ.

gradle window intellij

Click on the “Execute Gradle Task” button as highlighted in the above image.

You’ll see small pop-up as below. Enter your task name i.e. cucumber

run gradle task - cucumber - softwareautomata

Click OK and execution will begin.

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s