|
| 1 | +package testSuit.stepDef; |
| 2 | + |
| 3 | +import io.cucumber.java.After; |
| 4 | +import io.cucumber.java.Before; |
| 5 | +import io.cucumber.java.Scenario; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import testSuit.factories.ReporterFactory; |
| 8 | + |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Abhishek Kadavil |
| 17 | + */ |
| 18 | +@Slf4j |
| 19 | +public class Hooks { |
| 20 | + |
| 21 | + @Before |
| 22 | + public void beforeScenario(Scenario scenario) { |
| 23 | + log.info("beforeScenario {}", scenario.getName()); |
| 24 | + } |
| 25 | + |
| 26 | + @After |
| 27 | + public void afterScenario(Scenario scenario) { |
| 28 | + log.info("afterScenario {}", scenario.getName()); |
| 29 | + |
| 30 | + /* Code to manage @Author and @Category tags */ |
| 31 | + Collection<String> tagsCollection = scenario.getSourceTagNames(); |
| 32 | + Set<String> tags = new HashSet<>(tagsCollection); |
| 33 | + |
| 34 | + tags.forEach(tag -> { |
| 35 | + if (tag.startsWith("@Author")) { |
| 36 | + ReporterFactory.getInstance().getExtentTest().assignAuthor(extractedTagValue(tag)); |
| 37 | + } else if (tag.startsWith("@Category")) { |
| 38 | + ReporterFactory.getInstance().getExtentTest().assignCategory(extractedTagValue(tag)); |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + private String extractedTagValue(String tag) { |
| 44 | + |
| 45 | + Pattern pattern; |
| 46 | + Matcher matcher = null; |
| 47 | + if (tag.startsWith("@Author")) { |
| 48 | + pattern = Pattern.compile("@Author\\(\"([^\"]+)\"\\)"); |
| 49 | + matcher = pattern.matcher(tag); |
| 50 | + } |
| 51 | + if (tag.startsWith("@Category")) { |
| 52 | + pattern = Pattern.compile("@Category\\(\"([^\"]+)\"\\)"); |
| 53 | + matcher = pattern.matcher(tag); |
| 54 | + } |
| 55 | + |
| 56 | + if(matcher != null) |
| 57 | + { |
| 58 | + if (matcher.find()) { |
| 59 | + return matcher.group(1); // Return the author's name |
| 60 | + } |
| 61 | + } |
| 62 | + return "Unknown"; |
| 63 | + } |
| 64 | +} |
0 commit comments