Sunday, December 9, 2012

Safe escaping for HTML and URL


 >>> link = 'file.py?fname=a&lname=b&amp=c&sect=d&lt=e'  
 # escape for HTML   
 >>> import cgi  
 >>> cgi.escape(link)  
 'file.py?fname=a&jlname=b&amp=c&sect=d&lt=e'  
 # escape for URL   
 >>> import urllib.parse  
 >>> elink = urllib.parse.quote_plus(link)  
 >>> elink  
 'file.py%3Ffname%3Da%26lname%3Db%26amp%3Dc%26sect%3Dd%26lt%3De'  
 # URL satisfies HTML too: same   
 >>> cgi.escape(elink)  
 'file.py%3Ffname%3Da%26lname%3Db%26amp%3Dc%26sect%3Dd%26lt%3De'  

Monday, November 26, 2012

Configure vim for python

First important stuff after installation of ubuntu:
 sudo apt-get install vim python python-pip python-virtualenv git-core bash-completion  
Create in a home directory config for vim
 $touch ~/.vimrc  
or open global config to apply settings for all
 $sudo vim /etc/vim/vimrc  
and add the following parameters to this files.
 autocmd FileType python set omnifunc=pythoncomplete#Complete  
 autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS  
 autocmd FileType html set omnifunc=htmlcomplete#CompleteTags  
 autocmd FileType css set omnifunc=csscomplete#CompleteCSS  
 autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags  
 autocmd FileType php set omnifunc=phpcomplete#CompletePHP  
 autocmd FileType c set omnifunc=ccomplete#Complete  

Good how-to in russian 

Monday, November 5, 2012

Selenium with python

First of all ensure that python is installed on your PC.
Or you can download it from python.org, use v.2.7.
On ubuntu just run the following commands in terminal:
 $sudo apt-get install python python-setuptools  
 $sudo easy_install pip  
 $pip install selenium  
On windows install python and setup tools from windows installers:
python-2.7.3.msi
setuptools-0.6c11.win32-py2.7
Add PYTHONPATH to envirenmonet variables
 c:>easy_install pip  
 c:>pip install selenium  
Run python interpreter and check that all stuff was installed correctly:
 >python  
 >from selenium import webdriver #import selenium webdriver library  
 >browser = webdriver.Firefox() #start firefox  
 >browser.get('www.qa-notes.blogspot.com')  
You can explore all available methods by using the following command interactively:
 >for method in dir(webdriver): print(method)  
Then you can look at help by:
 >help(webdriver.<methodname>)  
Selenium Python API here selenium-python API
If you want to write robust and maintainable test, you can use 'unittest' module with webdriver
For example:
 import unittest  
 from selenium import webdriver  
 class firstTestSuiteTest(unittest.TestCase):  
   """initialize some stuff here"""  
   def setUp(self):  
     self.browser = webdriver.Firefox()  
   """clean some stuff here after each test"""  
   def tearDown(self):  
     self.browser.close()  
   """each test methods' name should be start with 'test_'"""  
   def test_PasswordValidation(self):  
     browser = self.browser  
     browser.get(URL + 'sing-up')  
     pwdbox = browser.find_element_by_id('passord')  
     pwdbox.sendkeys(pwdGenerator())  
     pwd.click()  
     self.assertIn "Incorrect passowrd", errorMessage  
 suite1 = unittest.TestLoader().loadTestsFromTestCase(firstTestSuiteTest)  
 suite2 = ....  
 suiteN = ....  
 """Prepare all suites all together"""  
 """  
 To skip some tests use anotations before method definition, e.g  
 @unittest.skip("demonstrating skipping")  
 def test_ThisTestWillBeSkipped(self):  
   Blah Blah Blah  
 """  
 fullTest = unittest.TestSuite([suite1, suite2, ..., suiteN])  
 unittest.TextTestRunner(verbosity=2).run(fullTest)  

Tuesday, October 30, 2012

Selenium headless with python

Headless selenium. Will be useful to integrate with CI

Install virtual display and python wrapper for it:
 $ sudo apt-get install xvfb xserver-xephyr  
 $ sudo pip install pyvirtualdisplay  
Usage example:
 from pyvirtualdisplay import Display  
 from selenium import webdriver  
 """visible=0, visible=1 for display or not virual display"""  
 display = Display(visible=0, size= (1024, 768))
 display.start()
 driver= webdriver.Chrome() 
 driver.get('http://www.qa-notes.blogspot.com')
 print 'The title of current page is: ', driver.title  
 driver.quit()
 display.stop()  

Saturday, October 27, 2012

Install Java in Ubuntu 12.04

If there is a problem with java in Ubuntu, remove previous version 
$sudo apt-get purge openjdk*
add new repo and install Oracle Java 7
$sudo add-apt-repository ppa:webupd8team/java
$sudo apt-get update
$sudo apt-get install oracle-java7-installer

Sunday, October 14, 2012

Just for me

///////
$ echo 'Andrii The Great' | hexdump -Cv
00000000 41 6e 64 72 69 69 20 54 68 65 20 47 72 65 61 74 |Andrii The Great|
00000010 0a |.|
00000011
$ echo -e '\x41\x6e\x64\x72\x69\x69\x20\x54\x68\x65\x20\x47\x72\x65\x61\x74'
Andrii The Great
: if you have huge amount of hex data to convert to binary---you can use xxd utility (part of vim package).
$ echo -e $( echo "30 31 323334 35 " | sed 's/ //g; s/../\\x&/g' )
////////