如何解决对象为空 - 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();
}
}
将永远感谢您的帮助!
解决方法
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);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。