In short when i run this commend mvn clean test -D"cucumber.filter.tags=@Test1"
after i run mvn clean test -D"cucumber.filter.tags=@Test2"
command i want i can use the variable created in the previous method in the other method. For example;
@Test1
Scenario: Create Folder
When I create folder with "test" name api
This scenerio linked this method
@When("^I create folder with \"([^\"]*)\" name api$")
public void createFolder(String name) {
response = RestAssured.given()
.baseUri(prp_url)
.accept("application/json")
.contentType("application/json")
.header("X-Auth-Token", xAuthToken.userAuth())
.header("Folder-Name", name)
.header("Folder-Uuid", "123456")
.queryParam("parentFolderUuid", "")
.when()
.post("/filesystem/createFolder")
.then().extract().response();
getUuid = getJsonPath(response, "uuid");
}
And here is my second scenerio
@Test2
Scenario: Create inside the Folder
When I send create inside the folder with "1" name api
```
And this scenerio is also linked this method
```
@When("^I send create inside the folder with \"([^\"]*)\" name api$")
public void createInsideFolder(String name) {
String uuid = getUuid();
response = RestAssured.given()
.baseUri(prp_url)
.accept("application/json")
.contentType("application/json")
.header("X-Auth-Token", xAuthToken.userAuth())
.header("Folder-Name", name)
.header("Folder-Uuid", "123456")
.queryParam("parentFolderUuid", getUuid)
.when()
.post("/filesystem/createFolder")
.then()
.extract().response();
}
```
So when I use the same tag, I can use the variable created in the previous method in the other method, but when I run each scenario with different tags, I get a null pointer error. How can I run 2 scenarios separately and use the variable in the previous method in the other method?
Can you do this? Yes, you can.
Should you do this? Absolutely not! Unless, you are creating a Background to do this.
Under no circumstance, you should create a scenario that depends on another scenario. Scenarios are meant to be run independently and in no specific order. Creating a dependency such as the one you described here is counterproductive. But, against my better judgment, I am going to show you how to do it...
Create a global variable!
public class MyFeatureClass {
private String folderName = "";
@When("^I create folder with \"([^\"]*)\" name api$")
public void createFolder(String name) {
response = RestAssured.given()
.baseUri(prp_url)
.accept("application/json")
.contentType("application/json")
.header("X-Auth-Token", xAuthToken.userAuth())
.header("Folder-Name", name)
.header("Folder-Uuid", "123456")
.queryParam("parentFolderUuid", "")
.when()
.post("/filesystem/createFolder")
.then().extract().response();
getUuid = getJsonPath(response, "uuid");
folderName = // whatever;
}
@When("^I send create inside the folder with \"([^\"]*)\" name api$")
public void createInsideFolder(String name) {
// Use "folderName" here
String uuid = getUuid();
response = RestAssured.given()
.baseUri(prp_url)
.accept("application/json")
.contentType("application/json")
.header("X-Auth-Token", xAuthToken.userAuth())
.header("Folder-Name", name)
.header("Folder-Uuid", "123456")
.queryParam("parentFolderUuid", getUuid)
.when()
.post("/filesystem/createFolder")
.then()
.extract().response();
}
}
A better approach is to use a Background
. A background in Cucumber is very much the same as what @Before
annotation is for JUnit or TestNG. A background step definition is a chunk of code that runs automatically before each scenario (whether a particular scenario needs it or not). This is the correct place to resolve all the dependencies a scenario might need to execute correctly. In the cucumber file, add a background after the declaration of the feature, like this:
Feature: Test Background Feature
Description: The purpose of this feature is to test the Background keyword
Background: Files and Folders are created
When I create folder with "XYZ" name api
Scenario: Create subfolder
Given Open my app
When I send create inside the folder with "1" name api
Then I can create files inside folder
To access this variable, do the same as I proposed: declare it as global. When you add scenarios to this feature that require this attribute, they will be able to access it automatically.
If you are required to use a variable in between steps, you should be able to use the same strategy.