This article is contributed. See the original author and article here.
By Anusha Ammaluru
This time we bring you a blog article about Cucumber, Selenium and Integration with Azure DevOps, let’s get started and welcome to the journey to learn Cucumber.
The blog post will cover the following topics:
- Cucumber Introduction
- Setup Cucumber with Selenium in Eclipse
- Cucumber Basics
- Eclipse Integration with Azure DevOps
Cucumber Introduction
Cucumber is a tool that supports Behaviour-Driven Development(BDD). It lets us define application behavior in plain meaningful English text using a simple grammar defined by a language called Gherkin. Cucumber itself is written in Ruby, but it can be used to “test” code written in Ruby or other languages.
Cucumber is one of the most powerful tools. It offers us the real communication layer on top of a robust testing framework. The tool can help run automation tests on wide-ranging testing needs from the backend to the frontend. Moreover, Cucumber creates deep connections among members of the testing team, which we hardly found in other testing frameworks.
What is Gherkin? It is a simple, lightweight, and structured language, which uses regular spoken language to describe user requirements and scenarios. Examples of regular spoken language are English, French, and around 30 more languages. Gherkin contains a set of syntax/keywords
Feature: Defines the feature (generally a user story)
Given: Specifies the pre-condition of the test
And: Defines additional conditions of the test
Then: States the post-condition/expected result of the test
Key points to note:
- The test is written in plain English, which is common to all the domains of your project team.
- This test is structured that makes it capable of being read in an automated way. Thereby creating automation tests at the same time while describing the scenario.
Setup Cucumber with Selenium in Eclipse
- Setup java
- Download the latest version of Java from https://www.oracle.com/technetwork/java/javase/downloads/index.html
Download .exe file
- Install Java by double-clicking on the .exe file and proceed with default options
- Set up Java Environment Path
- Type Env in the windows search and click on the ‘Environment Variables’ option
- Click on Path in the System variables and click the ‘Edit’ button
- Add folder path where the JDK is installed and click the ‘OK’ button
- Go to Command Prompt and type java -version If you see a screen like below, Java is installed
- Type Env in the windows search and click on the ‘Environment Variables’ option
- Download the latest version of Java from https://www.oracle.com/technetwork/java/javase/downloads/index.html
- Setup Eclipse
- Download Eclipse Photon from https://www.eclipse.org/downloads/packages/release/photon/rc3, if you are going to maintain the repo in Azure DevOps, I had faced issues earlier with other bundles.
- Select Eclipse IDE for Java Developers and Install
- Launch the eclipse and select a workspace folder to save your repo
- Download Eclipse Photon from https://www.eclipse.org/downloads/packages/release/photon/rc3, if you are going to maintain the repo in Azure DevOps, I had faced issues earlier with other bundles.
- Install Cucumber Plugin for Eclipse
- Launch the Eclipse IDE and from the Help menu, click “Install New Software”.
- You will see a dialog window, click the “Add” button.
- Enter the name “Cucumber” and location as “http://cucumber.github.com/cucumber-eclipse/update-site”. Click the ‘OK’ button.
- Check the ‘Cucumber Eclipse Plugin’ and click next to install
- You may or may not encounter a Security warning, but if you do just click the ‘OK’ button.
- Restart Eclipse for the changes to take effect
- Download Cucumber JARS from Maven Repo
- Go to https://search.maven.org.
- Search for cucumber-core in the Central Maven Repository.
- Download jar file
- Similarly, search for all the below libs in the Maven repo and download JAR’s
- Download Selenium
- Download WebDriver Java client from Selenium website
- Extract the files and save it in your local folder
- Download WebDriver Java client from Selenium website
- Configure Eclipse with Cucumber and Selenium WebDriver libs
- Go to Eclipse : File-> New -> Project -> Select Maven
- Click the ‘Next’ button
- Filter Cucumber and select cucumber-archetype
- Enter Group Id and Artifact Id and click the ‘Finish’ button
- Add Selenium Jars
- Right-click on Project ‘CucumberTest > Select Properties > Java build path. Then navigate to the Libraries tab and click Add External JARs.
- Browse to the local folder where the selenium jars are saved and select all the jars and add
- Go to the lib folder and add all the remaining Jars
- Click ok
- Add Cucumber Jars
- Right-click on Project ‘CucumberTest > Select Properties > Java build path. Then navigate to the Libraries tab and click Add External JARs.
- Browse to the local folder where the cucumber jars are saved and select all the jars and add
- Click the ‘Ok’ button
- Now we are all set :smiling_face_with_smiling_eyes:
- Go to Eclipse : File-> New -> Project -> Select Maven
Cucumber Basics
- Cucumber Feature File
- An entry point to the Cucumber tests. It contains a list of scenarios/test cases written in natural English language using Gherkin keywords
- Create a package in eclipse project -> name it as ‘Features’
- Right-click on the ‘Features’ package and create a file -> name it as ‘DemoTest.feature’
- Add a Feature: Name and description and a scenario underneath like the below example
- Step Definitions
- Add selenium java code in the step definition methods corresponding to feature files.
- Sample code:
@Given(“user is on home page“)
public void user_is_on_home_page() {
driver.get(“https://pul-ai-anu.azurewebsites.net/“);
driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
}
@When(“I click on login link“)
public void i_click_on_login_link() {
login = new LoginPage(driver);
login.lnk_Login.click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@When(“I enter username {string}“)
public void i_enter_username(String email) {
login = new LoginPage(driver);
login.txtbx_UserName.sendKeys(email);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@@When(“I enter password {string}“
public void i_enter_password(String password) {
login = new LoginPage(driver);
login.txtbx_Password.sendKeys(password);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@When(“I click on login button“)
public void i_click_on_login_button() {
login = new LoginPage(driver);
login.btn_Login.click();
driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
}
@Then(“I verify the login is successful“)
public void i_verify_the_login_is_successful() {
login = new LoginPage(driver);
assert(login.lnk_profile.isEnabled());
}
Junit Test Runner Class
package runner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {“pretty“},
features= “srctestresourcesfeatures” ,
glue= “stepDefinitions“)
public class RunCucumberTest {
}
Right-click on TestRunner class and Click Run As > JUnit Test Application
Test Run Report will be shown like this
Eclipse Integration with Azure DevOps
- Reference link: https://docs.microsoft.com/en-us/azure/devops/java/labs/eclipsegit/?view=azure-devops
- If you face issues in accessing the Teams Explorer, follow the below workaround
- Credits: https://github.com/Microsoft/team-explorer-everywhere/issues/285#issuecomment-474944707
- Download jaxb-api-osgi jar from Maven Central (https://search.maven.org/search?q=g:javax.xml.bind%20AND%20a:jaxb-api-osgi&core=gav)
- Copy/move this jar to your eclipse/dropins directory
- Edit eclipse/plugins/com.microsoft.tfs.core_14.134.0.201804261732/META-INF/MANIFEST.MF
- append “,javax.xml.bind” to the “Import-Packages” line. It should look like this when complete:
- Import-Package: org.eclipse.equinox.security.storage,javax.xml.bind
- restart eclipse with -clean option so OSGi bundles are recomputed
- Right-click on the project -> Team and you will have the rest of the git operations
Conclusion
I have created a sample Java Maven project using the Selenium Page factory and Cucumber in GitHub https://github.com/anu-01/CucumberSelenliumPageFactory. This is a great place to get started with a Cucumber-based framework.
Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.
Recent Comments