首页 > 解决方案 > 对象为空 - Cucumber Selenium

问题描述

我正在尝试加载菜单但收到此错误:

预习

功能文件:

预习

这是代码:

Step Definition (Failing @When step):
public class ThresholdsAndAlertsPageSteps {
    
    private LoginPage loginPage = new LoginPage(DriverFactory.getDriver());
    private DashboardPage dashboardPage;
    private ThresholdsAndAlertsPage thralertPage;
    
    @When("user clicks on ThresholdsAndAlerts link")
    public void user_clicks_on_thresholds_alerts_link() {
        dashboardPage.clickMenu();
        thralertPage = dashboardPage.clickMenu();
    }

    @Then("user gets the title of the page menu {string}")
    public void user_gets_the_title_of_the_page_menu(String expectedTitle) {
        String actualTitle = thralertPage.getPageMenu();
        Assert.assertTrue(actualTitle.contains(expectedTitle));
        System.out.println(actualTitle);
    }
}

页:

package com.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class ThresholdsAndAlertsPage {
    
    private WebDriver driver;
    
    //1. Locators
    private By dfn_thrs = By.xpath("//html[@id='konga']/body/div/div[2]/div[2]/div/div/div[2]/div/div/div/a");
    private By cfg_thrs_on1 = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div/div/div/div/span[3]");
    private By ent_srvc = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[2]/div/select");
    private By lim_typ = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[3]/div/select");
    private By thrs_typ = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[4]/div/select");
    private By act_typ = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[5]/div/select");
    private By act_cfg = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[6]/div/chips/div[2]/input");
    private By act_frq = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[7]/div/input");
    private By cfg_thrs_on2 = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[8]/div/div/div/span[3]");
    private By val = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[9]/div/input");
    private By unit = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[10]/div/select");
    private By add_thrs = By.xpath("//html[@id='konga']/body/div/div/div/div[3]/div/form/div[11]/div/button");
    private By menu_title = By.xpath("//div/div[2]/div[2]/div/div/div/h3/span");
    
    //2. Constructor
    public ThresholdsAndAlertsPage(WebDriver driver) {
        this.driver = driver;
    }
    
    //3. Page Actions   
    public String getPageMenu() {
        return menu_title.toString();
    }
    
    public ThresholdsAndAlertsPage navigateThresholdsAndAlertsPage() {
        return new ThresholdsAndAlertsPage(driver);
    }
    
    public void clickDefineThresh() {
        driver.findElement(dfn_thrs).click();
    }
    
}

将永远感激帮助!

标签: seleniumcucumber-junit

解决方案


DashboardPage并且ThresholdsAndAlertsPage类未初始化。你也必须通过司机。

Step Definition (Failing @When step):
public class ThresholdsAndAlertsPageSteps {
    
    private LoginPage loginPage = new LoginPage(DriverFactory.getDriver());
    private DashboardPage dashboardPage;
    private ThresholdsAndAlertsPage thralertPage;
    
    @When("user clicks on ThresholdsAndAlerts link")
    public void user_clicks_on_thresholds_alerts_link() {
        dashboardPage = new DashboardPage(DriverFactory.getDriver());
        dashboardPage.clickMenu();
        thralertPage = new ThresholdsAndAlertsPage(DriverFactory.getDriver());
        thralertPage = dashboardPage.clickMenu();
    }

    @Then("user gets the title of the page menu {string}")
    public void user_gets_the_title_of_the_page_menu(String expectedTitle) {
        String actualTitle = thralertPage.getPageMenu();
        Assert.assertTrue(actualTitle.contains(expectedTitle));
        System.out.println(actualTitle);
    }
}

推荐阅读