-1

What is the meaning of lines 3,16,17,18 and 19 which are highlighted with *. Can someone explain what they do? I am new to python and programming

import unittest

from selenium import webdriver

**class Iframe(unittest.TestCase):**

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

def test_Iframe(self):
    driver = self.driver
    driver.maximize_window()
    driver.get('http://www.toolsqa.com/iframe-practice-page/')

    iframe1 = driver.find_element_by_id('IF1')
    driver.switch_to.frame(iframe1)

    driver.find_element_by_name('email').send_keys('xyz')

    driver.switch_to.default_content()

    list = driver.find_elements_by_tag_name('iframe')

    print(len(list))

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


**if __name__ == '__main__':
   unittest.main()**
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Aayush
  • 129
  • 2
  • 8

2 Answers2

3

Only three lines in this code are highlighted with an *, but here's what they mean:

First line:

 class Iframe(unittest.TestCase):

This is declaring the class for the functions (test_Iframe and tearDown) that follow. A class is used to create "objects" in object oriented programming. Think of the class as the abstraction of data/procedures, while the object is the particular instance of the class.

Next line:

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

This section first declares a function with the def keyword, and the function quits the driver, which was set as:

driver = self.driver
driver.maximize_window()
driver.get('http://www.toolsqa.com/iframe-practice-page/')

in the test_Iframe() function.

Final line:

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

This section simply executes the main function of the program. More details on this can be found here.

Let me know if you have any more questions!

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
2

As you have choosen to use Python’s unittest here is the relevant info:

  • import unittest: You need to import the required unittest module as a mandatory measure.
  • class Iframe(unittest.TestCase):: The testcase class is inherited from unittest.TestCase. Inheriting from TestCase class is the way to tell the unittest module that this is a testcase.
  • def setUp(self):: The setUp is the part of initialization and this method will get called before every test function which you are going to write in this testcase class.
  • def test_Iframe(self):: This is the actual testcase method. The testcase method should always start with the characters test.
  • def tearDown(self):: The tearDown method will get called after every test method. This is the method to do all the cleanup actions.
  • if __name__ == '__main__':: This line sets the __name__ variable to have a value "__main__". If this file is being imported from another module then __name__ will be set to the other module's name. You will find a detailed discussion in What does if name == "main": do?
  • unittest.main(): Invokes the test functions from the configured module.

Note A: For more details see Using Selenium to write tests and Walk through of the example


Note B: Refer A module's name for complete details.


Why self

The first argument of every class method, including init is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object while in other class methods, it refers to the instance whose method was called.


Trivia

The self variable in python explained

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi Debanjan, Why do we use self here? – Aayush Mar 30 '18 at 03:38
  • @Aayush Check out my answer update and let me know if you have any questions. – undetected Selenium Mar 30 '18 at 05:16
  • Debanjan, Thanks for the explanation. Can you please site an example for this.How this works and why it is necessary? As I am a newbie in programming.I don't have any prior knowledge of any language and OOPs and I am not able to fully understand the content in books. – Aayush Mar 30 '18 at 06:01
  • Hi Debanjan, This link https://pythontips.com/2013/08/07/the-self-variable-in-python-explained/ was really helpful. Do u have a same type of link for Unittest?? – Aayush Mar 31 '18 at 05:04
  • @Aayush Go through the links in _Note A_ and _Note B_ which covers the basic understandings. – undetected Selenium Mar 31 '18 at 10:28
  • Hi Debanjan, I have one doubt in below lines "class Iframe(unittest.TestCase): : The testcase class is inherited from unittest.TestCase. Inheriting from TestCase class is the way to tell the unittest module that this is a testcase." In this , can't we just write like this - class Iframe(TestCase)? – Aayush Apr 09 '18 at 01:27