Hooks(Tagged-Example)

@Par5 @setcookies

Scenario: Multiple Filters in Advanced Search Page

Given I am in Ebay Shopping Homepage

When I advanced search on items with multiple filters

| keyword | Exclude | min | max |

| iphone 11 | refurbished | 300 | 900 |

| iphone 13 | refurbished | 500 | 1000 |

| Samusung S24 | refurbished | 1000 | 5000 |

@Par6 @setcookies @test

Scenario Outline: Home Page Links

Given I am in Ebay Shopping Homepage

When I click on '<link>'

Then I validated the page navigates to '<url>' and titie is '<title>'


Examples:

| link | url | title | |

| Deals | https://www.ebay.com/globaldeals | Daily Deals on eBay | Best deals and Free Shipping |

| Fashion | https://www.ebay.com/b/Fashion/bn_7000259856 | Fashion | eBay |

| Sports | https://www.ebay.com/b/Sporting-Goods/888/bn_1865031 | Sporting Goods | eBay |

============================================


@RunWith(Cucumber.class)

@CucumberOptions(

features = {"src/test/java/Features"},

glue = {"Steps"},

tags = "@Par5 or @Par6", // Runs only scenarios tagged with @Par5 or @Par6

monochrome = true

)

public class ParameterRunner {

}


Tagged with @Par6, @setcookies, and @test — triggers multiple hooks

Tagged with @Par5 (used for test selection) and @setcookies (used for scenario-specific behavior)

================================


package Steps;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.After;

import io.cucumber.java.Before;


public class HookSteps {


private WebDriver driver;


@Before //Runs before every scenario (regardless of tag)

public void setup() {

driver = new ChromeDriver();

driver.manage().window().maximize();

System.out.println("global BEFORE got executed");

}

@Before("@setcookies") //Only runs for scenarios tagged with @setcookies.

public void setcookies() {

System.out.println("Scenario specific hook - setcookies got executed");

}

@After //Runs after every scenario, regardless of tags.

public void teardown() throws InterruptedException {

driver.quit();

System.out.println("global AFTER got executed");

Thread.sleep(2000);

}


@After("@test") //Only runs after scenarios tagged with @test.

public void afterHook() throws InterruptedException {

System.out.println("Scenario specific hook - test got executed");

}

public WebDriver getDriver() {

return driver;

}

}


====================================


global BEFORE got executed

Scenario specific hook - setcookies got executed

global AFTER got executed



global BEFORE got executed

Scenario specific hook - setcookies got executed

Scenario specific hook - test got executed

global AFTER got executed