TESTEROPS

A pragmatic approach to QA and OPS

Simple Script Using Unittest

Let us have one of the most basic examples of a script using the unittest framework. We already have this script, as a part of our basic browser commands, that we studied.


import unittest
from selenium import webdriver

class logintest(unittest.TestCase):
  def setUp(self):
     self.driver=webdriver.Firefox()

  def test_filldetails(self):
     driver=self.driver
     driver.maximize_window()
     driver.get('http://www.toolsqa.com/')

     driver.implicitly_wait(30)

     title1=driver.title
     print(title1)

     print(len(title1))

     url1=driver.current_url

     print(url1)
     print(len(url1))

     driver.refresh()

     print(driver.page_source)
     print(len(driver.page_source))

  def tearDown(self):
     self.driver.quit()
if __name__ == '__main__':
  unittest.main()

Now, we need to understand this script. How we have arranged the creation of everything in one section, followed by testing the functionality in another section and then finally destroying the objects created in the script in final section.

The first section,

def setUp(self):
     self.driver=webdriver.Firefox()

helps us create an instance of Firefox browser. This is done before we go about testing about any functionality. This will be executed before each single test function that we would incorporate in this script. This is somewhat similar to @BeforeTest annotation that we use in TestNG.

The next section ,

def test_filldetails(self):
     driver=self.driver
     driver.maximize_window()
     driver.get('http://www.toolsqa.com/')

     driver.implicitly_wait(30)

     title1=driver.title
     print(title1)

     print(len(title1))

     url1=driver.current_url

     print(url1)
     print(len(url1))

     driver.refresh()

     print(driver.page_source)
     print(len(driver.page_source))

Creates a test region, which incorporates the functionality that we are going to test. This would include any Assertion, Verification, searching for element, performing any actions on them- like click, moving etc.
Always remember that these function always start with a test_ prefix. If you’re familiar with TestNG, this is somewhat similar with @Test annotation.

The final section,

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

This section helps us in code clean up. Once we are done with performing our tests, we don’t want the browser to lie there open, or to leave any object created unattended- for the sake of security and memory considerations. So in order to clean up that, we need to close or quit the browser instance. This section does the same thing as @AfterTest annotation would do in TestNG

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: