Website testing is an important aspect of ensuring the quality and functionality of your website. With the increasing popularity of Selenium WebDriver, it has become a widely used tool for automating website testing in Java. Whether you are a beginner or an experienced tester, it is always beneficial to improve your skills by practicing hands-on exercises.
In this article, Novatesting will cover 5 essential training exercises that will help you master the basics of testing websites with Selenium WebDriver in Java. These exercises are designed to help you understand the various features and capabilities of the tool, and will give you a solid foundation for developing your testing skills further.
- Testing the page title of a website:
scss
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to the website
driver.get("https://www.example.com");
// Get the page title
String pageTitle = driver.getTitle();
// Verify the page title
assertEquals("Example Web Page", pageTitle);
// Close the WebDriver
driver.quit();
- Testing the presence of an element on a page:
scss
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to the website
driver.get("https://www.example.com");
// Find the element
WebElement element = driver.findElement(By.id("element_id"));
// Verify the element is present
assertTrue(element.isDisplayed());
// Close the WebDriver
driver.quit();
- Testing the filling of a form on a page:
scss
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to the website
driver.get("https://www.example.com/form");
// Fill the form
driver.findElement(By.id("name_field")).sendKeys("John Doe");
driver.findElement(By.id("email_field")).sendKeys("[email protected]");
driver.findElement(By.id("submit_button")).click();
// Verify the form was submitted
WebElement successMessage = driver.findElement(By.id("success_message"));
assertTrue(successMessage.isDisplayed());
// Close the WebDriver
driver.quit();
- Testing the navigation between pages:
scss
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to the website
driver.get("https://www.example.com");
// Navigate to another page
driver.findElement(By.linkText("About")).click();
// Verify the navigation was successful
String pageTitle = driver.getTitle();
assertEquals("About Example", pageTitle);
// Close the WebDriver
driver.quit();
- Testing the page load time:
scss
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Get the start time
long startTime = System.currentTimeMillis();
// Navigate to the website
driver.get("https://www.example.com");
// Get the end time
long endTime = System.currentTimeMillis();
// Calculate the load time
long loadTime = endTime - startTime;
// Verify the load time is less than 3 seconds
assertTrue(loadTime < 3000);
// Close the WebDriver
driver.quit();
The 5 exercises outlined in this article will provide you with a comprehensive understanding of how to test websites with Selenium WebDriver in Java. By practicing these exercises, you will be able to master the basics of the tool and gain confidence in your testing skills. Happy testing!