如何使用selenium webdriver和页面对象模型解决java.lang.NullPointerException

如何使用selenium webdriver和页面对象模型解决java.lang.NullPointerException,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,我使用页面对象模型使用Testng运行这些测试。但我有一个空点异常。 我已经实例化了webdriver。但我不确定我这样做是否正确 FindObjectPage-元素页面 package Pages; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import o

我使用页面对象模型使用Testng运行这些测试。但我有一个空点异常。 我已经实例化了webdriver。但我不确定我这样做是否正确

FindObjectPage-元素页面

package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

public class Flash_Alerts_Elements {

    WebDriver driver;

    public void Search_alert_Title(){
        driver.findElement(By.cssSelector(".d-flex.justify-content-between.my-3 > div > a:nth-of-type(1)")).click();
        System.out.println("Pass Alert search");

       WebElement element = driver.findElement(By.xpath("//div[@id='app']/div[@class='app-body']/div[@class='container-fluid']/div[@class='mb-3 px-3']//h1[@class='tw-my-auto']"));
        if (element.getText().equals("Add Flash Alert"))
        System.out.println("Match found -Form heading - Add Flash Alert");
        else
        System.out.println("Match Not found");
        Assert.assertEquals(element.getText(), "Add Flash Alert");

    }

    public void Add_Alert_Title_Country(String Country_or_region){
        driver.findElement(By.cssSelector("div:nth-of-type(2) > .form-control.w-100")).sendKeys(Country_or_region);

    }
    public void Add_Alert_Title_Event(String Event){
        driver.findElement(By.cssSelector("div:nth-of-type(3) > .form-control.w-100")).sendKeys(Event);

    }

    public void Add_Alert_Title_Date(String Date){
        driver.findElement(By.cssSelector("div:nth-of-type(4) > .form-control.w-100")).sendKeys(Date);
    }

    public void Add_Alert_Title_Specific_Area(String Area){
        driver.findElement(By.cssSelector("div:nth-of-type(5) > .form-control.w-100")).sendKeys(Area);
    }

    public void Select_Severity(){
        driver.findElement(By.xpath("//div[@id='severity']//span[.='Informational']"));
    }

    public Flash_Alerts_Elements(WebDriver driver) {
        this.driver = driver;
    }
}
package Tests;
import Pages.Dashboard_Elements;
import Utilities.DriverFactory;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import Pages.Flash_Alerts_Elements;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class Test_Flash_Alerts {


    public WebDriver driver;

    @BeforeClass
    public static void loginTestWithDashboardTests() throws InterruptedException {
        ///Initialize diver

        WebDriver driver = DriverFactory.open("chrome");///note this can be changed to Firefox or IE to pom.xml different browsers, ensure you remove the comments from the IE data in the utilities package
        driver.get("https://wip.devbox.red24.com/login-as/slenkers");
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
    }

    @Test
    public void Capture_Flash_alert_from() {
        ///Testing Flash form
        Flash_Alerts_Elements Create_Flash_alert = new Flash_Alerts_Elements(driver);
        Create_Flash_alert.Search_alert_Title();
        Create_Flash_alert.Add_Alert_Title_Country("South Africa");
        Create_Flash_alert.Add_Alert_Title_Event("Test");
        Create_Flash_alert.Add_Alert_Title_Date("15 Sep");
        Create_Flash_alert.Add_Alert_Title_Specific_Area("CPT");

    }

}
测试页面

package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;

public class Flash_Alerts_Elements {

    WebDriver driver;

    public void Search_alert_Title(){
        driver.findElement(By.cssSelector(".d-flex.justify-content-between.my-3 > div > a:nth-of-type(1)")).click();
        System.out.println("Pass Alert search");

       WebElement element = driver.findElement(By.xpath("//div[@id='app']/div[@class='app-body']/div[@class='container-fluid']/div[@class='mb-3 px-3']//h1[@class='tw-my-auto']"));
        if (element.getText().equals("Add Flash Alert"))
        System.out.println("Match found -Form heading - Add Flash Alert");
        else
        System.out.println("Match Not found");
        Assert.assertEquals(element.getText(), "Add Flash Alert");

    }

    public void Add_Alert_Title_Country(String Country_or_region){
        driver.findElement(By.cssSelector("div:nth-of-type(2) > .form-control.w-100")).sendKeys(Country_or_region);

    }
    public void Add_Alert_Title_Event(String Event){
        driver.findElement(By.cssSelector("div:nth-of-type(3) > .form-control.w-100")).sendKeys(Event);

    }

    public void Add_Alert_Title_Date(String Date){
        driver.findElement(By.cssSelector("div:nth-of-type(4) > .form-control.w-100")).sendKeys(Date);
    }

    public void Add_Alert_Title_Specific_Area(String Area){
        driver.findElement(By.cssSelector("div:nth-of-type(5) > .form-control.w-100")).sendKeys(Area);
    }

    public void Select_Severity(){
        driver.findElement(By.xpath("//div[@id='severity']//span[.='Informational']"));
    }

    public Flash_Alerts_Elements(WebDriver driver) {
        this.driver = driver;
    }
}
package Tests;
import Pages.Dashboard_Elements;
import Utilities.DriverFactory;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import Pages.Flash_Alerts_Elements;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class Test_Flash_Alerts {


    public WebDriver driver;

    @BeforeClass
    public static void loginTestWithDashboardTests() throws InterruptedException {
        ///Initialize diver

        WebDriver driver = DriverFactory.open("chrome");///note this can be changed to Firefox or IE to pom.xml different browsers, ensure you remove the comments from the IE data in the utilities package
        driver.get("https://wip.devbox.red24.com/login-as/slenkers");
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
    }

    @Test
    public void Capture_Flash_alert_from() {
        ///Testing Flash form
        Flash_Alerts_Elements Create_Flash_alert = new Flash_Alerts_Elements(driver);
        Create_Flash_alert.Search_alert_Title();
        Create_Flash_alert.Add_Alert_Title_Country("South Africa");
        Create_Flash_alert.Add_Alert_Title_Event("Test");
        Create_Flash_alert.Add_Alert_Title_Date("15 Sep");
        Create_Flash_alert.Add_Alert_Title_Specific_Area("CPT");

    }

}
我已经添加了webdriver,并在Flash\u alert\u元素页面中对其进行了实例化。我还认为应该将此添加到test_flash_警报流中。 如何解决此问题。

尝试使用:

WebDriver driver = new ChromeDriver();

这是我一直实例化WebDriver的方式。

我应该在测试页面或元素页面中添加它吗?我已经在这两个页面中尝试过了,但仍然出现NullpointerException错误。您应该在最初实例化它的地方实例化它。我以前在测试中听说过这个问题。尝试将注释从BeforeClass更改为BeforeTest。