Привет, сообщество: я застрял в проблеме с Page Factory.
В настоящее время я работаю с Cucumber, Selenium, Java с Maven. Кстати: я использую WebDriverManager Бони Гарсии.
Это ошибка:
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy15.clear(Unknown Source)
at pages.Page_First.search(Page_First.java:41)
at stepdefs.Step_First.I_query_for_cucumber_spring_selenium(Step_First.java:28)
at ✽.When I query for "<search>" cucumber spring selenium(first.feature:10)
Вот мой BasePage :
public abstract class BasePage extends DriverClass {
public BasePage() {
PageFactory.initElements(driver, this);
}
protected void waitForElementClickable(WebElement element){
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(element));
}
protected void waitForElementVisible(WebElement element){
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOf(element));
}
protected void setTextAs(WebElement element, String text){
waitForElementClickable(element);
element.sendKeys(text);
}
protected void clearElement(WebElement element){
waitForElementClickable(element);
element.clear();
}
protected void clickElement(WebElement element){
waitForElementClickable(element);
element.click();
}
protected void clickElementByJavascriptExecutor(String xpath){
WebElement element=driver.findElement(By.xpath(xpath));
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
}
protected void clickLink(WebElement element){
waitForElementVisible(element);
element.click();
}
protected String getText(WebElement element){
waitForElementVisible(element);
return element.getText();
}
protected void freeze(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Мой 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 {
//Properties taken from config.properties
String browser = ReadProperties.getPropertyValue(key_browser);
String url = ReadProperties.getPropertyValue(key_url);
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();
}
log.info("URL in use: "+ url);
return driver;
}
// hook Before method is used for instantiate driver
@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();
driver.get(key_url);
if (driver == null) {
log.info("-----> Driver did not initialize correctly <-----");
}
}
// Close driver instance
@After
public void close(){
driver.quit();
}
}
Наконец-то здесь, моя Страница
public class Page_First extends BasePage implements Constants {
//Taken from first.properties
String search = ReadProperties.getPropertyValue(key_search);
public Page_First() throws IOException {
PageFactory.initElements(driver, this); }
//////////////////////////////////////////////WEB ELEMENTS//////////////////////////////////////////////////////////
@FindBy(name = "q")
private WebElement searchText;
@FindBy(name="btnK")
private WebElement searchButton;
//////////////////////////////////////////////BASE METHODS//////////////////////////////////////////////////////////
public void startNavigation() {
log.info("Accessing to Google Chile");
}
public void search(String search) {
searchText.clear();
searchText.sendKeys(this.search);
}
public void enterButton (){
// clickElement(searchButton);
}
}
Может ли кто-нибудь помочь мне, пожалуйста? Заранее спасибо.
ОБНОВЛЕНО: Это мой Свойства Reader:
public class MultiplePropertyReader {
private static Properties properties;
public static void main(String[] args) {
try {
properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("first.properties"));
//First properties fields
System.out.println("::: First Feature Property File 1 Data :::");
System.out.println(properties.getProperty("search"));
//Set the properties (First properties)
properties.setProperty("search", "Selenium Cucumber" );
} catch (Exception e) {
System.out.println("No properties file found...");
e.printStackTrace();
}
}
}
first.properties файл:
search = "Selenium Cucumber"
А вот, мои константы класс:
public interface Constants {
String key_url = "url";
String key_browser = "browser";
//First feature constants
String key_search = "search";
}