Can an if-else statement be incorporated into the Cucumber test steps for a scenario that did not originally include one?

Here's the situation I am facing.

In the past, the system under test (SUT) had just one button labeled "refund" for offers. Due to legal reasons, this button is now only accessible 20 days after the offer creation. Once this time period has passed, the refund button is replaced with a "cancel" button. Additionally, there used to be a cancel button next to the refund button which has since been removed. Now there is only one button, initially "refund" and later changed to "cancel" after 20 days.

We currently have two scenarios we are working with:

Scenario: Validating user ability to refund an offer
    Given I log into the system if not already logged in
    Given I navigate to the back office page of the system
    And I click on the offers tab in the system
    And I go to the last active offer page 
    And I should see the offer page
    And I should see the status as "Paid" for the latest offer in the system
    When I initiate a refund for the offer
    Then I should see the status change to "Open" for the latest offer in the system

Scenario: Ensuring users can successfully cancel an offer
    Given I access the offer page
    And I click on the offers tab in the system
    And I visit the last active offer page 
    And I see the offer page in the system
    And I confirm the status as "Open" for the latest offer in the system
    When I select the option to cancel the offer within the system
    And I encounter the cancellation reason page in the system
    And choose a random reason for the most recent offer in the system
    And I return to the system offer page
    Then I verify that the status is now "Cancelled" for the latest offer in the system

I am seeking advice on how to address the current issue, where tests fail intermittently due to the visibility of either the refund or cancel button depending on the offer. What would be the best approach to resolve this discrepancy?

Answer №1

Unfortunately, Gherkin does not have built-in support for if-statements, leaving it up to testers to develop a testable automated system. This seems to be a current limitation in its functionality.

Addressing this issue may require organizational changes, as test automation presents engineering challenges that may surpass individual capabilities. However, such discussions are better suited for platforms beyond StackOverflow.

In the meantime, one workaround is to skip a scenario when specific preconditions are not met.

To verify these pre-conditions, an additional step can be added. While typically used for creating refundable offers, here we focus solely on verification.

  Scenario: Verify that a user can refund an offer
   Given a refundable offer
...

Scenario: Verify that a user is able to cancel an offer
    Given a non-refundable offer
...

Cucumber offers support for OpenTest4Js TestAbortedException, enabling the use of JUnit Jupiter's Assumptions to abort scenarios instead of failing them.

import org.junit.jupiter.api.Assumptions;

@Given("a refundable offer")
public void verify_offer_is_refundable(){
    boolean condition = // decide if tests should abort because the offer is not refundable 
    Assumptions.assumeTrue(condition, "Offer was not refundable");
}

A similar approach can also be implemented using JUnit 4's Assumption class and TestNG's SkipException.

While having skipped tests may impact report aesthetics,

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The sendKeys function in Selenium seems to be missing some characters when inputting text

Currently, I am working on test automation using Java, Selenium, and Chrome. Our developers recently made an upgrade from AngularJS to Angular2 for our UI. Ever since the upgrade, I've been facing an issue where sendKeys is inputting incomplete charac ...

Unable to locate element in the DOM using XPath

Having trouble retrieving an element from the DOM using XPath <input bsdatepicker="" class="form-control font-size-normal ng-untouched ng-pristine ng-valid" id="endDate" name="endDate" placement="top" type="text"> The value of the element is not p ...