Search code examples
seleniumjunitbddthucydidesserenity-bdd

Serenity BDD with jUnit how to inject steps into setup method?


I am working on a test framework which is using Serenity, Selenium and jUnit. I have some tests which depend on user authentication. Basically I have to repeat all login steps for each test. I wanted to move these steps to a @Before method, but it seems that Steps are not being initialized in a method which is not annotated as @Test... See the code snippet below, AuthSteps instance is not being initialized.

Which are my options?

@RunWith(SerenityRunner.class)
public class MyTests extends AbstractTest {
@Managed(driver = "firefox", uniqueSession = false)
@Steps
AuthSteps auth;

@Before
public void authSetup() {
 if (!authenticated){
  auth.login();
  //a lot of other things
 }
}

@Test
public void mytest(){
 //do test related stuff
}

Solution

  • They do. Steps will run with either @BeforeClass, @Before, @Test and so on. It seems that your if (!authenticated) statement might be excluding execution of your auth.login() step.

    There's certainly not enough code provided here (like what is boolean authenticated)to clearly examine your issue, but I hope this answer helps you.