2
votes

Base Class Code :

protected WebDriver driver;
protected String URL = "https://www.example.com/";
public Signup signuppage;   

@BeforeGroups
 public void setup()
    {    
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.navigate().to(URL);
        signuppage = PageFactory.initElements(driver, Signup.class);

    }

Signup Page Code :

protected WebDriver driver;

    public Signup(WebDriver driver) {
        this.driver = driver;
    }

Testcase Code :

@Test(groups="SignupButton")
public void Signup_Disabled_Check() {

    signuppage.signupbtn();

}

Stack Trace :

java.lang.NullPointerException at Testcases.SignUpTest.Signup_Disabled_Check(SignUpTest.java:27) Please refer to C:\Users\mike\IdeaProjects\web\target\surefire-reports for the individual test results. at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286) at org.apache.maven.cli.MavenCli.main(MavenCli.java:197) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

When I use @BeforeClass for setup(), it works fine. But If try to use @BeforeSuite, @BeforeGroups, I am getting null pointer exception.

I run it using this command:

mvn test -Dgroups=SignupButton
2
Don't you need to give it a group name? e.g. @BeforeGroups("database") - Minh Kieu
It does not understand it as common for any group? - Helping Hands
Based on documentation, mkyong.com/unittest/testng-groups-example - you need to give it a group name. Try it out. - Minh Kieu

2 Answers

0
votes

for @BeforeGroups you should first define a group and then give the annotation the name. That's why you are getting exception with this annotation.

@BeforeGroup(groups={"SignupButton"})
0
votes

The issue is with your SignupPage class. It should have a no-args or default constructor. Or better way of implementing is something as belows:

signuppage = new Signup(driver);

And then in Signup class create a constructor as below:

public Signup(WebDriver driver)
{
     PageFactory.initElements(driver, this);
}

EDIT: Run your code in debug mode. add debug point on line 27 of that class and see if its null. if so..then add debug point in the initializer block and see if when does control goes there and that should tell you the flow.