Let’s Automate (Part – 1)

Here in this post I would like to start with the automation of Generic login and log out scenario.

In my previous post, we had discussed about the topics that are needed to learn Selenium WebDriver and the setups required before starting an Automation Test Script. Now here we will learn to write an automation code for Facebook application, in particular, login and logout scenario.

First, let’s write the scenarios that are used to execute login and logout functionality. Following is the order of scenarios required to be automated:

  1. Open a Firefox browser.
  2. Navigate to URL.
  3. Maximise the window.
  4. Enter the Username and Password on Login Page.
  5. Click on Login button.
  6. Sign out from the Facebook account.
  7. Close the window.

Now we will automate the above-mentioned scenarios in the same order in which they are written. First, we need to make sure if we know to create a Java Project, Package, Class, how to import JAR files into the project and how to identify locators for different WebElements as I had mentioned in my previous post. We should also know to use the Eclipse very well.

Before writing the script, we should identify the WebElements and their locators necessary in writing the script. So, following are the WebElements and their locators:

  1. Email or Phone – //input[@id=’email’]
  2. Password – //input[@id=’pass’]
  3. Login Button – //label[@id=’loginbutton’]
  4. Menu Button – //div[@id=’userNavigationLabel’]
  5. Logout Button – //div[@id=’BLUE_BAR_ID_DO_NOT_USE’]/div/div/div/div/div/ul/li[12]

Here we have used “XPath” as locators to identify the WebElements. We will write the code as below on Eclipse:

 

package Blog;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class LoginLogout

 {

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

{

//Open the Firefox browser by creating new instance of the Firefox Driver

WebDriver driver = new FirefoxDriver();

 

//Navigate to URL

 driver.get(“https://www.facebook.com/“);

 

//Maximize the window

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

 

//Enter UserName

driver.findElement(By.xpath(“//input[@id=’email’]”)).sendKeys(“username”);

 

//Enter Password

 driver.findElement(By.xpath(“//input[@id=’pass’]”)).sendKeys(“password”);

 

//Click on Login Button

 driver.findElement(By.xpath(“//label[@id=’loginbutton’]”)).click();

 

//Wait statement

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

 

//Click on Menu Button

driver.findElement(By.xpath(“//div[@id=’userNavigationLabel’]”)).click();

 

//Wait statement

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

 

//Click on logout

 driver.findElement(By.xpath(“//div[@id=’BLUE_BAR_ID_DO_NOT_USE’]/div/div/div/div/div/ul/li[12]”)).click();

 

//Close the Browser

driver.close();

}

}

 

 

In the above code, we have used few methods related to WebDriver. So let me brief out on those methods and their uses:

get()

Uses : It will load a new web page in the current browser window. This is done using http get operation, and the method will block until the load is complete.

Syntax : get(“url”)

 

findElement()

Uses : Find the required WebElement using the given method.

Syntax : findElement(By by)

 

sendKeys()

Uses : Put the test data into editable box.

Syntax : sendkeys(“Test Data“)

 

click()

Uses : Click the WebElement where we are using this method.

Syntax : click()

 

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

This would maximize current window if not already maximized”.

 

quit()

Uses : Quits the driver instance, closing every associated window which is opened.

Syntax : quit()

 

On running the script, we can watch the desired action being automated in few secs via automation which takes mins to execute the same scenario through manual tests.

So, in this post, we learnt about automating the Facebook Login and Log-out functionality using WebDriver.

Now we can automate many more scenarios by using the methods that we have used in our code.

Hope this post helps all the Beginners  J. Stay tuned for next posts on automation!

Advertisements

Steps To Learn Selenium WebDriver For Beginners

In today’s world where internet is easily available, it is easy to access learning materials. If someone wants to know something new, he/she can get answer from google or tutorial videos available on YouTube. Today people are learning coding, new techniques or anything from any field from the internet.

But, the challenge here lies in finding the right content in right way. If someone wants to learn Java, then the person should know the procedure to learn it and relevant topics to be covered. The purpose of this blog is to identify the topics and the order in which people can understand and learn Selenium WebDriver in easier way.

Here we will just talk about the steps and not the details about a topic. For details on a particular topic, one can find references easily available on the internet.

Before that, let me brief out on Software testing and its types.

Software Testing:  It is a process of executing the application with an intention to find defects in it. A defect deviation in application function from the requirement.

So basically, Software testing is the part of quality assurance process which a software has to go through in order to get certified. This is required to ensure the compliance of a software product in relation with regulatory, business, technical, functional and user requirements.

Types of Software Test Execution:

  1. Manual Execution (Also known as Manual Testing)
  2. Automated Execution (Also known as Automation Testing)

Manual Testing: It is a type of testing in which testing is done manually. In this testing, testers do not use any automated tool or any script. There are different stages for manual testing such as unit testing, integration testing, system testing, and user acceptance testing.

Automation Testing: Testing is carried using an automation tool to execute testing scenarios. It is when the tester writes scripts and uses another software to test the product. This process involves automation of a manual process. Automation Testing is used to re-run the test scenarios that are performed manually. It is faster than manual execution and helps in repetitive execution of tests. It is also known as Test Automation.

Apart from Regression testing, Automation testing can also be used as part of Non-functional testing i.e. load testing, performance testing, stress testing etc. It increases the test coverage, improves accuracy, and saves time and money in comparison with manual testing.

Types of Software Testing Tools:

Following are the most popular test tools:-

  • HP Quick Test Professional
  • Selenium
  • IBM Rational Functional Tester
  • SilkTest
  • Windmill
  • Sahi

Here, let me focus on the topics to be covered to learn Selenium Web Driver Basics:

Selenium:

Selenium is a suite of tools to automate web browsers across many platforms. The entire suite of tools results in a rich set of testing functions specifically geared to the needs of testing of web applications of all types. These operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behaviour.

It has four components and they are as follows:

  1. Selenium Integrated Development Environment (IDE)
  2. Selenium Remote Control (RC)
  3. WebDriver
  4. Selenium Grid

Selenium WebDriver:

Selenium WebDriver is a collection of Core Java APIs. It is not a UI based tool. Technically, it is an interface and all browser classes implements WebDriver interface. It acts as native support with all the browser so that it can directly communicate to browser.

Advantages:

  1. It Supports multiple browsers like Firefox, Chrome, IE, Safari, Android, iOS etc.
  2. It supports multiple platforms like Windows, Linux etc.
  3. It supports multiple languages like Java, Perl, .Net, Python etc.

Before learning WebDriver, first and foremost, it is very important to know any of the programming language mentioned above. If we want to use WebDriver with Java, then it is important to have good knowledge of Core Java.

Under Core Java, understanding basics like Methods, Variables, Loops and OOPs concept should be covered in detail. One should also have sound knowledge in String, Exception Handling, Collection and Array. These are the topics which should be properly covered in Java before we start learning WebDriver.

Once we learn Java, next step is to learn HTML basics as it is used for describing web pages and is helpful in identifying locators correctly.

Selenium uses ‘Locators’ to find and match the elements of the webpage that it needs to interact with. Using the right locator ensures tests are faster, more reliable or has lower maintenance over releases. But there will be times when choosing a right locator will become a nightmare. It can be a real challenge to verify that we have the right locators to accomplish what we want.

There are total 8 locators present in WebDriver:-

  1. Id
  2. Name
  3. Linktext
  4. Partial Linktext
  5. Tag Name
  6. Class Name
  7. CSS Selectors
  8. Xpath

As it is important to have good knowledge of mentioned locators.

Following set up should be available in the system:

  1. Download and open Eclipse.
  2. Download Selenium WebDriver from Selenium official website and add it as external libraries in Eclipse.
  3. Download Apache POI from Apache POI official website and add it as external libraries in Eclipse.
  4. Download TestNG from Eclipse’s market place and install it.

Below is the advisable order in which topics should be taken up before starting with framework:

  1. Basic APIs related to WebDriver including methods like get(), getText(), close(), quit(), manage(), navigate(), findElement(), isDisplayed(), isSelected(), getAttribute() etc.
  2. Select Class.
  3. Actions Class.
  4. WebDriver wait statements.
  5. Window and Pop-up alert handling.
  6. Frame Handling.
  7. Working with bulk elements.
  8. Working with multiple browser.
  9. File download and file attachment.
  10. File handling by using Apache POI.
  11. Unit Testing Framework by using Junit or TestNG.

Once the above mentioned topics are learnt with adequate practice, one can go for Framework implementation exercise.

Following are the different types of frameworks that can be used for implementation:

  1. Hybrid Driven Framework.
  2. Keyword Driven Framework.
  3. Data Driven Framework.

If all of the above mentioned Steps and Procedures are covered and learnt accordingly with loads of Practice, one can surely start working on live Projects successfully.

Happy Learning!