I tried to write some simple Cucumber tests for my blog from scratch. Now separated by a few weeks, or decided to organize the whole process into a blog article, one is to share with you, reference correction, the second is to facilitate their later back to read.

Technology stack selection

In order to facilitate Coding, this article will be based on the following technology stack:

  • Language: Java8
  • Framework: SpringBoot
  • Build tool: Gradle
  • WebDriver: Selenium
  • BDD framework: Cucumber
  • Unit testing framework: Junit

Capital construction

Before you start, make sure you have Java8 installed on your machine. As a new generation of Spring framework, Spring Boot improves the development experience of Spring developers. In principle, it is not necessary to use Spring Boot. Gradle as an automated build tool also greatly improves the experience of developers building projects. To automatically generate basic Project files, go to start.spring. IO and change Maven Project to Gradle Project. Group and Artifact names can be changed to your own custom names, as shown below:

Since this initial tool doesn’t provide any cucumber-related Dependencies, we don’t need to add anything in Dependencies. Then click on “Generate Project” and it will automatically Generate an initial Project based on SpringBoot and Gradle, packaged as a ZIP and automatically downloaded locally through the browser. Decompress the zip package and enter the following information in the command line interface (the working directory is the project root directory) :

Of course, if you have Gradle installed locally, you can also use:

Gradle then installs some basic dependencies (including Junit) on your local machine to build the basic tools you need next. You can open the project using an IDE such as Intellij and change parts of the plugin in the build.gralde file depending on the IDE used.

Selenium Test

Before writing Cucumber Test, let’s learn how to write Selenium Test. Selenium is a WebDriver framework that supports a variety of commonly used browsers that drive various platforms. It was originally developed by ThoughtWorks. Generally speaking, it can realize the automatic operation of various elements in the browser (including click, fill in, etc.), so it can be used in various BDD frameworks/automated test tools, including Cucumber, Jbehave, SauceLabs, etc. So we used Selenium to write simple behavior-level tests first, and then Cucumber to transform them into more BDD oriented tests. Here, in my blog, http://yaowenjie.github.io, for example, will be in the next few behavior of automation:

  1. To enter this web page at http://yaowenjie.github.io
  2. Click the search button on the right
  3. Enter the keyword to search for “PowerShell” in the input box
  4. Click the first search result to enter the page of this article

Before writing the code, you need to add selenium dependency to dependencies in build.gradle:

The compile (' org. Seleniumhq. Selenium, selenium - Java: 2.53.0 ')Copy the code

Then need to perform again before the build instructions (. / gradlew clean build), then you will have to stay in YourProject/SRC/main/Java/com/wenjie to create a new class (called BlogPage), To represent the corresponding element under the page and the operation on it, the code is as follows:

package com.wenjie; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; public class BlogPage { @FindBy(xpath = "//*[@id=\"masthead\"]/button") private WebElement searchButton; @FindBy(xpath = "/html/body/div[1]/div/input") private WebElement searchInput; @FindBy(xpath = "/html/body/div[1]/div/ul/li[1]/article/a") private WebElement searchResultFirst; public void clickSearchButton() { searchButton.click(); } public void inputSearchWording(String wantedStr){ searchInput.sendKeys(wantedStr); } public void clickFirstResultOfSearch() { searchResultFirst.click(); }}Copy the code

You can see that the BlogPage defines three WebElement, the search button, the search input box, and the first of the search results (all defined in xpath or ID form), and exposes the click and input actions on these three elements in three ways. You can then create a new test class (or build on the existing test class) and write a corresponding test as follows:

package com.wenjie; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.firefox.FirefoxDriver; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.openqa.selenium.support.PageFactory.initElements; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CucumberApplication.class) public class CucumberApplicationTests { FirefoxDriver driver = new FirefoxDriver(); BlogPage blogPage = initElements(driver, BlogPage.class); String blogUrl = "http://yaowenjie.github.io"; @Test public void search_function_should_work_well() { driver.get(blogUrl); blogPage.clickSearchButton(); blogPage.inputSearchWording("PowerShell"); blogPage.clickFirstResultOfSearch(); }}Copy the code

It’s easy to see what this test does from the code above:

  • Open http://yaowenjie.github.io – driver. The get (blogUrl);
  • Click on the search button – blogPage. ClickSearchButton ();
  • Input a keyword – blogPage. InputSearchWording (” PowerShell “);
  • First click on the search results – blogPage. ClickFirstResultOfSearch ();

Finally, execute this test:

You’ll see that the test calls the Firefox browser (how about Chrome or another browser?). “And automatically do what you just did. Isn’t that cool? At this point, the simple Selenium Test is complete. You can also use this method to automate other web pages. Next, you’ll see how to write a simple Cucumber Test based on this.