黄瓜硒 (Java) - PageFactory - NullPointErexception 获取 URL
Cucumber Selenium (Java) - PageFactory - NullPointException getting URL
嗨社区:我被困在下一期:
java.lang.NullPointerException
at pages.Page_First.getURL(Page_First.java:31)
at stepdefs.Step_First.I_go_to_Google(Step_First.java:22)
at ✽.I go to Google (src/test/resources/features/first.feature:9)
这是我的特色:
Feature: Navigation Test
As a user, bla, bla, bla...
Scenario: Search google.com to verify google search is working
Given I go to Google
When I query for "<search>" cucumber spring selenium
And click search
Then google page title should become the first page
下一个是我的DriverClass
public class DriverClass implements Constants {
// Take the instance of WebDriver
protected static WebDriver driver;
//Initialize a log
protected static final Logger log = Logger.getLogger(BasePage.class.getName());
public static WebDriver initializeDriver() throws IOException {
String browser = ReadProperties.getPropertyValue(key_browser);
if(browser.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if(browser.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if(browser.equals("ie")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else if(browser.equals("edge")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
} else {
System.setProperty("webdriver.safari.driver","/usr/bin/safaridriver");
driver = new SafariDriver();
}
return driver;
}
}
另一个是我的 DriverInitializer Class
public class DriverInitializer extends DriverClass {
@Before
public void initialize() throws IOException {
log.info("-----> Proceed to initialize driver <-----");
driver = DriverClass.initializeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
if (driver == null) {
log.info("-----> Driver did not initialize correctly <-----");
}
}
@After
public void close(){
driver.quit();
}
}
我的页面Class:
public class Page_First extends BasePage implements Constants {
public Page_First() {
PageFactory.initElements(driver, this); }
//////////////////////////////////////////////WEB ELEMENTS//////////////////////////////////////////////////////////
@FindBy(name = "q")
private WebElement searchText;
@FindBy(name="btnK")
private WebElement searchButton;
//////////////////////////////////////////////BASE METHODS//////////////////////////////////////////////////////////
public void getURL() throws IOException {
String url = ReadProperties.getPropertyValue(key_url);
driver.get(url);
我的步骤定义:
public class Step_First {
public static WebDriver driver;
private Page_First page_first = PageFactory.initElements(driver, Page_First.class);
@Given("I go to Google")
public void I_go_to_Google () throws IOException {
page_first.getURL();
}
我知道问题出在下一段代码(Page):
String url = ReadProperties.getPropertyValue(key_url);
driver.get(url);
顺便说一下:这是我的 常量 class
public 接口常量 {
String key_url = "url";
String key_browser = "browser";
}
拜托,我需要你的帮助。提前致谢
更新:这是我用来阅读 属性 个文件的文件:
public class ReadProperties {
public static final String filePath = "src/main/resources/data/config.properties";
public static String getPropertyValue(String key) throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(filePath);
prop.load(fis);
fis.close();
return prop.getProperty(key);
}
}
可能是它没有找到您的路径。试试这个:
public class ReadProperties {
public static String getPropertyValue(String key) throws IOException {
String filePath = "src/main/resources/data/config.properties";
InputStream input = new FileInputStream(filePath);
Properties prop = new Properties();
try (input) {
prop.load(input);
return prop.getProperty(key);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
嗨社区:我被困在下一期:
java.lang.NullPointerException
at pages.Page_First.getURL(Page_First.java:31)
at stepdefs.Step_First.I_go_to_Google(Step_First.java:22)
at ✽.I go to Google (src/test/resources/features/first.feature:9)
这是我的特色:
Feature: Navigation Test
As a user, bla, bla, bla...
Scenario: Search google.com to verify google search is working
Given I go to Google
When I query for "<search>" cucumber spring selenium
And click search
Then google page title should become the first page
下一个是我的DriverClass
public class DriverClass implements Constants {
// Take the instance of WebDriver
protected static WebDriver driver;
//Initialize a log
protected static final Logger log = Logger.getLogger(BasePage.class.getName());
public static WebDriver initializeDriver() throws IOException {
String browser = ReadProperties.getPropertyValue(key_browser);
if(browser.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if(browser.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if(browser.equals("ie")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else if(browser.equals("edge")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
} else {
System.setProperty("webdriver.safari.driver","/usr/bin/safaridriver");
driver = new SafariDriver();
}
return driver;
}
}
另一个是我的 DriverInitializer Class
public class DriverInitializer extends DriverClass {
@Before
public void initialize() throws IOException {
log.info("-----> Proceed to initialize driver <-----");
driver = DriverClass.initializeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
if (driver == null) {
log.info("-----> Driver did not initialize correctly <-----");
}
}
@After
public void close(){
driver.quit();
}
}
我的页面Class:
public class Page_First extends BasePage implements Constants {
public Page_First() {
PageFactory.initElements(driver, this); }
//////////////////////////////////////////////WEB ELEMENTS//////////////////////////////////////////////////////////
@FindBy(name = "q")
private WebElement searchText;
@FindBy(name="btnK")
private WebElement searchButton;
//////////////////////////////////////////////BASE METHODS//////////////////////////////////////////////////////////
public void getURL() throws IOException {
String url = ReadProperties.getPropertyValue(key_url);
driver.get(url);
我的步骤定义:
public class Step_First {
public static WebDriver driver;
private Page_First page_first = PageFactory.initElements(driver, Page_First.class);
@Given("I go to Google")
public void I_go_to_Google () throws IOException {
page_first.getURL();
}
我知道问题出在下一段代码(Page):
String url = ReadProperties.getPropertyValue(key_url);
driver.get(url);
顺便说一下:这是我的 常量 class
public 接口常量 {
String key_url = "url";
String key_browser = "browser";
}
拜托,我需要你的帮助。提前致谢
更新:这是我用来阅读 属性 个文件的文件:
public class ReadProperties {
public static final String filePath = "src/main/resources/data/config.properties";
public static String getPropertyValue(String key) throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(filePath);
prop.load(fis);
fis.close();
return prop.getProperty(key);
}
}
可能是它没有找到您的路径。试试这个:
public class ReadProperties {
public static String getPropertyValue(String key) throws IOException {
String filePath = "src/main/resources/data/config.properties";
InputStream input = new FileInputStream(filePath);
Properties prop = new Properties();
try (input) {
prop.load(input);
return prop.getProperty(key);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}