0
0
Selenium Javatesting~10 mins

Base test class pattern 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 the base test class with WebDriver setup.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = new [1]();
        driver.manage().window().maximize();
    }
}
Drag options to blanks, or click blank then click option'
AWebElement
BFirefoxOptions
CChromeDriver
DActions
Attempts:
3 left
💡 Hint
Common Mistakes
Using FirefoxOptions instead of a driver class.
Trying to instantiate WebElement directly.
Using Actions class which is for user interactions.
2fill in blank
medium

Complete the code to close the browser after each test method.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @AfterMethod
    public void [1]() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Drag options to blanks, or click blank then click option'
AtearDown
BsetUp
Cinitialize
DstartDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using setUp which is for starting tests.
Naming the method initialize which is unclear.
Using startDriver which is not a standard name.
3fill in blank
hard

Fix the error in the base test class to correctly import the TestNG annotation for setup.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.[1];

public class BaseTest {
    protected WebDriver driver;

    @[1]
    public void setUp() {
        driver = new ChromeDriver();
    }
}
Drag options to blanks, or click blank then click option'
AAfterMethod
BBeforeMethod
CTest
DBeforeClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterMethod which runs after tests.
Using @Test which marks test methods.
Using @BeforeClass which runs once before all tests.
4fill in blank
hard

Fill both blanks to create a base test class that initializes and quits the WebDriver properly.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @[1]
    public void setUp() {
        driver = new [2]();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Drag options to blanks, or click blank then click option'
ABeforeMethod
BAfterClass
CChromeDriver
DFirefoxDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterClass which runs after all tests.
Using FirefoxDriver when ChromeDriver is expected.
Mixing annotations and driver classes incorrectly.
5fill in blank
hard

Fill all three blanks to implement a base test class with setup, teardown, and a sample test method.

Selenium Java
public class BaseTest {
    protected WebDriver driver;

    @[1]
    public void setUp() {
        driver = new [2]();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void [3]() {
        driver.get("https://example.com");
        Assert.assertEquals(driver.getTitle(), "Example Domain");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeMethod
BChromeDriver
CtestHomePage
DFirefoxDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using FirefoxDriver instead of ChromeDriver.
Naming the test method unclearly or missing @Test annotation.
Using wrong annotations for setup.