簡體   English   中英

如何使用 selenium webdriver 和頁面對象模型解決 java.lang.NullPointerException

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

我正在使用頁面對象模型通過 Testng 運行這些測試。 但是我得到一個 NullPointerException。 我已經實例化了 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");

    }

}

我已經添加了 webdriver 並在 Flash_alert_element 頁面中對其進行了實例化。 還想我應該在 test_flash_alert 流程中添加這個。 我該如何解決這個問題。

嘗試使用:

WebDriver driver = new ChromeDriver();

這是我一直實例化 WebDriver 的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM