0

Trying to run the below Selenium code in pycharm, the test is getting executed with no errors but the functions defined inside the class GoogleSearch is not being executed. Need help in finding what is the issue!!

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import unittest


class GoogleSearch(unittest.TestCase):
    print("123")

    @classmethod
    def setUpClass(cls):
        print("456")
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

    def test_search_automationstepbystep(self):
        self.driver.get("https://google.com")
        self.driver.find_element_by_name("q").send_keys("Automation Step by Step")
        self.driver.find_element_by_name("btnK").send_keys(Keys.ENTER)

    def test_search_kiranmallaiah(self):
        self.driver.get("https://google.com")
        self.driver.find_element_by_name("q").send_keys("Kiran Mallaiah")
        self.driver.find_element_by_name("btnK").send_keys(Keys.ENTER)

    @classmethod
    def tearDownClass(cls):
        cls.time.sleep(3)
        cls.driver.close()
        cls.driver.quit()


print("Test Completed")

The below is the output:

C:\Users\Indrani\PycharmProjects\SeleniumProject\venv\Scripts\python.exe "C:/Users/Indrani/PycharmProjects/SeleniumProject/Sample Projects/GoogleSearchTest.py"
123
Test Completed

Process finished with exit code 0
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

-1

There are a couple of things you need to consider while using

  • The unittest module is extensively used for constructing and running testcases.
  • A testcase is created by subclassing unittest.TestCase. The tests are defined with methods whose names start with the letters test.
  • The setUp() and tearDown() methods allow you to define instructions that will be executed before and after each test method.

However you missed the most important part to set the __name__ variable to have a value and define which test functions to execute. In order to configure those you need to add a couple of lines as follows:

  • 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.

So your effective code block will be:

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import unittest


class GoogleSearch(unittest.TestCase):
    print("123")

    @classmethod
    def setUpClass(cls):
        print("456")
        cls.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

    def test_search_automationstepbystep(self):
        self.driver.get("https://google.com")
        self.driver.find_element_by_name("q").send_keys("Automation Step by Step")
        self.driver.find_element_by_name("btnK").send_keys(Keys.ENTER)

    def test_search_kiranmallaiah(self):
        self.driver.get("https://google.com")
        self.driver.find_element_by_name("q").send_keys("Kiran Mallaiah")
        self.driver.find_element_by_name("btnK").send_keys(Keys.ENTER)

    @classmethod
    def tearDownClass(cls):
        cls.time.sleep(3)
        cls.driver.close()
        cls.driver.quit()

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

print("Test Completed")

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352