Showing posts with label unittest. Show all posts
Showing posts with label unittest. Show all posts

Friday, 23 January 2015

Handling Web Alerts

Web Alert are used to confirm the action taken by the user.
It take the focus away from the current window , and force the user to read the alert message.

Generally Web alerts consist of  message and  an OK, CANCEL button.

How does Selenium WebDriver handle Web Alert ??

In selenium WebDriver when an alert appears in the web-page ,
the control is still in the web-page hence we have to switch the control from
the web-page to alert window.

For accessing the Web Alert , we first have to create an alert object.

alert = browser.switch_to_alert()

Now we have control over the alert.
To perform action on the alert we need to know about alert class.

What is a Alert Class??

With the help of  Alert Class we can interact with alerts.
With this class we can
                              accept the alert using accept()----method
                              reject the alert using dismiss()-----method
     enter value in the alert box using send_keys()-----method
       receive the text found in the alert using ".text"


Simple Alert  Button

Lets handle a simple alert.In order to generate alert please copy the below html code,
and save it in ex1.html .



Navigate to folder where the file is saved and double click it.


The browser open with



Now before we write test scripts lets write test cases.

                                      test_ex1.html

Output in window's command prompt


Alert with OK & CANCEL Buttons

Save the below code in ex2.html


When we open ex2.html in a browser we get



The Test Cases for ex2.html are


test_ex2_v1.py



Lets us run test_ex2_v1.py in command line.


Structure of test_ex2_v1.py ,it has  two class namely  ConfirmationTestCase & ConfirmationTest




Now let us put the ConfirmationTestCase class in a file called ex2Base.py



Let us retain  ConfirmationTest and save the file as ex2Test.py



lets us run ex2Test.py from window command line.










Dialogue Box




















Please copy the below code for generating a dialogue box as show above.



Now lets write the test cases












test_ex3.py



Now lets execute test_ex3.html from window command line,










Thursday, 15 January 2015

My Python Notes:class ToolKit & unittest Framework

What is a class ?

Class is way to represent Object found in the Real World.
The way we define a function using the keyword def ,class can be created using the keyword class.
So Lets create a class which describes the behavior of a human being using a Human class. 

Syntax   
class Human(object):


Class allow us to logical group data & functions and the data is know as attributes,functions is referred to as  methods.

As humans  we  have a name & age these are attributes.

Now let create a class Human with name & age.To represent the behavior of the Human we have defined two method eat() & my_stomach().
       
human.py
                      

With human.py we have create a human class ie... we have a blueprint for Humans


Function within a class are known as methods
Now lets create a who Human whose name is Jude Augustine,age 24 from the Human class ie... we are creating an object whose name is Jude Augustine & age 24

                                              object.py


lets run object.py


What have we done we have create a new object from the Human class

Note:
__init__()--- is used to assign value to the object ie..initialize the attributes.
The INIT method doesn't have a return statement, but it return the object that was created.

__str__()--- it return information about the object.

Lets us talk  to Jude why is he so sad.


Let write code it and save it in conversation1.py

Lets make Jude Happy....


Lets write the code for the above and save in conversation2.py





Creating Multiple Objects from Human Class

 Create James & John whose ages are 21 &22 have a sister whose name is Lisa who is 18 year Old,lets
create them using the Human Class.

                                              family.py
                                       
                                 save the above code in family.py & run it
#1 in family.py we are importing the Human class from the file human.py

#2 we create a brother_james with name james & age 21.

#3 we create a brother_john which is an object of class Human with name John & is 22 years old.

Creating multiple objects using List-Comprehension

                                       

from the above code we notice that humans[0] ----> James,24
                                                         humans[1] ----> John,22
                                                         humans[2] ----> Lisa,18

Now lets test  human.py using Unittest module



Lets now write the code for the above test cases.
 
                                    testHuman.py

 Now lets run  it testHuman.py




Now lets write some negative test cases


lets write the code & save it in negative.py

TookKIT
                  Human Class with few TookkIT elements.



 

1)Instance Variables are defined within the __init__ ()--method.

Information about Instance variable may vary as we can see from the multiple objects code.
Eg for Human class , name & age are instance variables.

2)Regular methods----function within a class are know are methods
Eg---- __init__(), __str__() ,eat() , my_stomach() are all methods

all the above method use self ,ie method using self are a parameter are known as Regular Methods.

3)Class Variables--- as human being we all have a heart & every being has One Heart.
   Eg-- heart = 1

4)Static Methods-- method that dont need self as a paramter, because they dont modify the Instance variables.They are helper function which can be used by other Regular Method.
They are added to class using decorators.

Eg   @staticmethod
        def heart_sound():
               print("lup--dup--lup---dup")

How does you heartBeat

the code for the above scenario 
heart.py


Run it


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