[英]“NullPointerException” on trying to run my script using PageFactory
I have attached POM, BaseTest and Test classes. 我已经附上了POM,BaseTest和Test类。 Im getting NullPointerException for the below code on trying to run it as TestNG test by right clicking on the project. 我试图通过右键单击项目以将其作为TestNG测试运行,从而获得以下代码的NullPointerException。 Please could suggest? 请可以建议吗?
POM Class: 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 Class: 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();
}
}
Test Class: 测试类别:
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();
}
}
Your code has a few issues. 您的代码有几个问题。
@BeforeSuite
. 您正在@BeforeSuite
中实例化@BeforeSuite
。 This causes your webdriver instance to be created ONLY once per <suite>
tag. 这将导致每个<suite>
标签仅创建一次您的webdriver实例。 So all other @Test
methods will always get a NullPointerException
because the @BeforeSuite
annotated method doesn't get executed the second time. 因此,所有其他@Test
方法将始终获得NullPointerException
因为@BeforeSuite
注释的方法不会第二次执行。 So to get started, I would suggest that change your test code to something like below 因此,一开始,我建议您将测试代码更改为如下所示
BaseTest.java 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 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();
}
}
Here what we have done is chose to use @BeforeMethod
and @AfterMethod
for instantiation and cleanup of webdriver, because these methods are guaranteed to be executed before and after every @Test
method. 在这里,我们选择使用@BeforeMethod
和@AfterMethod
进行@AfterMethod
的实例化和清理,因为可以保证这些方法在每个@Test
方法之前和之后执行。 We then went on to using ThreadLocal
variants of Webdriver
because ThreadLocal ensures that every thread gets its own copy of webdriver, so that you can easily start running your tests in parallel. 然后,我们继续使用Webdriver
ThreadLocal
变体,因为ThreadLocal确保每个线程都拥有自己的webdriver副本,以便您可以轻松地开始并行运行测试。 This is right now not a problem, but very soon you will face this issue as you start building upon your implementation. 现在这不是问题,但是很快您将在开始构建实现时遇到这个问题。 You can read more about how to resort to parallel executions using TestNG by reading this blog post of mine. 通过阅读我的这篇博客文章,您可以了解有关如何使用TestNG进行并行执行的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.