I try to do some tests using Selenium, but I can’t test what I do
HomePage.java
package PageObject; import net.serenitybdd.core.annotations.findby.FindBy; import net.thucydides.core.annotations.DefaultUrl; import net.thucydides.core.pages.PageObject; import org.openqa.selenium.WebElement; @DefaultUrl("page:homepage") public class Homepage extends PageObject { //Menu @FindBy(xpath = "//*[@id="fadein"]/header/div[2]/div/div/div/div/div[2]/nav/ul/li[1]/a") WebElement homeLInks; @FindBy(xpath="//a[@title='Hotels']//span[text()='Hotels ']") WebElement hotelsLink; @FindBy(xpath="//a[@title='Travelstart']//span[text()='Flights ']") WebElement flightsLink; @FindBy(xpath="//a[@title='Tours']//span[text()='Tours ']") WebElement toursLink; @FindBy(xpath="//a[@title='Cars']//span[text()='Cars ']") WebElement carsLink; @FindBy(xpath="//a[@title='Offers']//span[text()='Offers ']") WebElement offersLink; @FindBy(xpath="//a[@title='Ivisa']//span[text()='c']") WebElement visaLink; public void checkLink(){ hotelsLink.click(); } }
Hooks.java
import PageObject.Homepage; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class Hooks { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); driver.manage().window().maximize(); System.setProperty("webdriver.chrome.driver","chromedriver"); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://phptravels.net/"); Homepage homepage = new Homepage(); homepage.checkLink(); } }
Error:
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Starting ChromeDriver 101.0.4951.41 (93c720db8323b3ec10d056025ab95c23a31997c9-refs/branch-heads/4951@{#904}) on port 38072 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. Apr 27, 2022 2:46:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C Apr 27, 2022 2:46:27 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch WARNING: Unable to find an exact match for CDP version 101, so returning the closest version found: 99 Apr 27, 2022 2:46:27 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch INFO: Found CDP implementation for version 101 of 99 Exception in thread “main” java.lang.NullPointerException: Cannot invoke “org.openqa.selenium.WebElement.click()” because “this.hotelsLink” is null at PageObject.Homepage.checkCookie(Homepage.java:73) at Hooks.main(Hooks.java:19)
Advertisement
Answer
I could see the following issues:
It seems you didn’t pass driver instance to HomePage?
This line is not needed – System.setProperty(“webdriver.chrome.driver”,”chromedriver”);
The hotelsLink locator seems incorrect.
package PageObject; import net.serenitybdd.core.annotations.findby.FindBy; import net.thucydides.core.annotations.DefaultUrl; import net.thucydides.core.pages.PageObject; import org.openqa.selenium.WebElement; @DefaultUrl("page:homepage") public class Homepage extends PageObject { //Menu @FindBy(xpath = "//*[@id="fadein"]/header/div[2]/div/div/div/div/div[2]/nav/ul/li[1]/a") WebElement homeLInks; @FindBy(xpath="//span[contains(.,'Hotels')]") WebElement hotelsLink; @FindBy(xpath="//a[@title='Travelstart']//span[text()='Flights ']") WebElement flightsLink; @FindBy(xpath="//a[@title='Tours']//span[text()='Tours ']") WebElement toursLink; @FindBy(xpath="//a[@title='Cars']//span[text()='Cars ']") WebElement carsLink; @FindBy(xpath="//a[@title='Offers']//span[text()='Offers ']") WebElement offersLink; @FindBy(xpath="//a[@title='Ivisa']//span[text()='c']") WebElement visaLink; public Homepage(WebDriver driver) { super(driver);//In case you want to pass the driver instance to super class PageFactory.initElements(driver,this); } public void checkLink(){ hotelsLink.click(); }
}
Hooks.java
import PageObject.Homepage; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class Hooks { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://phptravels.net/"); Homepage homepage = new Homepage(driver); homepage.checkLink(); } }
Execution Result: I made the above changes and its working fine for me. (Note: I have removed PageObjects class and some Serenity imports as you didn’t share them)