3

This is a sample code which will login into system

@BeforeClass
public void verifyLogin()
    {
    loginObject.enterUsername("admin");
    loginObject.enterPassword("!212313132");
    loginObject.clickOnsignIn();
    Assert.assertTrue(homeObject.isLoggedIn());
}
Deepanshu
  • 99
  • 3
  • 11

2 Answers2

0

It's more about opinion.

You are doing login in @BeforeClass , it must be validated which you are doing correctly by doing Assert.assertTrue(homeObject.isLoggedIn()); this.

Because if login fails you might not wanna continue your testing.

Please note that @BeforeTest is bigger unit in TestNG as compared to @BeforeClass.

Personally I write login method in @BeforeMethod followed by all @Test methods in a class.

@BeforeClass is a setupClass in which you might want to declare your prerequisite such as driver initialization and all.

So coming to your question , where we should assert and what is good practice.

Wherever validation is required we must assert. It is as simple as that.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

Common design pattern for Selenium is the Give-When-Then design.

Looks like your described test contains the 'Given user X is logged in' therefore 'When..' and 'Then..'. So as the logged in user is a precondition in your test, it makes a lot of sense to assert it.

However, it's not mandatory. You can make 'homeObject.isLoggedIn()' method to throw LoginException in case of not finding a WebElement in the logged in page. In this way you would be capable to create a negative test (wrong password for example) which would use exactly the same code and would expect LoginException.

Community
  • 1
  • 1
AutomatedOwl
  • 1,039
  • 1
  • 8
  • 14