How to implement Selenium Webdriver with Python?
Selenium is an automation tool which is composed of multiple software tools like selenium IDE, web-driver, Selenium RC, Selenium grid.
Key features of Selenium includes:
1. Multiple browser support:Firefox, Chrome, Safari, Opera, Yes
2. Multiple OS support:Windows, OS X, Linux, Android and IOS
3. Multiple language support:Python, Ruby, Java
4. Free as in beer and freedom
Selenium supports different languages, here I would like to show you how to implement Selenium Web-driver with Python (Python 2.7).
Pip
It is a package management tool used to install and manage software package, written in Python.
Virtualenv
It provides an isolated working environment which allow us to work on a specific project without worrying about affecting other projects.
In Order to install it simply do,
1 | pip install virtualenv |
Other python packages
There are other python packages. These are also need to be installed
1.selenium
2.nose
In order to install this packages use the code
1.pip install selenium==2.40
2.pip install nose
We can use any OS, programming language and browser. Here I would like to show you how to implement Selenium Webdriver with Python.
- OS:Windows 8
- Python: python 2.7
- Browser: Firefox 28
- Selenium: Selenium 2.40
Writing a test
Automation testing performs the same functionalities as manual testing. But this is done with computer which allow you to do other works as well as easily repeat your tests. Test code runs a series of instruction to interact with web browser.
Write a sample test
For the cause, I am using a website http://demo.mahara.org using firefox. Make sure that title is “Home mahara” and also we can easily login using the respective credentials. Type the following code in the python shell and run it .
1.import unittest
2.from selenium import webdriver
3.from selenium.webdriver.common.keys import Keys
4.class DemoMaharaOrgLogin(unittest.TestCase):
5.def setUp(self):
#define a driver instance.for example firefox
6. self.driver=webdriver.Firefox()
7. def test_login_in_demo_mahara_org(self):
8. driver=self.driver
#navigate to the demo website
9. driver.get(“ http://demo.mahara.org//”)
#do assertion on the site title
10. self.assertIn(“Home-Mahara”, driver.title)
#define username web element
11.username=driver.find_element_by_id(“login_login_username”)
#enter username as student1
12.username.send_keys(“student1”)
13.password=driver.find_element_by_id(“login_login_password”)
#enter password as testing1
14.password.send_keys(“testing1”)
#define login button
15.loginbutton=driver.find_element_by_id(“login_submit”)
#click on the login button
16. loginbutton.click()
#verify that logout button displays
17. self.assertTrue(driver.find_element_by_link_text(“Logout”),”Logout link”)
18.def tearDown(self):
19. self.driver.close()
I hope this blog gives you a good insight about implementing Selenium Webdriver with Python. Feel free to contact for any queries.