This practice project is used for Lesson 15: Functional UI Testing with Selenium WebDriver.
The goal of this activity is to learn how to automate a web browser using Selenium WebDriver and verify that a Jakarta Faces page works from a user's perspective.
Functional UI testing follows this pattern:
Open page → find element → interact → wait → assert visible result
You will complete a set of Selenium practice activities using the provided page:
selenium-practice.xhtml
This repository is a public starter project for the Lesson 15 Functional UI Testing with Selenium WebDriver practice activity.
Do not clone the original repository directly unless you only want a read-only copy. If you clone the original repository, you will not be able to push your changes back to GitHub.
Use the following workflow:
- Click Fork on the GitHub repository page.
- Create the fork under your own GitHub account.
- Clone your fork into IntelliJ IDEA.
- Complete the TaskManager JPA practice activity.
- Commit and push your changes to your fork.
- Use what you learned to complete Assignment 4 in your separate Assignment 4 GitHub Classroom repository.
Important:
- This practice repository is not your Assignment 4 submission.
- Assignment 4 must be completed in your separate GitHub Classroom Assignment 4 repository.
- Do not submit your Lesson 15 practice fork for Assignment 4.
By the end of this practice activity, you should be able to:
- open a browser using Selenium WebDriver
- navigate to a Jakarta Faces page
- locate HTML elements using Selenium locators
- enter text into input fields
- click buttons and links
- read visible page content
- verify expected results using assertions
- use
WebDriverWaitinstead ofThread.sleep() - understand how Jakarta Faces generated IDs affect Selenium locators
The starter project includes the following pages:
| Page | Purpose |
|---|---|
index.xhtml |
Home page used for navigation testing |
selenium-practice.xhtml |
Practice page used for Selenium activities |
The selenium-practice.xhtml page includes:
- a page heading
- a search term input field
- a search mode dropdown
- an "Only show stores with stock" checkbox
- a Submit button
- a Clear button
- a result panel
- a validation message area
- a Go Home button or link
You will use Selenium WebDriver to interact with these page elements.
Make sure:
- The project builds successfully.
- The application is deployed and running.
- You can open the practice page manually in your browser.
- You can run the provided Selenium test class.
Example local URL:
http://localhost:8080/selenium-practice.xhtml
Your actual URL may be different depending on your project name and server configuration.
You can run the Selenium test class from IntelliJ IDEA.
You can also run tests from the terminal using Maven:
mvn verifyIf the browser opens and closes automatically, Selenium WebDriver is working.
Do not use Thread.sleep() in your final practice tests.
Use WebDriverWait instead.
Good:
WebElement resultPanel = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("practiceForm:resultPanel"))
);Avoid:
Thread.sleep(3000);A fixed delay may work on your machine once, but it can make tests slow and unreliable.
Complete Activities 1 to 3 first.
These activities are the minimum goal for this lesson.
Confirm that Selenium can open the practice page and read content from it.
Write a test that:
- Opens
selenium-practice.xhtml. - Verifies that the page title contains:
Selenium Practice
- Finds the page heading.
- Verifies that the heading contains:
Functional UI Testing Practice
Useful Selenium methods:
driver.get(url);
driver.getTitle();
driver.findElement(By.id("..."));
element.getText();Useful assertion:
assertThat(driver.getTitle()).contains("Selenium Practice");Learn how to inspect the page and identify stable Selenium locators.
Use your browser's Inspect tool to find the IDs for the following elements:
- search term input field
- search mode dropdown
- in-stock checkbox
- submit button
- clear button
- result panel
- Go Home button or link
Record the locators in comments in your test class.
Example:
// Search term field: practiceForm:searchTerm
// Submit button: practiceForm:submitButtonJakarta Faces often generates client IDs that include the parent form ID.
Example:
practiceForm:searchTerm
When using By.id(), this is fine:
By.id("practiceForm:searchTerm")When using CSS selectors, the colon can cause issues. Use an attribute selector:
By.cssSelector("[id='practiceForm:searchTerm']")Automate a simple user workflow.
Write a test that:
- Opens
selenium-practice.xhtml. - Enters the search term:
keyboard
- Clicks the Submit button.
- Verifies that the result panel contains:
keyboard
Useful Selenium methods:
element.clear();
element.sendKeys("keyboard");
element.click();
element.getText();Example structure:
WebElement searchTermField = driver.findElement(By.id("practiceForm:searchTerm"));
searchTermField.clear();
searchTermField.sendKeys("keyboard");
WebElement submitButton = driver.findElement(By.id("practiceForm:submitButton"));
submitButton.click();
WebElement resultPanel = driver.findElement(By.id("practiceForm:resultPanel"));
assertThat(resultPanel.getText()).contains("keyboard");This version may work, but it can be improved using WebDriverWait in Activity 4.
Complete Activities 4 and 5 after you finish the required checkpoint.
Make the test more reliable by waiting for the browser result before asserting.
Update your Activity 3 test so it:
- Clicks the Submit button.
- Waits until the result panel is visible.
- Verifies the result panel contains the submitted search term.
Create a wait:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));Wait for the result panel:
WebElement resultPanel = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("practiceForm:resultPanel"))
);Then assert:
assertThat(resultPanel.getText()).contains("keyboard");Do not wait for a number of seconds.
Wait for the browser condition you expect.
Verify that Selenium can test page navigation.
Write a test that:
- Opens
selenium-practice.xhtml. - Clicks the Go Home button or link.
- Verifies that the browser navigated to the home page.
You can verify the URL:
assertThat(driver.getCurrentUrl()).contains("index.xhtml");Or verify visible page content:
WebElement heading = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("homeHeading"))
);
assertThat(heading.getText()).contains("Home");Visible page content is usually a stronger assertion than only checking the URL.
Complete Activities 6 and 7 if you finish early.
Verify that the page displays an error message when required input is missing.
Write a test that:
- Opens
selenium-practice.xhtml. - Leaves the search term field empty.
- Clicks Submit.
- Verifies that a validation message is displayed.
The page should display a message similar to:
Search term is required.
Wait for the message element before asserting.
Example:
WebElement message = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("practiceForm:searchTermMessage"))
);
assertThat(message.getText()).contains("Search term is required");Verify that the Clear button resets the form.
Write a test that:
- Opens
selenium-practice.xhtml. - Enters the search term:
monitor
- Selects a search mode.
- Checks "Only show stores with stock".
- Clicks Submit.
- Verifies that the result panel appears.
- Clicks Clear.
- Verifies that the search term field is empty.
To check the current value of an input field:
String fieldValue = searchTermField.getAttribute("value");
assertThat(fieldValue).isBlank();You may also verify that the result panel is cleared or no longer displayed.
If you completed Assignment 3, try writing one Selenium test for your own Jakarta Faces CRUD page.
Suggested workflow:
- Open your create page.
- Enter valid form data.
- Click Create or Save.
- Verify that a success message appears.
- Navigate to the list page.
- Verify that the created item appears in the table.
This is optional practice only.
Your test class may follow this structure:
class SeleniumPracticeSeleniumIT {
private WebDriver driver;
private WebDriverWait wait;
private final String baseUrl = "http://localhost:8080/dmit2015-lesson15-selenium-practice";
@BeforeEach
void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
wait = new WebDriverWait(driver, Duration.ofSeconds(5));
}
@AfterEach
void tearDown() {
if (driver != null) {
driver.quit();
}
}
@Test
void shouldOpenPracticePage() {
// Activity 1
}
@Test
void shouldSubmitSearchRequest() {
// Activities 3 and 4
}
@Test
void shouldNavigateHome() {
// Activity 5
}
@Test
void shouldDisplayValidationMessage() {
// Activity 6
}
@Test
void shouldClearSearchRequest() {
// Activity 7
}
}Check that the Selenium WebDriver dependency and WebDriverManager dependency are included in pom.xml.
Check the actual generated HTML in the browser using Inspect.
Jakarta Faces IDs may include the form ID:
practiceForm:searchTerm
This may fail because of the colon:
By.cssSelector("#practiceForm:searchTerm")Use this instead:
By.cssSelector("[id='practiceForm:searchTerm']")Use WebDriverWait.
WebElement resultPanel = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("practiceForm:resultPanel"))
);This usually means the test has a timing issue.
Replace immediate assertions with explicit waits.
Required:
- Activity 1 completed
- Activity 2 completed
- Activity 3 completed
Target:
- Activity 4 completed
- Activity 5 completed
Extension:
- Activity 6 completed
- Activity 7 completed
Optional:
- Selenium test created for your own Assignment 3 page
Functional UI testing verifies that a user can complete a workflow in the browser.
Remember the pattern:
Open page → find element → interact → wait → assert visible result