I have defined Web Elements using Page Object model in Selenium. From test method, when ever I try to access or perform any action on those web elements my test completely skips it and completes, with no error.
public class HomePage extends Base{
@FindBy(xpath="//button[@id='sparkButton']")
public WebElement menuDropDown;
public HomePage(){
PageFactory.initElements(driver, this);
}
public void clickHomepagemenuDropDown() {
menuDropDown.click();
System.out.println("Print HELLO");
}
}
public class Test1 extends Base{
HomePage homepage = new HomePage() ;
@Test(priority=1)
public void homePage() throws Exception {
try {
//do something
homepage.clickHomepagemenuDropDown();
//print something
}catch (Exception e) {
System.out.println (e);
return;
}
}
If I replace homepage.clickHomepagemenuDropDown(); with below lines, my program will run fine.
WebElement mdd = driver.findElement(By.xpath("//button[@id='sparkButton']"));
mdd.click();
Is there some set up that I'm missing?
Update after correcting catch message-- I get following null exception Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null
In your Test1
class you initialized HomePage
class using field initializing so @FindBy(xpath="//button[@id='sparkButton']")
can not find the web element. When you call clickHomepagemenuDropDown()
in the test method, it throws an exception and it enters the catch block. In the catch block you just wrote return
. For this reason the test skips and completes, with no error. I think you should initialize your page objects in your test methods.