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,320 points
5,828 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,910 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
3,090 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 21, 2018 in Selenium by king_kenny
• 3,710 points
1,365 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
16,293 views
0 votes
2 answers

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,500 points
13,580 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,090 views
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,910 points
7,183 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,843 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,910 points
8,910 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP