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


Sunday 11 January 2015

My Python Notes-->(List & List Comprehension)

What is a list ?

List is a collection of similar numbers , strings or both.
List is a container which hold elements in a particular order.
   

Creating a list 

To create an empty list
 lst = []

To create a list of number from 1 to 5
num =[1,2,3,4,5]

To create a list of alphabets
alpha = ['a','b','c','d','e']

To create a list  of words     
seasons = ['Summer','Winter','Autumn','Spring']

To create a list of word containing number
pythonReleases = ['python3.4.2','python 2.7.0','python 2.7.9']

To create a list within a list
bigList =[[], 
                [1,2,3,4,5],
                ['a','b','c','d','e'],
                ['Summer','Winter','Autumn','Spring'],
                ['python3.4.2','python 2.7.0','python 2.7.9']
               ]



Note-----Like string elements we can access each and every list element using the index value.

alpha = ['a','b','c','d','e']

To access the 1st element
firstElement = alpha[0]

To access the last element
lastElement = alpha[-1]

Example 1

Python List Methods

List in python is a class.

Note:List can also be created by using list()--method.
Eg ---list_name = list()




.append(value) ---adds "value" to the end of the list.

Lets consider an empty list
lst = []
To add an element to the list
lst.append(1)



.pop()---remove element from the list

pop()--by default remove the last element

seasons = ['summer', 'winter', 'autumn', 'spring']
seasons.pop()---will remove 'spring' from the list "seasons".

We can remove a particular element based on the index
Syntax-------seasons.pop([index])
seasons.pop(1)--- will remove 'winter' from the list "seasons".

.insert()---Now if we need to insert a value at a particular index we can use insert.

List are Mutable

browsers = ['IE6','Chrome','firefox','UC']

Now the element at index 2 is firefox,  if we what to insert "phantomjs" at index 2 we can do it using insert().
Syntax-------browsers.insert([index],value)


.remove()---remove an element based on its value and not based on the index value.

pythonContainters  = ['list','sets','tuple','dictionary']

pythonContainters.remove('list')---will remove 'list' value from the list "pythonContainers".

If the element to be remove if not found , an error is throw.
Lets try to remove 'jude' from the list 'pythonContainters'
  .count()---count the number of time an element occurs in a given list
lets us create two list
lst = [1,2,1,4]
lst_char = ['a','a','d','e']
Note:List can store duplicate values.

Python String Methods

.joint()---is a string method we can use it to join a list of strings

words = ['my','name' ,'is ','jude ']
If we want to join the above list with a space between each list element

' '.join(words)

If we want to join the above list with '_' between each list element
'_'.join(words)

.split()---is a string method we can convert string into list.

string ="my name is jude"
' '.split(string)-----> will convert the string to a list.

names = "jude job james john joseph"
'j'.split(names)---->will return a list contain word without the letter "j".


Looping over List

For--loop--over single list

Syntax:
            for name in iterable:
                  statements
                  ........
                  ......
          iterable---produces a stream of values
         and assign each value to name.
         for each value of name the statements are executed.

names = ['john','jude','mary','james','joseph']
I want to print the name from the names list using for--loop

for name in names:
      print(name)
Output
Now using for--loop we can print the data and index value as well

enumerate()---help us to make useful pair.


for index,name in enumerate(names):
      print("names[%d] = %s "%(index,name))

 For--loop--over two list

zip()---makes a pair-wise loop
words = ['apple','boy','cat','dog']
alphabets = ['a','b','c','d']

Python List Comprehension

 List-Comprehension allow us to built sequence from other sequence.

Example 2.1--write a program which takes a list and return a new list with square of each element from the given input list(Without list - Comprehension)



Example 2.2--A function which takes a list and return a new list with square of each element from the given list.(Using list-Comprehension)


Output --  for ex2.1.py and ex2.2.py

List-Comprehension which filter input sequence based on a certain condition.
Note:Condition are optional.

Example 3.1--A function which takes a input sequence of number and return a sequence of numbers greater than zero.(Without List Comprehension)


Example 3.2--A function which takes a input sequence of number and return a sequence of numbers
greater than Zero( Using List-Comprehension)

Output -- is for both ex3.1.py and ex3.2.py

 List-Comprehension with multiple conditions

Example 4.1--A function which takes a input sequence of number and return a sequence of even-numbers.(WithOut list-Comprehension)

Example 4.2--With List Comprehension Output for ex4.1.py & ex4.2.py

List-Comprehension for two loops
Example 5.1--Working with two loops Example 5.2--Using list-Comprehension
Output
Example 6.1 Let us combine two list, string list and  number list(WithOut Comprehension)
Note we can convert an integer into a string using str()--method.
Example 6.2---Repeat example 6.1 Using List Comprehension
Output

 Example 7--generating an unity matrix with List Comprehension
A unity matrix =[
                             [1,0,0]
                             [0,1,0]
                             [0,0,1]
                          ]

Example 8--generate a diagonal matrix whose diagonal element are equal to the row number
diagonal_matrix =[
                               [1,0,0,]
                               [0,2,0],
                               [0,0,3]
                              ]

Example 9--generate a diagonal matrix whose diagonal element are equal to square of the row number.
square_matrix =[
                               [1,0,0]
                               [0,4,0]
                               [0,0,9]
                              ]