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)  

No comments:

Post a Comment