2017-02-12 104 views
0

我附加了POM,BaseTest和Test类。我为下面的代码尝试通过右键单击项目来运行它作为TestNG测试而获得NullPointerException。请提出建议?尝试使用PageFactory运行我的脚本时出现“NullPointerException”

POM类:

package pom; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.PageFactory; 

public class Introduction 
{ 

@FindBy(xpath="//a[text()='Hello. Sign In']") 
WebElement signInLink; 

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

public void signIn() 
{ 
    signInLink.click(); 
} 
} 

BaseTest类:

package scripts; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.*; 


public class BaseTest 
{ 
public WebDriver driver; 

@BeforeSuite 
public void preCondition() 
{ 
    driver= new FirefoxDriver(); 
    driver.get("https://www.walmart.com/"); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
} 

@AfterSuite 
public void postCondition() 
{ 
    driver.close(); 
} 
} 

测试类:

package scripts; 

import org.testng.annotations.Test; 

import pom.Introduction; 

public class SignIn extends BaseTest 
{ 

@Test 

public void validSignIn() 
{ 
    Introduction i= new Introduction(driver); 
    i.signIn(); 
} 
} 
+0

尝试增加超时?你看到正确加载的页面吗? – liquide

+0

你能分享异常跟踪吗? – Mahipal

回答

0

代码有几个问题。

  • 您正在初始化您的webdriver在@BeforeSuite。这会导致您的webdriver实例仅根据<suite>标记创建一次。所以所有其他@Test方法将始终得到NullPointerException,因为@BeforeSuite注释的方法不会再次执行。
  • 您正在诉诸使用隐式超时。请不要使用隐式超时。您可以在this SO帖子中阅读更多关于隐性等待的祸害。

所以上手,我建议改变你的测试代码类似下面

BaseTest.java

package scripts; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.*; 

public class BaseTest { 
    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>(); 

    @BeforeMethod 
    public void preCondition() { 
     driver.set(new FirefoxDriver()); 
     driver.get().get("https://www.walmart.com/"); 
    } 

    @AfterMethod 
    public void postCondition() { 
     driver.get().quit(); 
    } 

    public final WebDriver driver() { 
     return driver.get(); 
    } 
} 

SignIn.java

package scripts; 

import org.testng.annotations.Test; 

import pom.Introduction; 

public class SignIn extends BaseTest { 

@Test 
public void validSignIn() { 
    Introduction i = new Introduction(driver()); 
    i.signIn(); 
} 
} 

这里我们所做的是选择使用@BeforeMethod@AfterMethod用于实例化和清理webdriver,因为这些方法保证在每个@Test方法之前和之后执行。然后,我们继续使用ThreadLocalWebdriver的变体,因为ThreadLocal确保每个线程都获得自己的webdriver副本,以便您可以轻松地开始并行运行测试。这现在不是问题,但是当你开始构建你的实现时,你很快就会面临这个问题。您可以通过阅读我的this blog文章了解更多关于如何使用TestNG进行并行执行的信息。

相关问题