Error: TestNG @BeforeClass initialization code not running before Test

0 votes

I am trying to set some elements on the @BeforeClass section but. But I am getting null pointer exception.

public class Error: TestNG @BeforeClass initialization code not running before Test

RunTest {

private static WebElement user;

private static WebElement pass;

private static WebElement login;


@BeforeClass

public static void driveraBaglan() {

    //Driver is declared here. I removed it to give the simple code.

    user = driver.findElement(By.id("user"));

    pass = driver.findElement(By.id("pass"));

    login = driver.findElement(By.xpath("//button[contains(.,'Log In')]"));

    statusMessage = driver.findElement(By.id("login-status-message")).getText();


}

@Test(priority=1)

public void loginNoInfo() {

        user.clear();

        pass.clear();

}

I get the null pointer error on user.clear() , pass.clear() and login.click() functions. Please help to resolve it.

Jul 6, 2018 in Selenium by Martin
• 4,220 points
767 views

1 answer to this question.

0 votes

Below will help:

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked

Change the annotation

@BeforeTest

public static void driveraBaglan() {

    //Driver is declared here. I removed it to give the simple code. 

    user = driver.findElement(By.id("user"));

    pass = driver.findElement(By.id("pass"));

    login = driver.findElement(By.xpath("//button[contains(.,'Log In')]"));

    statusMessage = driver.findElement(By.id("login-status-message")).getText();

}
answered Jul 6, 2018 by Samarpit
• 5,130 points

Related Questions In Selenium

0 votes
1 answer

Test case not running on TestNG

Hello @Sradha, You are not getting any ...READ MORE

answered Dec 26, 2018 in Selenium by Trisha
66 views
0 votes
1 answer

Error: FF Browser not working for Selenium test after update

Since Firefox's latest update to version 47.0, ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,610 points
84 views
0 votes
1 answer

Getting error while running a XML file in TestNG

Hello @Sradha, The synatx of using test is ...READ MORE

answered Dec 26, 2018 in Selenium by Rashmi
473 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
420 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
776 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
413 views
0 votes
1 answer

Selenium-Debugging Error: Element is not clickable at point (X,Y)

Another element is covering the element you ...READ MORE

answered Jul 2, 2018 in Selenium by Samarpit
• 5,130 points
2,076 views
0 votes
1 answer