The 5th Chapter cucumber Tags

Source: Internet
Author: User
5.1 Cucumber Tags

It looks very simple when we have only one, two, or possibly five scenes in a feature file. However, it will not happen in real life. For each of the tested features, we may have 10, 20 or more scenarios in a single attribute file. They may represent different purposes (smoke testing/Regression testing), different perspectives (Developer/QA/BA), different states (ready for execution/in progress), etc.

to do this, cucumber has provided a way to organize the execution of a scene by using the tags in the signature file. You can define each scene with a useful tag. Later, in the runner file, you can decide which particular label (and the scene) you want Cucumber to execute. The label begins with "@". After "@", you can use any related text to define your code. Use an example to understand this. 5.2 Cucumber Tags operation example

assume that there are two or more scenarios in the feature file. But just want to execute a scene as part of the smoke test. Therefore, the first thing is to identify the scene, followed by marking it with the "@SmokeTest" text at the beginning of the scene.

Write the following text in the file and save it. This feature file contains two scenarios, only one of which is marked as smoketest.

step_1: Create a Cucumbertag package under the/src/test/java path.

step_2: Create cucumbertag.feature under the cucumber package

Feature:cucumber Tag 
Scenario outline:login functionality for a social networking site. 
Given user navigates to Csdn if 
I enter Username as "<username>" and Password as "<password>" then 	
l Ogin should be unsuccessful 
Examples: 
|username |password | 
| UserName1 |password1 | 
| UserName2 |password2 |
	
#following scenario have been tagged as smoketest and this should get executed. 
@SmokeTest 
Scenario: 
Given user navigates to Csdn when
I enter Username as "<>" and Password as "< > then the 
user should is redirected to login retry

step_3: Create a step definition file under the cucumber package Cucumbertag.java

Package Cucumbertag; 
Import Org.openqa.selenium.By; 
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.chrome.ChromeDriver; 
Import Cucumber.annotation.en.Given; 
Import Cucumber.annotation.en.Then; 
Import Cucumber.annotation.en.When; 
	public class Cucumbertag {Webdriver driver = null; 
		@Given ("^user navigates to csdn$") public void Gotocsdn () {driver = new chromedriver ();
		Driver.navigate (). to ("Https://passport.csdn.net/account/login?ref=toolbar"); } @When ("^i enter Username as \" ([^\ "]*) \" and Password as \ "([^\"]*) \ "$") public void I_ENTER_USERNAME_AS_AND_PASSW 
		Ord_as (String arg1, String arg2) {driver.findelement (By.id ("username")). SendKeys (ARG1); 
		Driver.findelement (by.id ("password")). SendKeys (ARG2); 
		Driver.findelement (By.classname ("Logging")). Click (); } @Then ("^login should is unsuccessful$") public void Validaterelogin () {if (Driver.getcurrenturl (). Equalsignorec ASE ("HTTP://MY.CSDN.NET/MY/MYCSDN")) {System.out.println ("TesT Pass ");
			} else {System.out.println ("Test Failed"); 
		} driver.close (); } @Then ("^the user should is redirected to login retry$") public void Loginretry () {if (Driver.getcurrenturl (). equ
			Alsignorecase ("Http://my.csdn.net/my/mycsdn")) {System.out.println ("Test Pass"); 
			} else {System.out.println ("Test Failed"); 		
	} driver.close (); }
}

Step_4: Create a Runner class file under the cucumber package, Runtest.java

Package Cucumbertag; 
Import Org.junit.runner.RunWith; 
Import Cucumber.junit.Cucumber; 
@RunWith (Cucumber.class) 
@Cucumber. Options (format={"Pretty", "Html:target/cucumber"}) public 
class runtest {
	
}

step_5: The option to run test: Go to the left-hand package Browse, select Runtest.java, right-click to select "Run as" and select "JUnit Test" in the popup box. The results of the operation are as follows:

        

There are two main types of tags:

Ødefault Tag:default tag needs to be pre-defined. For example: @Dev, @Ignore

Øcustom tag:custom tag is flexible, users can choose the appropriate text to define their own tag

tag can also be defined at the feature level. After the tag is defined at the feature level, it will ensure that all scenes in the feature file inherit the tag. Depending on the nature of the scene, multiple tags can be used for a single feature. Each time cucumber finds the appropriate call, it executes a particular scene.

Cucumber also provides a way to reverse-select the tag. Imagine that in 25 defined scenarios, 10 are marked as SmokeTest. We only need to perform regression test scenarios.

To do this, we can use "~" in the JUnit runner class to exclude the smoke test scenario . It will be as follows:

Package Cucumbertag; 
Import Org.junit.runner.RunWith; 
Import Cucumber.junit.Cucumber; 
@RunWith (Cucumber.class) 
@Cucumber. Options (format={"Pretty", "Html:target/cucumber"},tags={"~ @SmokeTest"}) Public 
class Runtest {
	
}

When defining multiple tags, we can also define logical OR and logical and operations.

Ø define logical OR in the Runner class - @ Dev, @wip: It says that matches the scene that contains any of the tags that need to be executed.

ø  defines the logical and in the Runner class - [@ dev,〜@ WIP] : It says that matches the scene that contains the two tags that need to be executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.