0
0
Selenium Javatesting~10 mins

Page class design 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 declare a WebDriver variable in the page class.

Selenium Java
private [1] driver;
Drag options to blanks, or click blank then click option'
AWebDriver
Bdriver
CWebElement
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using WebElement instead of WebDriver
Using variable name instead of type
Using String type
2fill in blank
medium

Complete the constructor to initialize the WebDriver variable.

Selenium Java
public LoginPage([1] driver) {
    this.driver = driver;
}
Drag options to blanks, or click blank then click option'
AString
BWebDriver
CWebElement
DLoginPage
Attempts:
3 left
💡 Hint
Common Mistakes
Using WebElement as parameter type
Using class name as parameter type
Using String type
3fill in blank
hard

Fix the error in the method to locate the username input field.

Selenium Java
public WebElement getUsernameField() {
    return driver.findElement([1]);
}
Drag options to blanks, or click blank then click option'
ABy.id("username")
BBy.name("username")
CBy.xpath("//input[@id='username']")
DBy.className("username")
Attempts:
3 left
💡 Hint
Common Mistakes
Using By.name when the element has an id
Using By.className which may not be unique
Using complex XPath unnecessarily
4fill in blank
hard

Fill both blanks to create a method that clicks the login button and waits for the home page.

Selenium Java
public void clickLogin() {
    driver.findElement([1]).click();
    new WebDriverWait(driver, Duration.ofSeconds(10))
        .until(ExpectedConditions.[2](By.id("homePage")));
}
Drag options to blanks, or click blank then click option'
ABy.id("loginBtn")
BvisibilityOfElementLocated
CBy.name("login")
DelementToBeClickable
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong locator for login button
Using elementToBeClickable instead of visibilityOfElementLocated for waiting
Not waiting for the home page element
5fill in blank
hard

Fill all three blanks to create a method that enters username and password, then clicks login.

Selenium Java
public void login(String username, String password) {
    driver.findElement([1]).sendKeys(username);
    driver.findElement([2]).sendKeys(password);
    driver.findElement([3]).click();
}
Drag options to blanks, or click blank then click option'
ABy.id("username")
BBy.id("password")
CBy.id("loginBtn")
DBy.name("login")
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong locators for input fields
Using By.name("login") instead of id for login button
Mixing up username and password locators