Tuesday 30 December 2014

Installing Python & Selenium



Why Python ?

Python is my favorite Programming Language and it also happens be the only Programming Language I know.
C-Programming Language is the most widely used language of all times because it is a simple language to learn. JAVA is also widely used because it is an OBJECT-ORIENTED Programming Language.
Python has the simplicity of C-Programming-language and is an OBJECT-ORIENTED Programming Language like JAVA.

For developing games in Python we can use---pygame
For Testing we can use modules -----------------unittest, pytest,nose
For web-development-------------------------------django
For standalone Application-------------------------tkinter
For web-scrapping-----------------------------------urlib, BeautifulSoup

Google use python for its Search Engine
Instagram and Pinterest use Python's Django Framework

_______________*******_____________________********____________________________

Installing Python 

Step 1
Download Python from this URL or from "http://www.python.org"


 Step 2 :Open the Downloaded Python Installer



Step 3:Install python for all users




Python will be installed at C:\Python34\




Installation Begins



Installation Continues


Finally it is over


Step 4:Go to Start and type IDLE in the search box



Step 5:Open IDLE  
Step 6: type "import this"
___________________________******___________________******_____________________

Configuring Python from Windows Command Line

Now in order to use Python from Window Command Line, we need to configure the PATH variable found in the SYSTEM VARIABLES

Step 1: Right click on My Computer and Select Properties
Step 2:Select Advanced System settings


Step 3: We need to change System Variable which is found in Environment Variables



Step 4: In System Variables we need to edit PATH variable, so Select PATH and Click Edit Button


Step 5: Editing the PATH variable
Please add  ;C:\Python34\
Don't leave any space after the last \

After changing the PATH variable click on OK Button.

Step 6: Clicking OK again

Step 7: Go to start and type CMD  in the search box

Step 8: Running python from windows Command Line


Now Type python in the command Prompt


___________________________******___________________******_____________________


What is Selenium??

Selenium is an open source web-UI automation tool.
In particular let us focus on Selenium 2.0 (webdriver). WebDriver is a clean, fast framework for 
automation of web application

Key Features

  • Multiple-browser support.Firefox,Chrome,Safari,Opera & IE
  • Multiple-OS support.Windows,Linux,Android & IOS
  • Multiple Language Support. Python,Ruby,Java


Installing Selenium--(Selenium webdriver)

Step 1:Run windows command shell prompt as Administrator

Step 2: Windows Shell opens 

Now type C:\Python34\Scripts\pip.exe install selenium 
in the shell 
   

The Download starts

Download Complete



Step 3: Verify the installation of selenium

We will access Python by Window shell by typing python



 __________________________*******________________****______________________

Example 1---Using Selenium let us open Firefox browser

Step 1: we need to  import webdriver from Selenium



Step 2: create a firefox browser object





Example 2---Using Selenium to navigate to any valid URL

Step 1: we need to  import webdriver from selenium


Step 2: create a firefox browser object



Step 3: Navigate to "http://yahoo.com"
we can navigate to any valid URL using get--method.





Example 3 ----Open a particular web-page and verify if Selenium has indeed opened the correct webpage


Step 1: we need to  import webdriver from selenium



Step 2:create a firefox browser object



Step 3: Navigate to "http://pythonselenium.blogspot.com"
we can navigate to any valid URL using get--method.



Step 4: Finding the Title of any given webpage
we can find it by this command.




Step 5: In-order to verify that selenium has opened the correct web-page we verify it using title of web-page



Step 6: Closing the browser



The complete code for Example 3

Logging Exception In Selenium

 

What is  an Exception ?

When inputs to function are invalid or pre-condition are not meet an error is raised know as Exception.
To know more about exception we will see ValueError & DivideByZero exception in python
and later  on NoSuchElementFound in selenium

Exception in Python

Example 1--ValueError
For the code below if the user enter string or alpha-numeric an exception is raised.






Example 2--DivideByZero




How to handle Exceptions ?


Exception can be caught using TRY & EXCEPT block statements.
Note: Exception should not be used as part of the normal code,but only for exceptional situations.
Only catch exception that you know to handle.

Solution for Example 1--Handling ValueError


Output



Solution for Example 2--Handling DivideByZeror


Output

Exception in Selenium


NoSuchElementFound--- exception is raised when element to be located by the following methods
fails
  1. find_element_by_id
  2. find_element_by_name
  3. find_element_by_tag_name
  4. find_element_by_css_selector
  5. find_element_by_xpath
  6. find_element_by_link_text
  7. find_element_by_partial_link_text
  8. find_element_by_class_name

Example 1--NoSuchElementFound


 

Handling "NoSuchElementFound" exception




Output in IDLE


Output in Window Command-line



What is Logging ?

Logging is a standard python Module.It record or track the events during the execution of a Application.Logging message are displayed or stored using Logging function and logging Levels.
  • Developers  use it for Debugging ie....fault investigation.
  • System Admin use it for Monitoring ie... resources are used effectively. 
  • Help Desk use it for answering client about the problem and about the Patch or workaround to be applied.

Logging Functions

  • logging.debug('This function provides detailed information ')
  • logging.warning('Something unexpected happened')
  • logging.error('For storing exception traces')
  • logging.info('It is a confirmation that things are going according to plan')
  • logging.critical('The main action to be performed fails')

Logging Levels

Given below are logging levels in the increasing order of severity.Critical is the highest level and Info is the lowest level...ie if we configure the logs for WARNING our log file will contain logs of WARNING,ERROR & CRITICAL.The default log message are WARNING
  • DEBUG
  • INFO 
  • WARNING
  • ERROR
  • CRITICAL

Creating a simple logger

Step 1: Import the logging Module


Step 2: Create a logger with a level of logs to be recorded


Step 3:  Create a Log-Handler


Step 4: Create For-matter for Log-Handlers

  
Step 5: Add the log-handler to the logger
       
Example 1--Using the above steps we can write our first Logger, warning logs are found in 'warning_log.txt' and INFO logs in 'info_log.txt'



Output
 warning_log.txt
info_log.txt


Example 2--Handling Exception


Contents of  ex1_critical.txt
 


Example 3--We will consider an Example where we have to
  1. Navigate to www.youtube.com and Assert for the Title
  2. locate the searchBox
  3. Search for Python Tutorials
An exception can be raised if Invalid Locator to the searchBox is given(NoSuchElementException) or if the title for the Youtube Page in Test Script is Wrong(AssertionError)
 Here were store the CRITICAL logs in a file called Youtube.txt and INFO logs are seen at the console.

Example 3.1 Handling--AssertionError CRITICAL logs ---found in-'Youtube.txt'


Contents of Youtube.txt file

Example 3.2 Handling -- NoSuchElementException---CRITICAL logs ---found in-'Youtube.txt'


Contents of Youtube.txt file

Example 3.3 Finally the correct code
NOTE:Logging configuration can be placed in seperate logging.conf file
Output

Monday 22 December 2014

WORKING WITH CHROME WEBDRIVER

What is  a CHROME WebDriver ??

It is an open Source Tool for Automated Testing of Web-Pages in Google Chrome.
It provides capabilities for
  1.  Navigating to web pages
  2.  User input
  3.  JavaScript execution, and more.

Installing CHROME WebDriver

Step 1
          Download the latest Release from chrome webdriver Page
Step 2
          Unzip the downloaded file(chromedriver_win32)
Step 3
           After Unzip we have chromedriver file copy this file to C:\Python\Scripts\
Step4
     
         Open IDLE and type,
 

Chrome is Up & Running.





Steps to be followed before writing a Test Scrips

  1. Understand the Requirement 
  2. Write Test Plan and Test Cases
  3. Decide which Test Case to Automate
  4. Inspect the required Web UI elements  
  5. Starting writing your Test Script
Example 1:---LogIn to your Gmail Account Verify your Username in the HomePage
 and then LogOut.

Step 1

Requirement:Accessing your Gmail Account using Selenium(Gmail Sign In)

Step 2


Test Scenarios
  1. Enter a Valid UserName ,Password & login,check for the Username in  the HomePage.
  2. Logout and check for the Gmail Login Page.
Test Case
     Log In
  1. Open the Browser.
  2. Navigate to "http://gmail.com"
  3. Verify the Title of Gmail Login Page.
  4. Enter the Username and Password and click submit.
  5. Verify the  Username in the HomePage
   Logout 
  1. Hover Over the Username in the Home Page
  2. Click the LogOut Button
  3.  Verify the Title of Gmail Login Page.

Step 3

Inspection of Web UI elements using FireBug & FirePath.
We are going to use FireBug to extracting the HTML ID ,HTML NAME  of UI elements.
FireBug can be downloaded from this URL

Working With FireBug

Step 1 :
           
           Open FireBug
             



 Step 2 : 
            
            Inspecting elements with FireBug 
           Right click over the element to be inspected.
   

           For LogIn Test Case
            1)Username Inspection
            
         
        Username can be located using HTML ID & NAME
      


   2)Password Inspection
          
       Password can be located using HTML ID & NAME
      

     3)SignIn Button Inspection
           

               Submit Button can be located using HTML ID & NAME


             Working With FirePath

              To access the xpath of Web - UI elements
              FirePath can be downloaded from this URL
                Step 1 :
               Select FirePath















 Step 2 :
                For LogOut Test Case

                 1)Inspect the Username in HomePage
Xpath for Username found in the HomePage
            
2)Inspect SignOut
     




       Finally the Test Script



Output




  
 



Wednesday 17 December 2014

Unit Testing With Python

Unit Test Framework
Selenium is used for Web UI automation ,
To write and to maintain test cases a framework is required.
Here in this case  Unitest is used for this purpose

About Unittest

  • Unittest module started life as the third-party module PyUnit.
  • Unittest support fixtures,test suites and a test runner to enable automated testing.


Core Class of unittest
  1. TestCase 
  2. TestSuite
  3. TestLoader
  4. TextTestRunner
  5. TestResults
TestCase
  • TestCase is a one of the core classes of unittest module.
  • TestCase has to be inherited from unittest module
  • All the test routines or methods must start with "test".
  •  Only then will they be executed, Test case can be in a different file,if so the module to be tested must be imported.

 


Example 1


Unit test for lowercasevowel.py


Output


Note: each "." represents testcase successfully executed ,if test case fails "F" instead of "."


Structure of TestCase Class of unittest 

  1. setUp() where pre-condition for executing the test case are written down.
  2. tearDown() here clean up ,which is required after each test case is written down.
  3. skipTest() will skip the current test.
  4. fail() will fail the test explicitly ie..It will fail the test an raise AssertionError.
  5. id() Return a string containing the name of the TestCase object and of the test method.
  6. shortDescription() Return the Docstring present in the Test case method.
Note:setUp() get executed before all the test_case() method and tearDown() after each test_case()
methods.

Example 2
 Let us use the setUp() and tearDown() methods





Example 3
 Let us use the setUp() and tearDown() id() & shortDescription() methods




Problem 1
Search for a python tutorial on youtube ?

Answer

  1. Open a browser (firefox)
  2. Nagivate to "http://www.youtube.com"
  3. Verify that youtube page is opened
  4. Locate the search Box and type in Python Tutorials
  5. To take a screenshot

The complete solution with unittest as Framework

 


Output