简体   繁体   中英

How to resolve java.lang.NullPointerException, with selenium webdriver along with page object model

I am running these test with Testng using page object model. But I get a NullPointerException. I have instantiated the webdriver. But I am not sure if it is correct the way I have done it.

FindObjectPage - Element page

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

Test page

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

    }

}

I have added the webdriver and instantiate it in the Flash_alert_element page. Also thought should I add this in the test_flash_alert flow. How can I resolve this.

Try using:

WebDriver driver = new ChromeDriver();

This is the way I have always instantiated the WebDriver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM