In this tutorial, we would make you acquainted with the primary sections of an elemental WebDriver test script. We would create a sample Selenium WebDriver script for login functionality to make you familiar with code and strategy.
Before starting with the test script creation, let us take a moment to introduce you with the Application under Test (AUT) and the test scenario, we desire to automate.
As said, we would use “yahoomail.com” as an application under test. The scenario to be automated is as following:
– Launch the web browser and open “yahoomail.com”.
– Verify the page title to check if the correct page is launched.
– Enter an invalid username and password.
– Click on login button
– Verify that error message is displayed for authentication failure.
We are assuming that you have already downloaded and installed Selenium WebDriver on your system. Users who have not yet downloaded or installed Selenium WebDriver can read our article on “WebDriver Installation” from here.
Test Script Creation
For ease of understanding, let us discuss the process in steps:
Note: To create Java project inside the Eclipse IDE, refer this article.
Step 1: Create a new Junit test case named as “YahooInvalidLogin” under the java project inside the Eclipse IDE. Follow the steps below to create a test class.
– Right click on the project -> New -> Junit Test Case.
– Name the Junit test case as “YahooInvalidLogin”. Check the checkboxes for setUp() and tearDown() methods. Click on the Finish button.
– Now the test class is in place and the user would be able to see the test class as below:
Sample Code
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class YahooInvalidLogin {
// Create WebDriver instance
WebDriver driver;
@Before
public void setUp() throws Exception {
// Initialize the WebDriver instance using FirefoxDriver and launch the web browser
driver = new FirefoxDriver();
// Open the application - "https://login.yahoo.com/"
driver.get("https://login.yahoo.com/");
// Maximize the current window
driver.manage().window().maximize();
}
@Test
public void testYahooInvalidLogin() throws Exception {
// Verify the page title to check if the correct page is launched
String pageTitle = driver.getTitle();
if (!pageTitle.equals("Sign in to Yahoo"))
{
System.out.println("Launched the incorrect webpage");
tearDown();
}
// Enter an invalid username and password
WebElement TB_Username = driver.findElement(By.id("username"));
TB_Username.sendKeys("InvalidUsername");
WebElement TB_Password = driver.findElement(By.id("passwd"));
TB_Password.sendKeys("InvalidPassword");
// Click on login button
WebElement BTN_Login = driver.findElement(By.id(".save"));
BTN_Login.click();
// Wait for authentication process
Thread.sleep(3000);
// Verify that error message is displayed for authentication failure.
String InvalidLoginMessage = driver.findElement(By.xpath("//fieldset[@id='fsLogin']//div")).getText();
if (InvalidLoginMessage.equals("Invalid ID or password."))
{
System.out.println("Correct message is displayed");
}
else
{
System.out.println("Incorrect message is displayed");
}
}
@After
public void tearDown() {
// Quit the launched web browser
driver.quit();
}
}
Code Overview
Import Statements
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
Before moving to the coding section, import the following packages.
– import org.junit.Before – The statement makes the junit Before package available. Thus, now the user can use the Before annotation of Junit. Take a note that the user can use the before annotation to annotate the code /method to be executed before the actual test scenario. The code may be used to initialize variables etc.
– import org.junit.After – The statement makes the junit After package available. Thus, now the user can use the After annotation of Junit. Take a note that the user can use the after annotation to annotate the code /method to be executed after the actual test scenario. The code may be used to quit the web browser etc.
– import org.junit.Test – The statement makes the junit Test package available. Thus, now the user can use the Test annotation of Junit. Take a note that the user can use the test annotation to annotate the code /method that contains the actual test scenario. The code may be used to quit the web browser etc.
– All the other import statements are used to be able to use WebDriver artifacts like browser driver, element locating strategies etc.
Initialization steps
WebDriver driver;
In the above step, we create reference variable for WebDriver. Thus, the same variable now can be used across the entire class to perform actions.
Launching Web Browser
driver = new FirefoxDriver();
Open the application under test
driver.get("https://login.yahoo.com/");
Perform Actions
User can perform number of actions using the WebDriver instance and dynamic finders. Dynamic finders are used to locate the elements on the webpage. The elements can be located using different locator types.
WebElement TB_Username = driver.findElement(By.id("username"));
Quiting the browser
driver.quit();
Execution and Reporting
The user would be able to see the following Junit execution report.
Conclusion
The tutorial was all about script creation in WebDriver. I hope it made a clear picture into your minds about the constituents of a basic WebDriver script.
Very helpful and informative!!
Why have you not used assertions.
Also you can check for not equals check for the page title and call the tear down function itself. This will also remove the else statement and if the statement is not true then it will proceed further.
This is good coding practice and will reduce the code
Thank you for pointing it out. Actually i created an elementary test script. I would write about assertions in my upcoming tutorials and that is why i haven’t used it in this tutorial but i will surely take the teardown thing..
if (!pageTitle.equals (“Sign in to Yahoo”))
{
system.out.println(“Launched the incorrect webpage”);
Assert.fail();
tearDown();
}
Similarly for invalid password check for not equal
Wow, great blog.Thanks Again. Want more.
I like the valuable information you provide in your articles.
Thanks for the blog.Really looking forward to read more. Really Cool.