Saturday, October 31, 2015

API testing: SoapUI property transfer using Groovy scripting

//Importing XML module of SoapUI tool
import com.eviware.soapui.support.XmlHolder



//Access the Get_Employee Response XML and get the value of name tag.
def response=context.expand('${GetEmployee#response}')
def xml=new XmlHolder(response)
def namenode = xml.getNodeValue("//ns:name")
log.info namenode
context.setProperty("name","$namenode")
log.info context.getProperty("name")

//Access the Delete Employee Request XML and store the value grabbed from the Get Employee Response
def requestt = context.expand('${DeleteEmployee#request}')
def xmlreq=new XmlHolder(requestt)
xmlreq.setNodeValue("//typ:guestName", namenode)

//Formatting the xml and put it in the Testcase, Teststeps level 
def dxml = xmlreq.getXml()
testRunner.testCase.testSteps["DeleteEmployee"].setPropertyValue("request",dxml)

Monday, October 26, 2015

Python requests and grab values from json response

__author__ = 'valli_000'

import requests

r = requests.get('http://api.fixer.io/latest?base=USD')
data = r.json()
dict = data['rates']
for key,value in dict.iteritems():
    print "US Dollar Conversion of " + str(key) + " is " + str(value)

Wednesday, October 14, 2015

Sikuli: check multiple of the same/duplicate images and choose the image to perform action

def by_y(match):

   return match.y


icons = findAll("Capture.PNG")



# sort the icons by their y coordinates and put them into a new variable sorted_icons

sorted_icons = sorted(icons, key=by_y)

# another shorter version is using lambda.

print sorted_icons[3]


# Iterate the images and hover it - To confirm

for icon in list(sorted_icons)[:2]:

    hover(icon)


#Click the 4th image from the list.

click(list(sorted_icons)[4])

Saturday, October 3, 2015

Groovy scripting: Class

Bank.log=log
log.info("Hello World!")
Bank obj1 = new Bank()
Bank obj2 = new Bank()

obj1.accountnumber=1235
obj1.accounttype="Savings"
obj2.accountnumber=54321
obj2.accounttype="Current"

//log.info obj1.accountnumber + " " + obj1.accounttype
//log.info obj2.accountnumber + "  " + obj2.accounttype
log.info obj1.printaccount()



class Bank{

def static log
def accountnumber
def accounttype

public void printaccount(){
log.info("Your account type is $accounttype and number is $accountnumber")
}
}


Output:

Sat Oct 03 15:54:38 IST 2015:INFO:Hello World!
Sat Oct 03 15:54:38 IST 2015:INFO:Your account type is Savings and number is 1235
Sat Oct 03 15:54:38 IST 2015:INFO:null

Friday, October 2, 2015

Python: Encapsulation

Sample program

class Animal:
    def __init__(self, Animalname):
        self.__Animalname = Animalname

    def name(self):
        return self.__Animalname



x = Animal("This")
print x.name()

t = Animal("Pet2")
print t.__Animalname


Output:

C:\Python27\python.exe E:/Scripts_to_learn/Scripts_to_learn/Encapsulation.py

This

Traceback (most recent call last):
  File "E:/Scripts_to_learn/Scripts_to_learn/Encapsulation.py", line 17, in <module>
    print t.__Animalname
AttributeError: Animal instance has no attribute '__Animalname'


Notes:

1. To make the attribute private, use "__". So that it can't be accessed from outside
2. To know/use the private attribute, create a method as above (Eg: Above 'name' method)



Python polymorphism

Note
http://stackoverflow.com/questions/3724110/practical-example-of-polymorphism

Example:

#__author__ = 'valli_000'


class Animal(object):
    def __init__(self, Animalname):
        self.Animalname = Animalname

    def talk(self):
        pass


class Dog(Animal):
    def __init__(self, Animalname):
        super(self.__class__, self).__init__(Animalname)

    def talk(self):
        print "Sound of Animal " + self.Animalname + " is Bow Bow !"


class cat(Animal):
    def __init__(self, Animalname):
        super(self.__class__, self).__init__(Animalname)

    def talk(self):
        print "Sound of Animal " + self.Animalname + " is Meow Meow !"


x = cat("Pet 1")
x.talk()

y = Dog("Pet 2")
y.talk()


output:
C:\Python27\python.exe C:/Users/valli_000/PycharmProjects/Selflearning/Polymorphism.py

Sound of Animal Pet 1 is Meow Meow !
Sound of Animal Pet 2 is Bow Bow !

Process finished with exit code 0

Python Inheritace (accessing the init variables of base class from child class)


>>> class Baseclass(object):
...     def __init__(self, number):
...             self.number = number
...
...
>>> class childclass(Baseclass):

...     def __init__(self, number, number2):
...             self.number2 = number2
...             super(self.__class__, self).__init__(number)
...     def add(self):
...             return self.number + self.number2

>>> c = childclass(2,3)
>>> c.add()
5

Here, Accessing the init variables of base class from inside the child class  (i.e use the values of base class inside the child class)


Reference: http://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods



Case 2:  (Here we are accessing the parent variables from the child object, not from the child class/methods)

class base:
...     var1 = "Base class"
...     def __init__(self, number):
...             self.number = 5
...

class child(base):
...     var2 = "child class"
...     def add(self):
...             pass



Output:

 y = child(3)
>>> y.number
5

Python: Method overriding

Python Method overriding:

Parent class:

class pclass:
...     var1="Im in parent class"
...     def add(self, number1, number2):
...             return number1 + number2


Child Class:


class cclass(pclass):
...     var1="Im in parent class"
...     def add(self, number1, number2, number3):

...             return number1 + number2+number3


Here, we override the "add" method of parent class in the child class. See the output


Output:

>> c = class()
>> c.add(1,2,3)
>> 6

So, it uses only the add method of the child class, not the parent class


Note: Child class is also called as sub class


Why overriding is required?

One reason for overriding parent's methods is because you may want special or different functionality in your subclass.