Selenium with Python-unittest - Test returns Proce

2020-04-16 17:43发布

问题:

Could someone helping me to understand why the following code is executed, but no action is performed?

Returned code is 0 but browser is not opened or no action is performed.

it is worth to mention that setUp method has been configured in the same way in other modules and it works correctly. Please check at the end the response.


import unittest
from Init_load import set_up
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By


class HomeTest(unittest.TestCase):

    def setUp(self):
        con = set_up.Connect()
        self.driver = con.setUp()

        #self.link_list = ["Home", "flights", "Destinations", "CONTACT", "SIGN-ON", "REGISTER", "SUPPORT", "Hotels", \
        #                  "Car Rentals", "Cruises"]

        self.home = (By.LINK_TEXT, "Home")
        self.flights = (By.LINK_TEXT, "flights")
        self.destinations = (By.LINK_TEXT, "Destinations")
        self.contact = (By.LINK_TEXT, "CONTACT")
        self.sign_on = (By.LINK_TEXT, "SIGN-ON")
        self.register = (By.LINK_TEXT, "REGISTER")
        self.support = (By.LINK_TEXT, "SUPPORT")
        self.hotels = (By.LINK_TEXT, "Hotels")
        self.car_rentals = (By.LINK_TEXT, "Car Rentals")
        self.cruises = (By.LINK_TEXT, "Cruises")

    def test_homeLink(self):
        self._elementWait(By.LINK_TEXT, "home")
        self.driver.find_element(*self.home).click()

    def test_flight_link(self):
        self._elementWait(By.LINK_TEXT, "flights")
        self.driver.find_element(*self.home).click()

    def test_destinations_link(self):
        self._elementWait(By.LINK_TEXT, "flights")
        self.driver.find_element(*self.destinations).click()

    def test_contact_link(self):
        self._elementWait(By.LINK_TEXT, "flights")
        self.driver.find_element(*self.contact).click()

    def test_signOn_link(self):
        self._elementWait(By.LINK_TEXT, "SIGN-ON")
        self.driver.find_element(*self.contact).click()

    def test_register_link(self):
        self._elementWait(By.LINK_TEXT, "REGISTER")
        self.driver.find_element(*self.register).click()

    def test_support_link(self):
        self._elementWait(By.LINK_TEXT, "SUPPORT")
        self.driver.find_element(*self.support).click()

    def test_hotels_link(self):
        self._elementWait(By.LINK_TEXT, "hotels")
        self.driver.find_element(*self.hotels).click()

    def test_carRentals_link(self):
        self._elementWait(By.LINK_TEXT, "Car Rentals")
        self.driver.find_element(*self.car_rentals).click()

    def test_cruises_link(self):
        self._elementWait(By.LINK_TEXT, "cruises")
        self.driver.find_element(*self.cruises).click()

    def _elementWait(self,how, what):
        WebDriverWait(self.driver, 12).until(expected_conditions.element_to_be_clickable((how, what)), \
                                               "Element not loaded yet")

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

This is the response:

/home/osboxes/PycharmProjects/Automation/venv/bin/python /home/osboxes/PycharmProjects/Automation/unit_test/home_test.py

Process finished with exit code 0

Other scripts with exactly the same setUp() function are executed correctly but no this one. Ive tried a lot of options, but nothing works

回答1:

Are you calling your unittest with:

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

If you do not, then you are just defining the test, not actually executing anything.