Last Updated On
We may quickly validate the steps in your feature using Dry Run in Cucumber, without having to run the code inside the appropriate step definitions, to ensure that every Step has its corresponding Step Definition present in the Step Definition file. A cucumber dry run is used to confirm the compilation faults and compile the Step Definition and Feature files. It is a way to validate the correctness of the Gherkin syntax in the feature files and the mapping of steps defined in the step definitions without executing the actual application code.
The Dry Run option can either be set as true or false. By default, it is false.
Dry Run as true
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(plugin = {"pretty"},
features = "src/test/resources/Features",
glue = "org.example.stepdefinitions",
monochrome=true,
dryRun = true)
public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
}
Below is the sample feature file.
Feature: Login to HRM Application
@ValidCredentials
Scenario: Login with valid credentials
Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"
When User enters username as "Admin" and password as "admin123"
Then User should be able to login successfully and new page open
In the below snippet of code, I have created the stepdefinitions for the Given and When statements and no stepdefinition for the Then statement.
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import java.time.Duration;
public class LoginPageDefinitions {
private static WebDriver driver;
public final static int TIMEOUT = 5;
@Before
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(TIMEOUT));
}
@Given("User is on HRMLogin page {string}")
public void loginTest(String url) {
driver.get(url);
}
@When("User enters username as {string} and password as {string}")
public void goToHomePage(String userName, String passWord) {
// login to application
driver.findElement(By.name("username")).sendKeys(userName);
driver.findElement(By.name("password")).sendKeys(passWord);
driver.findElement(By.xpath("//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button")).submit();
// go the next page
}
@After
public void teardown() {
driver.quit();
}
}
Execute the feature file by right-clicking on the Runner class and selecting – Run As TestNG Test.
In the case of the dry run as true, none of the Steps is executed, but Cucumber has only made sure that every Step has the interconnected method available in the Step Definition file. Take a look at the time duration at the end of every Step. It is extremely less.
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-62.png?w=1200)
Dry Run as false
In the below case, dryRun is false.
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(plugin = {"pretty"},
features = "src/test/resources/Features",
glue = "org.example.stepdefinitions",
monochrome=true,
dryRun = false)
public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-61.png?w=1200)
In case of dry run as false, the steps 1 and 2 will be executed. The browser will open and all statements inside the step definitions method will execute when set to false. The time taken to complete this test is 0.235 sec.
So, you can set dry run to true to quickly check if any of the step definition is not implemented.