Wednesday, December 28, 2011

pyserial port scan

if you are using pyserial module to read the communication from your device on UART , you will have to know which port the is your system connected to.

the following is a simple code to scan through all the COM ports and find out which com port is in opened. at the end of the program , you will see the serial uart that is in use being printed

import pyserial
for i in range(256):
try:
s = serial.Serial(i)
available.append( (i, s.portstr))
port = i
s.close()
except serial.SerialException:
pass

ser = serial.Serial(port, 115200,timeout=1)
print ser.portstr


happy scripting!

Tuesday, December 27, 2011

Process kill

In python if we need to kill a running process we need to run the following command.

import os
os.system("taskkill /im "process_name" /f")

just give the name of the process that is running and it should kill it. we need this kind of setup when we try to kill all the existing process before launching a one.

Thursday, December 22, 2011

Python File Operations

Alright ,it is time for python file operation related examples. there could be a variety of things that you would like to do with a file. I am sharing a simple example below.

old_file= open ("file_Path","r")
new_file = open ("file_Path","w")

for line in old_file:
if "" in line:
new_file.write(line.replace("oldtext","newtext"))
else:
new_file.write(line)

In the above example we are opening a file in read and write mode and performing some basic operations.

Based on a condition we are trying to replace some content in the file and writing the new thing in a different file.

this code works as is , you just have to give the appropriate values. sys is the only module you will be needing.

Tuesday, December 20, 2011

Python with HP Quality center

As promised, check the code below will let you connect to QC
the below walks you through the test tree and updates the status of test cases.
you can use this as a function can call it in python script to update.
this was pretty useful to me , hops is the same case with you.

import win32com
from win32com.client import Dispatch

qcServer = "http://xxxxxxxxx/qcbin/"
qcUser = "xxxxx"
qcPassword = "xxxxx"
qcDomain = "xxxx"
qcProject = "xxxx"

t = win32com.client.Dispatch("TDApiOle80.TDConnection.1")
t.InitConnectionEx(qcServer)
t.Login(qcUser,qcPassword)
t.Connect(qcDomain,qcProject)

print "Logged in"

mg=t.TreeManager
npath="Root\xxxxxx"
tsFolder = t.TestSetTreeManager.NodeByPath(npath)
print tsFolder
tfactory=tsFolder.TestSetFactory
td_tsff=tfactory.Filter
td_testset=td_tsff.NewList()

for otest in td_testset:
print otest.Name
td_TSTestSetFactory = otest.TSTestFactory
td_tstsff = td_TSTestSetFactory.NewList("")


for otestitem in td_tstsff:
print otestitem.Name
td_RunFactory = otestitem.RunFactory
obj_theRun = td_RunFactory.AddItem("testrun")
obj_theRun.Status = "Passed"
otestitem.Status= "Passed"
obj_theRun.Post()
print otestitem.Status
next
next

tsFolder = None
tfactory = None
td_testset = None
td_TSTestSetFactory = None
td_RunFactory = None
obj_theRun = None
t.logout
t = None

print "done"

Monday, December 19, 2011

Python with Quality center

We fall back to the topic of test management . we can use all the scripting languages in the world but it boils down to the point on how will you report the status of the testing to your management . well that is true for almost every testing activity manual or automation.

I was wondering what to use , could it be a custom python script to prepare a report or off the shelf product . after going through a bunch of processes , Proof of concepts I came to a conclusion that we can write something in relatively less time but maintaining it is a problem. when I looked at any test management tool that supports python , I came across Quality Center. they have a OTA framework which makes is relatively simple to access their APIs. I spent time on it and found relatively simple to connect with the com objects.

my suggestion , if you have access to QC in your company , give it a shot , should be simple and straight forward for you to use.

I will be posting how to connect to QC from a python script pretty soon.

Tuesday, December 6, 2011

PyAuto

Today , i wanted to share about a module that you can use for testing windows application.
you reach a point where , you will have to use windows based application testing and you will run after tools which support them , the good thing about python is it has a pyauto module which you can use for the same . This module helps us running all the ui related tests . you might question "what is the advantage?" the answer is if you are truly comparing it with any ui automation tool "Nothing" . having said that , we all reach a point where , we will have to use scripting language across the board and you will have to have multiple tools being used. python has it all , so be it ui , embedded or application , this does it all !

so before you choose , give it a thought :)

Friday, December 2, 2011

Escape characters python

The one place everybody who starts scripting gets stuck is escape characters . this is about the point where you will have to give either a absolute or relative path from where you will have to open a file , exe etc.

one aspect which i was particularly glad about in python is , python has a inbuilt function to do this (kewl right! )

re.escape(string)

Example : print re.escape("www.example.com")

www\\.example\\.com is your ouput