Search code examples
javaseleniumintellij-ideacucumberpage-factory

How do I correctly implement PageFactory annotations within Selenium?


What I am trying to do:

I am currently migrating my Selenium(Java) framework from a standard Page Object model + cucumber setup to a PageFactory + cucumber setup.

This is what I've done so far to try and make it work:

  • I Initiated my PageFactory.
  • I imported all required packages and dependencies.
  • I checked multiple online tutorials to make sure I have the right syntax.

The issue I run into:

My IDEA (IntelliJ) keeps giving me errors on the @FindBy annotations and failing to compile because of it.

When I mouse-over my @FindBy annotations it tells me "@FindBy not applicable to method"

When I try and run my test it gives me the following error on each occurence of the @FindBy annotation. "annotation type not applicable to this kind of declaration"

The code my question is about:

    package pageObjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class AssociatedAlerts {


    public AssociatedAlerts(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    @FindBy(css = "#Rabo_SelectedBatchSLTransactionrow0 > * > [type = 'checkbox']")
    public WebElement firstAlertCheckboxSelected();
}

I must be missing something simple but I just can't seem to spot it. Any help would be much appreciated.

Thanks in advance.


Solution

  • You have an error in your syntax:

        @FindBy(css = "#Rabo_SelectedBatchSLTransactionrow0 > * > [type = 'checkbox']")
        public WebElement firstAlertCheckboxSelected();
    

    firstAlerchCheckboxSelected is NOT a method. It's a variable.

    Change it to:

        @FindBy(css = "#Rabo_SelectedBatchSLTransactionrow0 > * > [type = 'checkbox']")
        public WebElement firstAlertCheckboxSelected;