0
0
Selenium Javatesting~10 mins

PageFactory initialization in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the PageFactory elements in the constructor.

Selenium Java
public class LoginPage {
    WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        [1](driver, this);
    }
}
Drag options to blanks, or click blank then click option'
APageFactory.createElements
BPageFactory.loadElements
CPageFactory.initElements
DPageFactory.setupElements
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like loadElements or createElements.
Forgetting to pass 'this' as the second argument.
2fill in blank
medium

Complete the code to declare a WebElement using PageFactory annotation.

Selenium Java
public class LoginPage {
    @FindBy(id = "username")
    private [1] usernameField;
}
Drag options to blanks, or click blank then click option'
AWebElement
BWebDriver
CBy
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using WebDriver or By instead of WebElement.
Using String which is not a Selenium element type.
3fill in blank
hard

Fix the error in the PageFactory initialization code.

Selenium Java
public class HomePage {
    WebDriver driver;

    public HomePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.[1](driver, this);
    }
}
Drag options to blanks, or click blank then click option'
AinitPage
BinitElements
CinitElementsAsync
DinitElement
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like initElement or initPage.
Swapping the order of parameters.
4fill in blank
hard

Fill both blanks to correctly initialize PageFactory and declare a WebElement.

Selenium Java
public class DashboardPage {
    WebDriver driver;

    @[1](name = "searchBox")
    private [2] searchInput;

    public DashboardPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
}
Drag options to blanks, or click blank then click option'
AFindBy
BWebElement
CFindElement
DWebDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using @FindElement instead of @FindBy.
Declaring element as WebDriver instead of WebElement.
5fill in blank
hard

Fill all three blanks to declare a WebElement with XPath locator and initialize it in constructor.

Selenium Java
public class ProfilePage {
    WebDriver driver;

    @[1](xpath = "//input[@name='email']")
    private [2] emailField;

    public ProfilePage([3] driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
}
Drag options to blanks, or click blank then click option'
AFindBy
BWebElement
CWebDriver
DBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using By instead of @FindBy annotation.
Declaring the element as By or WebDriver instead of WebElement.
Using wrong type for constructor parameter.