Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like loadElements or createElements.
Forgetting to pass 'this' as the second argument.
✗ Incorrect
The correct method to initialize PageFactory elements is PageFactory.initElements(driver, this).
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using WebDriver or By instead of WebElement.
Using String which is not a Selenium element type.
✗ Incorrect
WebElement is the correct type for elements declared with @FindBy in PageFactory.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like initElement or initPage.
Swapping the order of parameters.
✗ Incorrect
The correct method is initElements with parameters (driver, this). The order matters.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @FindElement instead of @FindBy.
Declaring element as WebDriver instead of WebElement.
✗ Incorrect
The annotation is @FindBy and the element type is WebElement.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Use @FindBy annotation, WebElement type for the field, and WebDriver type for the constructor parameter.