diff --git a/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/Chrome.py b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/Chrome.py new file mode 100644 index 0000000..544411c --- /dev/null +++ b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/Chrome.py @@ -0,0 +1,123 @@ +import unittest + +from selenium.common import TimeoutException +from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from webdriver_manager.chrome import ChromeDriverManager +from selenium import webdriver +from selenium.webdriver.chrome.service import Service as ChromeService + +class ChromePositiveTests (unittest.TestCase): + + def setUp (self): + service = ChromeService (executable_path=ChromeDriverManager ().install ()) + self.driver = webdriver.Chrome (service=service) + self.driver.maximize_window () + + def test_chrome_TCP_112 (self): + driver = self.driver + print ( + " ") + print ( + " TESLA --> ENERGY --> UTILITIES ") + print ( + " ") + print ( + "---------------------------------------- POSITIVE TEST CASES ---------------------------------------") + print ( + " ") + print ( + " ") + print ( + " *** Test Case TC - 112 *** ") + print ( + " ") + # 1. Navigate to Tesla.com + driver.get ('https://www.tesla.com/') + self.driver.maximize_window () + + # 2. Click the "Energy" link in the main navigation bar + driver.implicitly_wait (5) + energy = driver.find_element(By.XPATH, "//span[contains(text(),'Energy')]") + energy.click() + + # 3. Click the "Utilities" link + driver.implicitly_wait (2) + utilities = driver.find_element(By.LINK_TEXT, "Utilities") + utilities.click() + + # 4. Verify that the page title matches the page content + try: + # Wait up to 10 seconds for the element to be present + # presence_of_element_located returns the WebElement if found + utilities_header = WebDriverWait (self.driver, 3).until ( + EC.presence_of_element_located ((By.XPATH, "//h1[contains(.,'Utilities')]")) + ) + + # Check for presence after waiting + self.assertIsNotNone (utilities_header, "Utilities header element was not found after waiting.") + print ("\nUtilities header is present on the page.") # Newline for cleaner unittest output + + except TimeoutException: + # If TimeoutException occurs, it means the element was not found within the timeout. + self.fail ("Utilities header was NOT found on the page within the timeout.") + except Exception as e: + # Catch any other unexpected exceptions + self.fail (f"An unexpected error occurred: {e}") + + print ("The browser correctly navigates to the Utilities page.") + + print ("Test passed: page is working as expected") + print ( + " *** TC-112 - Test Case TC - 112 PASSED *** ") + + def test_chrome_TCP_113 (self): + driver = self.driver + print ( + " ") + print ( + + " *** Test Case TC - 113 *** ") + + # 1. Navigate to Tesla.com + driver.get ('https://www.tesla.com/') + self.driver.maximize_window () + + # 2. Click the "Energy" link in the main navigation bar + driver.implicitly_wait (3) + energy = driver.find_element (By.XPATH, "//span[contains(text(),'Energy')]") + energy.click () + + # 3. Click the "Utilities" link + driver.implicitly_wait (3) + utilities = driver.find_element (By.LINK_TEXT, "Utilities") + utilities.click () + + + # 4. Click on the "Contact Us" button. + contactus = driver.find_element(By.CSS_SELECTOR, 'a[href="#contact-us"]') + contactus.click () + + + + + def tearDown (self): + self.driver.quit () + + + + if __name__ == '__main__': + unittest.main () + + + + + + + + + + + +# 6. Check if the main content for the "Utilities" page is displayed correctly. diff --git a/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/Helpers.py b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/Helpers.py new file mode 100644 index 0000000..e5699ff --- /dev/null +++ b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/Helpers.py @@ -0,0 +1,4 @@ + + +# Energy menu item +energy = \ No newline at end of file diff --git a/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/test_Chrome.py b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/test_Chrome.py new file mode 100644 index 0000000..ada4d03 --- /dev/null +++ b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/test_Chrome.py @@ -0,0 +1,27 @@ +import unittest +from selenium import webdriver +from selenium.webdriver.chrome.service import Service as ChromeService +from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait +from webdriver_manager.chrome import ChromeDriverManager + + + +class ChromePositiveTests(unittest.TestCase): + def setUp(self): + service = ChromeService (executable_path=ChromeDriverManager ().install ()) + self.driver = webdriver.Chrome (service=service) + self.driver.maximize_window () + + +def test_chrome_TCP_112(self): + driver = self.driver + # 1. Navigate to Tesla.com + driver.get('https://www.tesla.com/') + + + +# 3. Click on the "Utilities" option in the dropdown submenu. +# 4. Verify that the user is navigated to the correct page for the "Utilities" section. +# 5. Verify that the page title and URL correspond to the "Utilities" section. +# 6. Check if the main content for the "Utilities" page is displayed correctly. diff --git a/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/test_Utilities_Chrome.py b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/test_Utilities_Chrome.py new file mode 100644 index 0000000..3221165 --- /dev/null +++ b/02_Front_end_Testing/Utilities (Anton Buyanovski)/Unittest/test_Utilities_Chrome.py @@ -0,0 +1,337 @@ +import random +import time +import unittest + +from selenium import webdriver +from selenium.webdriver import ActionChains +from selenium.webdriver.chrome.service import Service as ChromeService +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.wait import WebDriverWait +from webdriver_manager.chrome import ChromeDriverManager + + +# from telnetlib import EC + + +# Import necessary modules and classes: +# - random, time: For introducing delays during actions. +# - unittest: To create and manage test cases. +# - selenium modules: For interacting with web elements and browser automation. +# - webdriver_manager: Simplifies the management of the Chrome driver executable. + +def delay(): + time.sleep(random.randint(2 , 4)) + + +# This function introduces a random delay between 2 and 4 seconds. +# It is used to simulate waiting for a page or element to load, but this approach can be unreliable. +# A better alternative would be to use WebDriver's explicit or implicit waits. + +class ChromeTests(unittest.TestCase): + # Define a test class that inherits from `unittest.TestCase`. + # This class contains setup, test cases, and teardown methods for browser-based tests. + + # Set up the test environment + def setUp(self): + print("Setting up resources...") + service = ChromeService(ChromeDriverManager().install()) + # Initialize a service instance for the Chrome WebDriver. + # `ChromeDriverManager().install()` downloads the appropriate driver version. + + options = webdriver.ChromeOptions() + # Create an instance of ChromeOptions to modify browser settings during WebDriver initialization. + + + self.driver = webdriver.Chrome(service=service, options=options) + # Start a Chrome WebDriver instance with the specified service and options. + + self.driver.maximize_window() + # Maximize the browser window for better visibility during testing. + + + def test_chrome_112(self): + # Define a test case named `test_chrome_112`. + + driver = self.driver + # Use the WebDriver instance created in the `setUp` method for browser interactions. + + driver.get("https://www.tesla.com/") + # Navigate to the Tesla homepage. + + delay() + # Introduce a random delay to wait for the page to load. + # This approach can be improved with dynamic waits like `WebDriverWait`. + + energy_menu= driver.find_element(By.XPATH, "//button[@id='dx-nav-item--energy']") + # Locate the "Energy" menu button using its XPath. + # The XPath addresses an element with the `id` attribute `dx-nav-item--energy`. + + action = ActionChains(driver) + # Create an instance of ActionChains to perform advanced user interactions. + + action.move_to_element(energy_menu).perform() + # Simulate a hover action over the "Energy" menu item. + + driver.find_element(By.XPATH, "//a[@href='/utilities']").click() + # Locate and click the "Utilities" submenu using its XPath. + + delay() + # Another random delay to wait for the "Utilities" page to fully load. + + driver.find_element(By.XPATH, "//h1[contains(.,'Utilities')]") + # Verify that the "Utilities" page has loaded by checking for an `

` tag + # containing the text "Utilities". + + if driver.current_url == "https://www.tesla.com/utilities": + # Check if the current page URL matches the expected "Utilities" page URL. + + print ("Test 112 - Passed, URL is correct") + # Output a success message if the URL validation passes. + + else: + print ("Test 112 - Failed, URL is incorrect", driver.current_url) + # If the URL validation fails, print a failure message along with the current URL. + + driver.quit() + # Close the browser session regardless of the test outcome to ensure cleanup. + + + def test_chrome_113(self): + # Define the test case `test_chrome_113`. + + driver = self.driver # Assign the WebDriver instance created in the setup phase to `driver` + # Assign the WebDriver instance initialized in the `setUp` method of the test class to the variable `driver`. + # This allows interaction with the browser session throughout the test. + + driver.get("https://www.tesla.com/") + # Navigate to the Tesla homepage. + + delay() + # Introduce a delay (randomized between 2 to 4 seconds) to allow the page to load fully. + # This should be replaced with explicit or implicit waits for better reliability. + + energy_menu = driver.find_element(By.XPATH, "//button[@id='dx-nav-item--energy']") + # Locate the "Energy" menu button on the Tesla homepage using its XPath. + # The XPath targets an element (`