使用除@BeforeClass

时间:2016-05-05 07:14:58

标签: java maven selenium-webdriver testng

基类代码:

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);

    }

注册页码:

protected WebDriver driver;

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

测试用例代码:

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

    signuppage.signupbtn();

}

堆栈追踪:

  

显示java.lang.NullPointerException           在Testcases.SignUpTest.Signup_Disabled_Check(SignUpTest.java:27)   有关各个测试结果,请参阅C:\ Users \ mike \ IdeaProjects \ web \ target \ surefire-reports。           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)           在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)           在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)           在org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)           在org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)           在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)           在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)           在org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)           在org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)           在org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)           在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)           在java.lang.reflect.Method.invoke(Method.java:497)           在org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)           在org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)           在org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)           在org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)   引起:org.apache.maven.plugin.MojoFailureException:有测试失败。

当我@BeforeClass使用setup()时,它运行正常。但是如果尝试使用@BeforeSuite@BeforeGroups,我会得到空指针异常。

我使用此命令运行它:

mvn test -Dgroups=SignupButton

2 个答案:

答案 0 :(得分:0)

对于@BeforeGroups,您应首先定义一个组,然后为该注释指定名称。这就是你用这个注释获得异常的原因。

@BeforeGroup(groups={"SignupButton"})

答案 1 :(得分:0)

问题在于您的SignupPage类。它应该有一个no-args或默认构造函数。或者更好的实施方式如下:

signuppage = new Signup(driver);

然后在Signup类中创建一个构造函数,如下所示:

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

编辑:在调试模式下运行代码。在该类的第27行添加调试点,看它是否为null。如果是这样的话......然后在初始化程序块中添加调试点,看看控制何时进入,那应该告诉你流程。