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.