Test Diary

Archives


Social Profile


Recent Comments


My first Selenium WebDriver Test (Text Fields, URL, Buttons)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu Next Topic

In this post we will begin our first selenium webdriver training. Our first test will be to validate the page title of the Test diary website. In our previous tutorials on selenium IDE, we performed a record and play back test on the Test diary contact page. Our second test will be to perform the same test with webdriver.

Prerequisite: Eclipse or any IDE, Java, Firefox Browser and Selenium jars in your build path. (If you do not have any of this, please refer to our previous posts on Installation.

(Validate the page title on Test diary)
  1. Create a new java class “Testdiary_VerifyTitle” under a default project.
  2.  Copy and paste the code in the below.
  3. All the phrases after the “//” sign explain each  line of code.


//Import Selenium webdriver and firefox driver
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

// Class Testdiary_VerifyTitle
public class Testdiary_VerifyTitle {

// Main method
public static void main(String[] args) {

//Open a new Firefox Browser
WebDriver driver = new FirefoxDriver();

// Maximise the browser window
driver.manage().window().maximize();

// Save the url of the page into a string object
String appUrl = "http://www.testdiary.com/";

// Open the application url
driver.get(appUrl);

// Declare and initialise the variable to store the expected title of
// the web page.
String expectedTitle = "Test Diary - A software testers guide";

// Fetch the title of the web page and save it into a string variable
String actualTitle = driver.getTitle();

// Use an if condition to check if both string objects are equal
if (expectedTitle.equals(actualTitle)) {

System.out
.println("verification successful - The correct Tile is Displayed on the Webpage");
} else {
System.out.println("verification unsuccessful");
}

// Close the browser
driver.close();

// End the program
System.exit(0);
}
}

Explanation of the Test

This test will open the browser, and go to the URL we specified. Also we tell selenium to fetch the title of the Test diary page and then we use an “If” condition to check if the title we have is the same as the title on the page. If they are the same, we then print the success message, if not we say the test is unsuccessful.

You can alter this code and change the URL, to your own website. Of course this test will fail because selenium will find a different title to the one you specified in the code. Therefore change the “String expectedTitle” to the correct title of the page and the test should pass.

First Test (Validate the contact form on Test Diary)
  1. Create a new java class  “Testdiary_ContactPage” under a default project.
  2.  Copy and paste the code in the below.
  3. All the phrases after the “//” sign explain each  line of code.

“A thread.sleep wait method will be used in this test because we have not covered selenium waits.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Testdiary_ContactPage {

public static void main(String[] args) throws InterruptedException {

// Open a new firefox browser
WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

// Initialise the variable to store the URL
String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";

// Navigate to the URL
driver.get(contactUrl);

// Allow the current thread (the process) to sleep for some seconds.
// Gives The page time to load.
Thread.sleep(4000);

// locate the Name text box on the webpage. Using the locator "By.name"
// Store this text box location to the variable "name"
WebElement name = driver.findElement(By.name("your-name"));

// clear the text box
name.clear();

// Insert your name into the text box using the sendKeys method.
// I am using my name in this scenario
// You can input your own name
name.sendKeys("Femi");

// locate the email text box. Using the locator By.name
// store this text box location to the variable "email"
WebElement email = driver.findElement(By.name("your-email"));

// clear the text box
email.clear();

// Insert your email into the text box using the sendKeys method.
// I am using my email in this scenario
// You can input your own email
email.sendKeys("xxxx@testdiary.com");

// Insert your subject into the text box
WebElement subject = driver.findElement(By.name("your-subject"));
subject.clear();
subject.sendKeys("New tester");

// Insert your message into the text box
WebElement your_message = driver.findElement(By.name("your-message"));
your_message.clear();
your_message.sendKeys("I want to be the best tester.");

// The code in the above should fill up the contact form on Test diary.

// locate the send button. Using the locator By.xpath
WebElement send = driver.findElement(By.xpath("//input[@value='Send']"));

// Use the .click method to click this button
send.click();

// Allow the current thread (the process) to sleep for some seconds.
// Gives The page time to load.
Thread.sleep(4000);

/*
* Message after sending inquiry. locate the success message banner,
* using the locator By.xpath Insert the .getText() method after the
* locator to let Selenium get the text
* from the Webelement you have located
*/
String actual_message = driver.findElement(By.xpath("//div[@id='wpcf7-f551-p683-o1']/form/div[2]")).getText();

// Declare and initialise the variable to store the expected success message of the web page.
String expected_message = "Your message was sent successfully. Thanks.";

// Use an if condition to check if both string objects are equal
if (expected_message.equals(actual_message)) {

System.out.println("verification successful - " + actual_message);
} else {
System.out.println("verification unsuccessful");
}

// Close the browser
driver.close();

// End the program
System.exit(0);
}

}

Explanation of the Test

This test will fill up the contact form on the Test diary website. Also, it will check if the correct success message is displayed after you fill up the form. This is to ensure that the form actually works on the front end.  Based on our previous lessons on selenium IDE, we have covered locator types and how you can find them using the “Inspect Elements” on the webpage or using the “Select” function on selenium IDE. Selenium webdriver uses the .findElement() method to find elements. The test above shows us examples of using this method with name and Xpath Locators. 

In summary we have covered the following selenium webdriver commands

  1. WebDriver driver = new FirefoxDriver()  (Open a new firefox browser)
  2. driver.manage().window().maximize() (Maximize the browser)
  3. driver.get()  ( Navigate to the URL)
  4. driver.findElement(arg0) (locate the webelement)
  5. clear() (clear the text box)
  6. sendKeys() (enter a value into the text box)
  7. click() (click on a webelement)
  8. getText() (get text from a webelement)
  9. close() (Close the browser)

This is  a basic introduction to selenium webdriver and as we progress, we will go through more selenium commands for writing selenium tests. 

You can download or clone this tests from the Testdiary Github.

Click: Basic Selenium Tests

Happy Testing!!!

Menu Next Topic

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.

  • Nahim

    nice explanation, looking forward to more posts on selenium from you