Webtesting using selenium python

Hope you guys have basic knowledge in selenium? yep..that’s enough for this.

I am using linux as testing environment.. the procedures will be same in windows or other platforms, but the installation process will be different for each platforms.

Prerequisite

1. Install python using – most of the cases this is not needed, should be having python installed in all linux versions. If so just update to latest version.

$ wget http://www.python.org/ftp/python/2.7/Python-2.7.tgz

$tar xzf Python-2.7.tgz $ cd Python-2.7

$ ./configure –with-pth –with-dec-threads –with-signal-module –prefix=/opt/python-2.7

$ make # make install

2. Install selenium

$ pip install -U selenium

For windows user simply follow this http://seleniumhq.org/docs/appendix_installing_python_driver_client.jsp

3. Download python selenium RC driver from seleniumhq.org (if you are using selenium RC for testing)
http://selenium.googlecode.com/files/selenium-server-standalone-2.29.0.jar

4. Download and install selenium ide for firefox.
http://seleniumhq.org/projects/ide/
http://seleniumhq.org/download/

Ok now we are ready to do the BDT with selenium python.

If you don't have good knowledge in Python, just use selenium ide to create the webdriver scripts.

1. Open firefox and open selenium IDE.

lets start a simple test with  google.com ;)

- Click on new testcase
- Start recording in selenium ide
- Go to google.com
- Enter a search term "Testing"
- in the search results page.. lets put a verification point? what?...hmm easiest page title :)
- Right click in the page and get the show all available commands.
- Add a verification point AKA checkpoint for Title [Here it's testing-Google Search.]
- You can see each iterations in Selenium IDE.
- The default selenium IDE scripting language is html, we have to change it to Python webdriver/RC test.
- In the selenium IDE navigate to options menu >> format >> (Default is html) change to Python webdriver. 
- Copy the code to a and save the file as google_test.py

Here is the code what i have got from Selenium IDE.
---------------------------------------------------------------------

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class GoogleTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.co.in/"
        self.verificationErrors = []
        self.accept_next_alert = true

    def test_google(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("Testing")
        try: self.assertEqual("testing - Google Search", driver.title)
        except AssertionError as e: self.verificationErrors.append(str(e))

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert.text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
--------------------------------------------------------------------

Update the testcase like this.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class GoogleTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "https://www.google.co.in/"

    def test_google(self):
        driver = self.driver
        driver.get(self.base_url + "/")
    assert "Google" in driver.title

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

---------------------------------------
Run it from command prompt using

$ python google_test.py 

You will get a result like this.

.
----------------------------------------------------------------------
Ran 1 tests in 6.133s

OK
-----------------------------

Lets do a negative assertion and find the result.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class GoogleTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "https://www.google.co.in/"

    def test_google(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        //I have updated assert to GoogleTest.
        assert "GoogleTest" in driver.title 

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()
----------------------------------
Run it again from console.
$ python google_test.py 
You should get a failure result as the page title is different.

F
======================================================================
FAIL: test_google (__main__.GoogleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "google_test.py", line 16, in test_google
    assert "GoogleTest" in driver.title
AssertionError
----------------------------------------------------------------------
Ran 1 tests in 8.028s

FAILED (failures=1)

--------------------------------------------------------------------------

You will get more information from these docs about Python-Selenium

http://selenium-python.readthedocs.org/en/latest/index.html
http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html

No images available.