简体   繁体   English

尝试使用PageFactory运行我的脚本时出现“ NullPointerException”

[英]“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. 您的代码有几个问题。

  • You are instantiating your webdriver in a @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注释的方法不会第二次执行。
  • You are resorting to using implicit timeouts. 您正在使用隐式超时。 Please don't use implicit timeouts. 请不要使用隐式超时。 You can read more about the evils of implicit waits in this SO post. 您可以在这篇 SO文章中了解有关隐式等待的弊端的更多信息。

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.

相关问题 NullPointerException 使用 PageFactory - NullPointerException uising PageFactory 使用PageFactory和Page Object通过Selenium调用SendKeys时发生NullpointerException - NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object 尝试从 testng.xml 运行我的测试时出现 NullPointerException - NullPointerException while trying to run my tests from testng.xml 尝试单击存在的并使用PageFactory.initElements()初始化的按钮时出现NullPointerException - I get NullPointerException when trying to click button that exists and initialized with PageFactory.initElements() 尝试使用groovy脚本运行程序在JIRA中删除时出现NullPointerException - NullPointerException while trying to delete in JIRA using groovy script runner 在测试类中使用PageFactory POM中的WebElements - Using WebElements from PageFactory POMs in my test class Selenium java.lang.NullPointerException与PageFactory - Selenium java.lang.NullPointerException with PageFactory 在 PageFactory (Selenium+Java) 中获取 NPE NullPointerException - Getting NPE NullPointerException in PageFactory (Selenium+Java) 使用PageFactory的Page类 - Page Class using PageFactory 尝试运行简单的“Hello!”示例脚本时,SBT会给出java.lang.NullPointerException - SBT gives java.lang.NullPointerException when trying to run simple “Hello!” example script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM