RUVIDEO
Поделитесь видео 🙏

How To: Hooks In Cucumber Using Java (3 Min)

In this tutorial, you'll learn how to use hooks in Cucumber using Java.

Facebook: https://www.facebook.com/GokceDBsql

Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to use hooks in cucumber using Java. Let's start by looking at the test directory structure under the Java folder. I have a hello cucumber package with a bunch of Java classes under the resources folder.

I have a hello cucumber package with one feature file. Now, let's look at the feature file. On line two, I'm defining the feature and on line three, I have a description of the feature. On line six, I'm defining a scenario outline with three steps, given, when, and then. On line 11, I'm defining three examples with their expected output.

Now let's look at the steps definition class. On line 64, I'm defining the given step. On line 70, I'm defining the at when step and on line 76 I'm defining the at then step.

Next, to execute this test right click on the feature file and hit run. As you can see all three scenarios and nine steps passed as expected. Now, Let's look at the at before hook in the step definitions file before hooks.

Run before the first step of each scenario and whatever happens in a before the hook is invisible to people who only read the feature files. Therefore, you should only use a before hook for low-level logic such as starting a browser or deleting data from a database. If I re-execute the feature file, you'll see the executing low-level logic line before the first step of each scenario. Similarly, hooks run after the last step of each scenario.

Now, let's talk about the step hooks. Step hooks are invoked before and after the step. If a before step, the hook is executed then the after step.

The hook will also be executed regardless of the result of the step. Next, let's look at conditional hooks that can be conditionally selected for execution based on the tags of the scenario. Here, I have a conditional after hook based on the smoke tag.

Finally, there are global hooks such as at before all which will run before any scenario is run, and at after all which will run after all scenarios have been executed. There you have it. Make sure you like, subscribe, and turn on the notification bell.

Until next time.
package hellocucumber;
import io.cucumber.java.*;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.junit.jupiter.api.Assertions.*;
public class StepDefinitions {
private String today;
private String actualAnswer;
@Before
// Before hooks run before the first step of each scenario
// Whatever happens in a Before hook is invisible to people who only read the features
// Only use a Before hook for low-level logic such as starting a browser/deleting data from a db etc.
public void doSomethingBefore() {
System.out.println("Executing low-level logic in Before hook");
}
@After
// After hooks run after the last step of each scenario, even when the step result
// is failed, undefined, pending, or skipped.
public void doSomethingAfter(){
System.out.println("In After hook");
}
// Step Hooks
@BeforeStep
// Step hooks are invoked before and after a step
// Meaning that if a BeforeStep hook is executed the AfterStep hooks will also be executed
// regardless of the result of the step. If a step did not pass, the following step and
// its hooks will be skipped.
public void doSomethingBeforeStep(){
System.out.println("In Before Step hook");
}

@AfterStep
// Executed after a step
public void doSomethingAfterStep(){
System.out.println("In After Step hook");
}

// Conditional hooks
@After("@smoke")
// Hooks can be conditionally selected for execution based on the tags of the scenario.
public void doSomethingConditionalAfter(){
System.out.println("In Conditional After hook");
}
// Global Hooks
@BeforeAll
// BeforeAll run before any scenario is run
public static void beforeAll() {
// Runs before all scenarios
}
@AfterAll
// AfterAll run after all scenarios have been executed.
public static void afterAll() {
// Runs after all scenarios
}
@Given("today is {string}")
public void today_is(String today) {
this.today = today;
System.out.println("--@Given Step");
}
@When("I ask whether it's Friday yet")
public void i_ask_whether_it_s_Friday_yet() {
actualAnswer = IsItFriday.isItFriday(today);
System.out.println("--@When Step");
}
@Then("I should be told {string}")
public void i_should_be_told(String expectedAnswer) {
assertEquals(expectedAnswer, actualAnswer);
System.out.println("--@Then Step");

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «How To: Hooks In Cucumber Using Java (3 Min)», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.