Wednesday, 28 November 2018

Web Application Testing Using Selenium & TestNG





Web Application Testing  Using Selenium & TestNG

  • Selenium 
  • Selenium Frameworks
  • TestNG 




Selenium Web Driver

Selenium WebDriver is the most powerful and famous tool of selenium toolkit. Using Selenium WebDriver, one can write automated web application UI tests in any programming language against any website. It is an open source and free tool that extends support to almost all the major browsers. Selenium WebDriver supports multiple programming languages like Java, C#, Ruby, Python, Javascript and PHP. To make the automated testing using Selenium WebDriver more efficient, test automation frameworks are used.
A test automation framework is basically a well integrated coding pattern that shapes up the thought and clarity of automation testing for better results and understanding. The main function of a framework is to direct the behavior of the test performed. There are various types of frameworks that suit the different test operations. Some of the most popular ones are :
~ Module Based Framework: In this framework, the entire Application under Test ( AUT) is divided into smaller, independent modules and tests scripts that are created for each modules. One can combine the smaller test scripts in order to generate a larger master test script so that they can accomplish the desired test scenarios. This master script is used to call the individual test scripts to run end to end test scenarios. In this framework, testers write function libraries to use it whenever required.
~ Data-driven Framework: In this framework, the Test Script and Test Data are separated from each other.  This allows us to create test automation scripts by just passing different sets of test data. The Test data does not needs to be hard-coded. The test data set is kept in the external files or resources such as MS Excel Sheets, SQL Database Tables, XML files, etc. The test scripts accesses the test data from the external resources. By using this framework we could make the test scripts work properly for different sets of test data. This framework significantly reduces the effort required for creating multiple scripts for different data.
~ Keyword Driven Framework: In a Keyword-driven testing, they use the table format to describe the keywords or action words for each of the functions or methods that needs to be performed. It executes automation test scripts based on only those keywords that have been stated in the excel sheet. With the help of this Framework, it is easy for testers to work with keywords for developing a test automation script. The logic to read keywords and call for the needed action cited in the external excel sheet is located in the main class. Hence, Keyword-driven testing is very much similar to data-driven testing.
~ Hybrid Framework: A Hybrid framework is a modeled testing automation framework that involves various features of the above mentioned three frameworks as described. It involves taking the required attributes of other frameworks for creating the most suitable framework for complex testing scenarios. It also has the ability of keyword driven, data driven and module based operations too. Thus, it is one of the most efficient testing frameworks to be used for complex tests.
One of the most commonly used testing framework is the TestNG framework. The NG in TestNG stands for Next Generation. The TestNG framework covers a wider range of test categories: unit, functional, end-to-end, integration, system, regression, etc. This enables the developer to write more flexible and dynamic tests with the help of annotations, grouping, prioritizing & parametrizing. TestNG can be used for creating a hybrid framework involving Data driven and Module Based frameworks.
It has been inspired by JUnit and NUnit but introduction of some new functionalities make it much better and easier to use than before, The functionalities added are:
Annotations: Various Annotations are available in TestNG
– @BeforeSuite: It will run before all tests in this suite.
– @AfterSuite: It will run after all tests in this suite have been executed.
– @BeforeTest: This method runs before the test method that belongs to the classes inside the tag is performed.
– @AfterTest: This method runs after each and every test methods that belong to the classes inside the tag are executed.
– @BeforeGroups: This method runs shortly before the first test method that belongs to any of these groups has been performed.
– @AfterGroups: This method will run shortly after the last test method that belongs to any of these groups have been executed.
– @BeforeClass: This method runs before the first test method in the current class is applied.
– @AfterClass: This method runs only after all the test methods in the current class have been performed.
– @BeforeMethod: This method will run before all the test method.
– @AfterMethod: This method runs after all the test method.
– @Test: This method is a part of a test case.
~ Grouping: TestNG allows to perform groupings of test methods. We can also execute only set of groups while excluding another groups. This gives us the maximum flexibility in dividing tests according to the requirement. Groups are specified in the testng.xml file and are found under the <test> or <suite> tag. Groups that are specified in the <suite> tag will apply to all the <test> tags underneath it. The groups can be included or excluded using the IDE tools like Eclipse, but the using the testng.xml file is more better.
~ Prioritizing: In TestNG, Priority is used to set the sequence in which the test cases have to be executed. Without prioritizing, the test cases may execute in the disorderly sequence, instead of sequence they are ought to be executed. When there are multiple test cases representing an end-to-end functionality, they have to be executed in a sequence for proper testing. For example, we need to execute a Login test case before executing other test of other functionality in the application. To set priority, we need to set the priority beside the @Test annotation like @Test(priority=0). The test case with priority=0 will be executed first, then the test cases with priority=1, and so on. If you do not give priority to all test cases, then all the tests will be executed in the alphabetical order of their class names.
~ DataProvider: A Data Provider is a method annotated with @DataProvider. A Data Provider returns an array of objects. It means marking a method to supplying data for a test method. The annotated method has to return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method ifit wants to collect data from this DataProvider then it needs to use a dataProvider name equivalent to the name of this annotation.
~ Parallel Tests: Your site will look, feel and perform differently in different browsers. That is because different browsers understand some code slightly differently. Each project needs you to perform multi-browser testing to ensure that the functionality is working as expected in all the browsers to give same user experience to all the audience. TestNG gives us a functionality to execute the same test on different browsers in a simple and easy way. Using the feature provided by TestNG for Parallel Executions, we can execute same tests on various browsers at the same time, helping to perform cross browser testing in very less time.
~ Reports and Asserts: TestNG also has the logging facility for tests. During the execution of a test case, user may want some information to be written on the console, after failure of certain tests or some other event. With the help of TestNG Logs, it is possible to use logging during the Selenium test case execution. TestNG also has the ability to take decisions in the middle of the test run with the help of Asserts methods. Asserts are mainly used to check the presence of an element during a test. There are three main assert statements :
– Assert.assertTrue(): Assert.assertTrue() statement fails the test and stops the execution of the test, if the required element is not found.
– Assert.assertFalse(): Assert.assertFalse() statement fails the test, if some element which not supposed to be present in the page, is present.
– Assert.assertEquals(): Assert.assertEquals() statement fails the test, if the expected value of an element is not equal to the expected value.
~ Reports and Listeners: TestNG provides a very helpful reporting feature. After the test execution, a test-output folder is generated at the root of project. This folder contains two type of Reports:
– Index.html: It is an html report containing information like results, errors, test time, reporter logs, testng XML files, etc.
– emailable-report.html: This is a summarized report of the test execution which contains passed test case messages in green and failed test cases messages in red.
By implementing two interfaces, ITestListener and IReporter, the reports can be customized as per requirement. The ITestListener enables to get a report between execution of the tests, while the IReporter is able to generate a final report after the test is executed. Screenshots can be taken simply by typecasting WebDriver to the screenshot interface. Log files can be generated by implementing the Log4j logging utility.
TestNG has many more features which make it one of the favourite and commonly used framework for Test Automation. Thus, a framework is a crucial necessity for performing automated test operations using Selenium. There are multiple test frameworks available, but choosing the right type of framework based on the type of test to be performed on the application matters the most. Choosing the right framework will not only help with the desirable results but also make the automated testing operation more efficient and effective.

Sunday, 4 November 2018

Polymorphism in Selenium

Polymorphism


Def:-Performing tasks in different ways/ ability of an object to take on many forms.
     - Polymorphism derived from two Greek words,
     Poly-means Many
     Morphs -means forms/ways
     -So Polymorphism means many ways.
Two types of Polymorphism in Java


a) Compile Time Polymorphism---Method Overloading 
b) Run-time Polymorphism---Method Overriding



The most common use of polymorphism occurs when a parent class reference 
is used to refer to a child class object.

Real life example of polymorphism:
Example:-1
You are in class room that time you behave like a student, 
You are in market at that time you behave like a customer, 
You at your home at that time you behave like a son or daughter, 

Example:-2
You are Son to your Father
You are Father to your Son
You are Brother to your Sister
You are Spouse to your wife 


In above Examples  one person present in different behaviors.

1) Method Overloading
  • Method Overloading is to define two or more methods of same name 

in a class, provided that there argument list or parameters are different. 

  • This increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be
any number of arguments, if you write the method such as 
addTwoNos(int,int) for two parameters, addThreeNos(int,int,int) for three parameters 
Then it may be difficult for you as well as other programmers to understand
the behavior of the method because their name differs.
So, we perform method overloading to figure out the program quickly.

Example:
In this example, we have created two overloaded methods, 
first sum method performs addition of two numbers 
second sum method performs addition of three numbers.
------------------------------------------------------------------

class Calculations {
//Method1
 public void sum(int a, int b) 
 {
  System.out.println(a + b);
 }

//Method2
 public void sum(int a, int b, int c)
 {
  System.out.println(a + b + c);
 }

 public static void main(String args[])
 {
  Calculations obj = new Calculations();
  obj.sum(10, 10, 10);
  obj.sum(20, 20);
 }
 
                   } 
--------------------------------------------------------------
--------------------------------------------------------------
2) Method Overriding
  • Child class has the same method as of base class. 
  • In such cases child class overrides the parent class method
        without even touching the source code of the base class.
  • In this process, an overridden method is called through the 
        reference variable of a superclass. 
  • The determination of the method to be called is based on the object
       being referred to by the reference variable.
  • Method overriding applies only to inherited methods.
  • Overriding method can have different return type.
  • Static and final methods cannot be overridden.
  • Constructors cannot be overridden.

Runtime Polymorphism in selenium



In Selenium we use runtime polymorphism concept.
It shows that we can execute same script on any browser.

To achieve runtime polymorphism following steps are mandatory-

  1. Inheritance (firefoxdriver is inheriting from webdriver)
  2. Method overriding (All the methods present in webdriver is overrided, As it is an interface)
  3. Upcasting (casting the object to a supertype)
     webdriver driver=new FirefoxDriver();

Example:-1

 Program for runtime polymorphism

package webdrivercommands;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {
 //Method-1
  public static void testmethod(WebDriver driver) throws InterruptedException 
  {
   
 driver.get("https://goo.gl/maps/oLdAYyMZx3G2");
 System.out.println(driver.getTitle());

 driver.navigate().to("https://saiprasanthadabala.wordpress.com/");
 System.out.println(driver.getTitle());

 driver.navigate().back();
 Thread.sleep(2000);

 driver.navigate().forward();
 Thread.sleep(2000);

 driver.navigate().refresh();
 Thread.sleep(2000);

 driver.quit();
  }


  public static void main(String[] args) throws InterruptedException 
  {
   System.setProperty("webdriver.gecko.driver","D://Silpa//geckodriver.exe");
   WebDriver driver=new FirefoxDriver();
   testmethod(driver);
   System.setProperty("webdriver.chrome.driver","D://Silpa//chromedriver.exe");
   testmethod(new ChromeDriver());//Auto upcast
  }

}
------------------------------
Example:-2
//Class1
class ParentClass{  
    public void status(){
         System.out.println("Parent's Method");
       }  
}  
-----

//Class2
class ChildClass extends ParentClass {
 public void status() {
  System.out.println("Child's Method");
 }

 public static void main(String args[]) {
  // Creating objects
  ParentClass parent = new ParentClass();
  ParentClass parent_1 = new ChildClass();
  ChildClass child = new ChildClass();
  parent.status(); // This will print Parent's status method
  child.status(); // This will print child's status method
  parent_1.status(); // This will print child's status method
 }
}