Saturday, March 17, 2018

Python : OOPS important Concepts

Python: OOPS important Concepts
static and class method
'''static method : 

1.cannot access any properties
2. Can have no parameters at all.
Class method: 
1) Similar to static method but can use class properties .
2) Must have a reference to a class object as the first parameter’’’
class stat_test():
    var="private" #properties
    def method(self):
        return ("normal method")  
    @staticmethod
    def static_method(self):
            return(self.var)    
    @classmethod
    def class_method(self):
           return(self.var)
            
print(stat_test().method())   
#print(stat_test.static_method()) #error
print(stat_test.class_method())
print("#========#Access Modifiers(Encapsulation) DUNDEE =======")
private: "__" at the beginning and at most “_” at end
protected: "_" at the beginning and at most “_” at end (Convention only)
Restricted": "__a__" at beginning and at end

E.g.: print ("adasdsa".__len__()) #__name__ = inbuilt functions
'''
class a:
    pubv="pub"
    _prov="pro"
    __priv="priv"   
    def __init__(self):
        pass
    def pubm(self):
        return(self.pubv)
    def _prom(self):
        return(self._prov)      
    def __prim(self):
        return(self.__priv)         
 
class b(a):
    def __init__(self):
        pass 
    def priv(self):
        return(self.__priv)    
    def prov(self):
        return(self._prov)              
    def prim(self):
        return(self.__privm())
    def prom(self):
        return(self._prom())
    def pubm(self):
        return(super().pubm())

A=a()       
print(A.pubv,A._prov)
#print(A.__priv) # Error
print(A.pubm(),A._prom())
#print(A.__prim()) # Error

B=b()
#print(B.priv()) # Error
print(B.prov())
#print(B.prim()) # error
print(B.prom())
print("#====Constructor/Method Overriding and Inheritance===")

#Note : Overloading does not exist in python , only overriding exist
class Bird():
    name=None
    def __init__(self,name):
        self.name=name
    def bird_name(self):
        return(self.name)
    def fly(self):
        return("True")

class non_fly(Bird):
    swim=None
    def __init__(self,name,swim):        #Constr overriding
        super().__init__(name)              #call parent constr
       #  Bird.__init__(self, name) 
        self.swim=swim
   def swim(self):
        print(self.bird_name(),self.swim) #Method inheritance
    def fly(self):                                                  #Method Overrdiing
        return("false")

peng=non_fly("peng",True)
print(peng.bird_name(),peng.fly(),peng.swim)#inheritance
#O/P: peng false True
print("#==========#polymorphism===================")
#Polymorphism (Duck Typing)
'''In a duck-typed language (if it looks like a duck and quacks like a duck, it's a duck)
If the object  have the methods, then execute  else it raises a run-time error.’’’
class Duck():
   def quack(self):
      print ("Duck Quack")

class Mallard():
    def quack(self):
        print ("Mallard Quack")

#Polymorphism
for i in [Duck(), Mallard()]:
   i.quack()
print("#=============Abstract Class ==========")
#( At least 1 method is abstract,entire class = abstract class)
from abc import ABC, abstractmethod
class Abstract(ABC):
    @abstractmethod
    def foo(self):
        pass

#a=Abstract()#uncomment below to raise exception 
print("#=========Diamond problem============")
class Duck():
   def quack(self):
      print ("Duck Quack")
class Mallard():
    def quack(self):
        print ("Mallard Quack")
class c(Duck,Mallard):
                  def c(self):
                                    super().quack()
print(c().c()) #O/p = Duck Quack (as Duck appears 1st)
print("#=========super vs self vs class name============")
super()
1.  Used inside child constructor to call parent constructor
2.  Used inside overriding method to call the parent method
self
1. Used to call method and properties of the same class
2. Used to call methods and properties of parent class
3. Cannot be used inside overriding constructor or method as it creates infinite loop.
className
1. Can be used to call properties and methods of parent class.
2. Parent class name needs to be hardcoded
3. "self" needs to be sent as an arguement.
print(#=========multiple inheritance============")
class a():
    x="1"
    def a(self):
        return("2")
class b():
    y="3"
    def b(self):
        return("4") 
class c(a,b):
    z="5"
    def c(self):
        print(self.x,self.y,self.z,self.a(),self.b())
C=c()
C.c()
print(C.x,C.y,C.a(),C.b())

Sunday, March 4, 2018

Selenium IDe : All about Selenium IDe

All about Selenium IDe


Note : You can directly use xpaths in the IDE 
example :



About :
  1. Ide is best use to perform disposable fast and small repeataitve tasks .
  2. So basically we know Java Selenium does not come with record option like other automation tools so when you can use IDe to record steps and later export to Java / Python - TestNg/ Junit . And change the element locators value to required and use it in your code.
Note : You use below links to grab your  locator values :
http://catchbug.blogspot.in/2013/11/selenium-ide-tips-and-crash-refs.html
http://catchbug.blogspot.in/2016/03/selenium-how-to-create-xpath.html
http://catchbug.blogspot.in/2015/09/selenium-using-firefox-inbuilt.html




Common Commands

Command
Number of Parameters
Description
open
0 - 2
Opens a page using a URL.
click/clickAndWait
1
Clicks on a specified element.
type/typeKeys
2
Types a sequence of characters.
verifyTitle/assertTitle
1
Compares the actual page title with an expected value.
verifyTextPresent
1
Checks if a certain text is found within the page.
verifyElementPresent
1
Checks the presence of a certain element.
verifyTable
2
Compares the contents of a table with expected values.
waitForPageToLoad
1
Pauses execution until the page is loaded completely.
waitForElementPresent
1
Pauses execution until the specified element becomes present.

Find by text value
css=tag:contains("inner text")
css=button:contains("Create Product")

Syntax for Locator Usage
Method
Target Syntax
Example
By ID
id= id_of_the_element
id=email
By Name
name=name_of_the_element
name=userName
By Name Using Filters
name=name_of_the_element filter=value_of_filter
name=tripType value=oneway
By Link Text
link=link_text
link=REGISTER
Tag and ID
css=tag#id
css=input#email
Tag and Class
css=tag.class
css=input.inputtext
Tag and Attribute
css=tag[attribute=value]
css=input[name=lastName]
Tag, Class, and Attribute
css=tag.class[attribute=value]
css=input.inputtext[tabindex=1]
DOM
>getElementById
>getElementsByName
>dom:name
(applies only to elements within a named form)
>dom:index

document.forms[index of the form].elements[index of the element]

document.getElementsByName("name")[index]
document.forms[0].elements[”phone”]



Add loop in selenium IDE :
  1. Download this js file: https://github.com/darrenderidder/sideflow/blob/master/sideflow.js
  2. Launch Selenium IDE from Firefox and open the options menu.
  3. Upload the .js file to the "Selenium Core extensions (user-extensions.js)" field.
  4. Restart IDE 
  5. Make sure u modify and add below commands depending on your requirement
New Test
Command
Target
Value
open
http://docs.seleniumhq.org/

setSpeed
1000

store
1
MyVar
while
storedVars.MyVar <= 3

echo
${MyVar}

highlight
css=img[alt="Selenium Logo"]

store
javascript{storedVars.MyVar++;}

endWhile





 Ref :
https://www.guru99.com/introduction-selenuim-ide.html

https://stackoverflow.com/questions/11033321/how-to-loop-tests-in-selenium-ide
https://www.guru99.com/first-selenium-test-script.html