tips to become an expert automation testing-meu-solutions.com

Getting Started on Automated Testing: Writing Your First Automation Test Case With Selenium Webdriver

In the previous article “Getting Started on Automated Testing: Let start with What, Why and When questions“, we introduced you what automation testing is as well as its benefits and what beginners really need for automation testing. So, in this article, we will go step by step with you an automation tool to break the iceberg by creating the first automation test case with the Selenium WebDriver.

tips to become an expert automation testing-meu-solutions.com

First, we need to know what is Selenium? What is Webdriver?

Selenium is an open source tool which is used for automating the tests carried out on web browsers. It has different components and WebDriver is one of them. We can conclude that Selenium WebDriver is a web-based automation testing framework which can test web pages initiated on various web browsers and various operating systems.

Advantages of Selenium WebDriver

  • Since Selenium is an open-source tool, you can use it for free so there is no licensing cost involved, which is a major advantage over other testing tools.
  • It also supports many programming languages such as Java, Python, C#, PHP, Ruby, Perl and .Net as well as any of these OSs: Windows, Mac, Linux and any of these browsers: Mozilla Firefox, Internet Explorer, Google Chrome, Safari or Opera.
  • It can be integrated with tools such as TestNG & JUnit for managing test cases and generating reports and with Maven, Jenkins & Docker to achieve Continuous Testing.

Besides advantages, there are some limitations of the Selenium Webdriver

  • It only supports web-based applications
  • Selenium automates Web browsers only, with Selenium we can automate web applications in computer and mobile but we cannot automate desktop applications.
  • If you want to use Selenium WebDriver for automated testing, you must have the knowledge at least one of the programming languages mentioned earlier.
  • Additionally, Selenium interacts with web pages, so you need to locate HTML elements and then perform actions on those elements such as clicking on a button or entering text into a text box, etc,.. so you also need to have the knowledge of HTML.

In WebDriver, there are 8 different ways to find element:

By.name

By.id

By.className

By.tagName

By.cssSelector

By.xpath

By.linkText

By.partialLinkText

 

Because it is an open source framework, it’s hard to find reliable to support if there are any issues, you can just seek for help on selenium community forums.

But Selenium WebDriver is still a good choice for beginners who want to learn automation testing.

Now we will give a tutorial to you to install Selenium WebDriver as well as write the first test case. As we said Selenium WebDriver supports many program languages but in this tutorial, we just guide you to write a test case with Java and use TestNG.

Let’s get started

Step 1: Install Eclipse IDE:

eclipse IDE for java EE developer-meu solutions

  • Create a folder with the name: “AutoTest” in any disk you want.
  • Create a subfolder: ”AutoTest/Java”
  • Extract Eclipse to the path: “AutoTest/Java/Eclipse”

config eclipse-meu solutions

  • Create a folder for a workspace at path: “AutoTest/Java/workspace”
  • Then run the eclipse.exe file and select the above directory for the workspace

set workplace for eclipse-meu solutions

Step 2: Install plugin TestNG

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:

  • Run Eclipse and from the Help menu, click “Install New Software”

install new software in eclipse-meu solutions

install testng in eclipse-meu solutions

  • Then you will see a dialog, click “Add” button, type “TestNG” as Name and type “http://beust.com/eclipse/” as location. Click “Add” button.
  • After that you will see TestNG option in the available software list, just tick TestNG and press “Next” button.

install testng in eclipse 2-meu solutions

  • Just click “Next” button until you reach the License Agreement dialog.
  • Click “ I accept…” then click “Finish”

install testng in eclipse 3-meu solutions

  • Now you just to need to wait for installing. If you see a security warning, just click “Anyway”

install testng in eclipse 4-meu solutions

  • To finish installing TestNG, Click “Restart Now” to restart Eclipse

install testng in eclipse 5-meu solutions

Step 3: Install Selenium Library

Install Selenium library - meu solutions

  • Extract the downloaded file to a folder
  • Create folders Lib/Selenium anywhere you want
  • Copy all Jar files to Lib/Selenium

Install Selenium library 2 - meu solutions

  • Then copy Lib/Selenium folders to the project folder

Install Selenium library 3 - meu solutions

  • Let’s create a new project and add the selenium library to the project

Install Selenium library 4 - meu solutions

  • Create the project with name “AutoTest” -> right click on the project -> select Properties
  • To add selenium library to the project, choose Java Buil Path->Library Tab -> click Add External JARs.. -> Select all JAR files then press “Open” like steps in the picture.

Install Selenium library 5 - meu solutions

  • Press “Apply and Close” to add selenium library.

Install Selenium library 6 - meu solutions

Step 4: Install ChromeDriver in the project

Install ChromeDriver in the project - meu solutions

  • Extract file and copy chromedriver.exe file to the project

Install ChromeDriver in the project 2

  • Then go to eclipse: right click on the project -> Refresh. You will see chromedriver.exe, Lib and test-output files.

Install ChromeDriver in the project 3

Step 5: Create a package and write a first test case

  • Create a package : Right click on “src” file -> New -> Package with name “testplan”

Create a package and write first test case

  • Right click on the packages and select TestNG->Create TestNG class

Create a package and write first test case 2

  • Type class name “MyFirstTestCase” -> Check to “@BeforeClass” and “@AfterClass” – > Click “Finish”

Create a package and write first test case 3

  • Modify class MyFirstTestCase.java as below:

In this test case, we will create a new account for this website http://newtours.demoaut.com/

Here are the codes snippets.

package testplan;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

import org.testng.annotations.AfterClass;

 

public class MyFirstTestCase {

WebDriver driver;

@Test

public void f() {

driver.get("http://newtours.demoaut.com/");

//Click REGISTER link

driver.findElement(By.partialLinkText("REGISTER")).click();

//Clear first name

driver.findElement(By.name("firstName")).clear();

//Enter last name

driver.findElement(By.name("lastName")).sendKeys("Surname1");

//Enter phone number

driver.findElement(By.name("phone")).sendKeys("123456789");

//Enter email address

driver.findElement(By.name("userName")).sendKeys("user1@test.com");

//Enter address

driver.findElement(By.name("address1")).sendKeys("Test Address");

//Enter city name

driver.findElement(By.name("city")).sendKeys("Test City");

//Select country

Select select = new Select(driver.findElement(By.name("country")));

select.selectByVisibleText("ANGOLA");

//Enter user name

driver.findElement(By.name("email")).sendKeys("user1@test.com");

//Enter password

driver.findElement(By.name("password")).sendKeys("user1");

//Enter confirm password

driver.findElement(By.name("confirmPassword")).sendKeys("user1");

//Click register

driver.findElement(By.name("register")).click();

}

@BeforeClass

public void beforeClass() {

driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

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

}

@AfterClass

public void afterClass() {

if(driver!=null)

{

//Close browser

driver.close();

//Kill driver

driver.quit();

}

}

}

 

Step 6: Save the class. To execute the test case, just right-click on MyFirstTestCase.java – > Run As -> TestNG Test.

execute the test case

Final Step: Observe and enjoy your first test case executed by the Selenium WebDriver

Conclusion

So how do you feel after running the first test case by the Selenium WebDriver? It’s faster and less boring when you do it with manual regression testing right? That’s one of automation testing‘s advantages. And if you think Selenium WebDriver is suitable for you, we recommend looking for selenium’s documents to understand more about it.

If you have any question or information to share about Selenium WebDriver as well as automation testing, just comment right here.

See you again in the next article and happy reading.

Post a Comment