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
 }
}

Monday, 13 August 2018

Software Testing Lab Manual

Software Testing Lab Manual

  • 1)     Understand the concepts of manual testing---
  • 2)     Automation testing approaches------
  • 3)     Introduction to selenium--------------------------------
  • 4)     How to design test cases-------------------------------------Selenium IDE
  • 5)     How to design test suites with examples -----------------------Selenium IDE
  • 6)     Installation of Selenium Web Driver ---------------------------- SWD Environment Setup
  • 7)     Write a Test Script for Following

                           I.          Launch Chrome Browser
                          II.          Open Test Url www.google.com
                        III.          Click on Gmail Link Text
                       IV.          Navigate Back  to Previous Page
                         V.          Navigate Forward to Next Page
                       VI.          Refresh Current Page
                      VII.          Maximize Browser Window
                    VIII.          Close Browser
  • 8)Write a  Test Script to automate  newtours.demoaut.com  Registration Page
  • 9) Write a Test Script to Verify  Element Existence Gmail link existence in Google home page

Test Steps:
i) Launch the browser
ii) Navigate to Google.com (Google Home page)
Verification point
Check the existence of Gmail link.
  • 10) Write a Test Script to automate Site was Secure or not?

Test Steps:
                           I.          Launch the browser
                          II.          Open Test Url Google.com (Google Home page)
                        III.          Navigate to www.newtours.demoaut.com
Verification point
Check the URL.
  • 11) Write a program to login into a web page------------SWD
  • Verify Login Successful

                           I.          Launch the browser
                          II.          Open Test URL  Google.com (Google Home page)
                        III.          Navigate to www.way2sms.com
                       IV.          Fill Mobile Number Textbox with Valid Phone Number
                         V.          Fill Password Textbox with Valid  Password
                       VI.          Click  Login Button
Verification point
Check the URL.

  •  
  • 12) Write  Test Script to automate  Google search box  with multiple  Data (JXL)

                           I.          Verify page title  
                          II.          Update Excel sheet  with result Test Passed/Failed



  • 13) How to design test suites using a selenium tool----TestNG Framework

Saturday, 14 July 2018

Types of Software


Types of Software






STE vs SDET


STE vs SDET 


Software Testing

Selenium IDE 3.1.1


Selenium IDE

ü Selenium IDE is an  integrated development environment for  Selenium tests.
ü It is implemented as a Firefox extension, and allows you to record, edit, and replay the test in Firefox.
ü Selenium IDE allows you to save tests as HTML, Java, Ruby scripts, or any other format
ü It allows you to automatically add assertions to all the pages.
ü Allows you to add selenese  commands as and when required
ü It’s an easy-to-use Chrome and Firefox extension
ü Record and Play back user action in WebApp
ü Its not Recommended for stable , long staying Test Suites
ü Test only on Chrome, Firefox Browser
Installing the IDE

ü Download the IDE using Chrome or Firefox
ü Click on Add to Chrome or Add to Firefox.
ü After Installation a button will appear on the right hand side of the address bar.

Opening the IDE
ü To run the Selenium IDE, simply click the button to the right of the address bar.
ü It opens as follows with an empty script-editing window and buttons to open a project or save one.













ü  Speed Control: controls how fast your test case runs.
ü  Run All: Runs the entire test suite when a test suite with multiple test cases is loaded.
ü  Run: Runs the currently selected test. When only a single test is loaded this button and the Run All button have the same effect.
ü  Pause/Resume: Allows pausing and resuming of the running test case.
ü  Stop: Stops the current running test case.
ü  Step: Allows you to “step” through a test case by running it one command at a time. Use for debugging test cases.
ü  Record: Records the user’s browser actions.

Recording Selenium Test Case
ü  Open Firefox that has the IDE installed
ü  Open the base URL of the application to record.
ü  Go To Tools  Selenium IDE and the IDE will be opened
ü  Now perform the operations on the application as you are testing the application.
ü  Once you are done with the recording click on the stop recording button and save the test case through the file menu. By default it will be saved as a selenese script (.side format)

Command Table
  • Command : Command column describes name of command.
  • Target :Target column describes Target element id or name or Xpath
  • Value :Value column describes Value of an element














Wait and wait for
ü Selenium IDE will now automatically wait for you, at every command
ü Every command will wait for the page to load.
ü  Commands which take a locator will wait for the element to appear.
ü  Hopefully this will eliminate the need for explicit waiting.
ü  In case that is not enough, the pause command can be used instead.
Recording
ü During recording, Selenium-IDE will automatically insert commands into your test case based on your actions.
ü  Typically, this will include:
§  clicking a link - click or click at commands
§  entering values - type command
§  selecting options from a drop-down listbox - select command
§  clicking check boxes or radio buttons - click command

Verifications and Asserts With the Context Menu
ü While Recording Script, Right Click anywhere on the page.
ü You will see a context menu showing verify and/or assert commands

Editing

ü  Insert Command
            Right-click and select Insert Command
ü  Insert Comment
            Comments may be added to make your test case more readable
ü  Edit a Command or Comment
            Select the line to be changed ,edit it using the Command, Target, Value and Comment fields.

Running Test Cases

ü  Run a Test Case
            Click the Run button to run the currently displayed test case.
ü  Run a Test Suite
            Click the Run All button to run all the test cases in the currently loaded test suite.
ü  Pause and Star
The Pause button can be used to stop the test case while it is running.
The icon of this button then changes to indicate the run is paused.
 To continue click it again, or click the Run button.
ü  Stop in the Middle
            -You can set a breakpoint in the test case to cause it to stop on a particular command.
            -select a command, right-click, and from the context menu select Toggle Breakpoint.
ü  Start from the Middle
-You can tell the IDE to begin running from a specific command in the middle of the test case. 
            -select a command, right-click, and from the context menu click Play from here.
ü  Run Any Single Command
            -Double-click any single command to run it by itself. 

Selenese
Selenium commands known as SELENESE
Sequence of selenese forms test script
Three types of Selenese –
Actions
    • Commands that do user action like click, sendKeys

 Accesors
   •examine / access the current state of application-under test

Assertions
   • Verify/validate the expected results
• Few commands comes with parameters(They are
optional depends on command)

Selenese Commands

  • ·       Open

opens a page using a URL.
  • ·       Click

performs a click operation, and optionally waits for a new page to load.
  • ·       verifyTitle/assertTitle

verifies an expected page title.
  • ·       verifyTextPresent

verifies expected text is somewhere on the page.
  • ·       verifyElementPresent

verifies an expected UI element, as defined by its HTML tag, is present on the page.
  • ·       verifyText

verifies expected text and its corresponding HTML tag are present on the page.
  • ·       verifyTable

verifies a table’s expected contents.

Test Scenario
ü Launch way2sms
ü Type Phone number
ü Type Password
ü Click Login
ü After Page Load Verify text
ü logout

Limitations of Selenium IDE
ü Can run the test only on Firefox.Chrome
ü No Programming login (like loops,conditional statements) can be applied
ü Selenium IDE can execute scripts created in Selenese only.
ü It is difficult to use Selenium IDE for checking complex test cases .