Skip to content

Add selenium tests for the home page #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added SeleniumTests/data/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions SeleniumTests/data/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class TestData:
URL = "https://zedd-weather.vercel.app/"
DRIVER_PATH=r"./driver/chromedriver.exe"
city_name="Paris"
invalid_location="1234"



Binary file added SeleniumTests/driver/chromedriver.exe
Binary file not shown.
Empty file added SeleniumTests/pages/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
44 changes: 44 additions & 0 deletions SeleniumTests/pages/home_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pages/home_page.py
import time
from selenium.webdriver.common.by import By

class HomePage:
def __init__(self, driver):
self.driver = driver
self.weather_button = (By.XPATH, "//button[contains(text(), \"today's weather\")]")
self.location_input = (By.ID, 'defaultLocation')
self.save_button = (By.CLASS_NAME, "swal2-confirm")
self.weather_details = (By.ID, 'weatherContainer')
# self.error_message = (By.CLASS_NAME, 'error-message')
self.settings_button=(By.XPATH,"//section[@class='footer-settings-section brand-text-mute' and text()='Settings']")
self.restore_button=(By.CLASS_NAME,"shadow")

def reset(self):
self.driver.find_element(*self.settings_button).click()
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
self.driver.find_elements(*self.restore_button)[2].click()



def click_weather_button(self):
# Scroll down a fixed amount (if element is near the bottom of the screen)
# Scroll to the bottom of the page
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
self.driver.find_element(*self.weather_button).click()



def enter_location(self, location):
self.driver.find_element(*self.location_input).clear()
self.driver.find_element(*self.location_input).send_keys(location)

def click_save_button(self):
self.driver.find_element(*self.save_button).click()

def get_weather_details_text(self):
return self.driver.find_element(*self.weather_details).text

#def get_error_message_text(self):
# return self.driver.find_element(*self.error_message).text
Empty file added SeleniumTests/tests/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
80 changes: 80 additions & 0 deletions SeleniumTests/tests/home_page_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# tests/home_page_tests.py
from data.test_data import TestData
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from pages.home_page import HomePage

# Test Execution
if __name__ == '__main__':
# Set up the driver
service = Service(TestData.DRIVER_PATH)
driver = webdriver.Chrome(service=service)
driver.get(TestData.URL)

# Create an instance of the HomePage class
home_page = HomePage(driver)

# Store test results
test_results = []

# Test Case 1.1: Verify weather display for a valid location entry
try:
home_page.click_weather_button()
home_page.enter_location(TestData.city_name) # Valid location
home_page.click_save_button()
time.sleep(2) # Wait for page load (adjust as necessary)
assert TestData.city_name in home_page.get_weather_details_text(), f"Weather info for {TestData.city_name} not found."
print("Test Case 1.1 Passed: Weather display for valid location.")
test_results.append("Test Case 1.1 Passed")
except Exception as e:
test_results.append(f"Test Case 1.1 Failed: {e}")

# Test Case 1.2: Verify the application's response to an invalid location entry
try:
home_page.reset()
home_page.click_weather_button()
home_page.enter_location(TestData.invalid_location) # Invalid location
home_page.click_save_button()
time.sleep(2) # Wait for page load (adjust as necessary)
assert "Invalid location" in driver.page_source, "Error message for invalid location not found."
print("Test Case 1.2 Passed: Error message for invalid location.")
test_results.append("Test Case 1.2 Passed")
except Exception as e:
test_results.append(f"Test Case 1.2 Failed: {e}")

# Test Case 1.3: Verify handling of the maximum allowed characters for location input
try:
home_page.reset()
home_page.click_weather_button()
max_location = 'A' * 50 # Adjust this based on the max allowed length (e.g., 50 characters)
home_page.enter_location(max_location)
home_page.click_save_button()
time.sleep(2) # Wait for page load (adjust as necessary)
assert max_location in home_page.get_weather_details_text(), "Weather info for max character input not found."
print("Test Case 1.3 Passed: Weather display for location with max characters.")
test_results.append("Test Case 1.3 Passed")
except Exception as e:
test_results.append(f"Test Case 1.3 Failed: {e}")

# Test Case 1.4: Verify the application's behavior when entering a location exceeding the max allowed characters
try:
home_page.reset()
home_page.click_weather_button()
long_location = 'A' * 51 # Adjust based on max allowed length (e.g., 51 characters)
home_page.enter_location(long_location)
home_page.click_save_button()
time.sleep(2) # Wait for page load (adjust as necessary)
assert "Input is too long" in driver.page_source, "Error message for exceeding max character input not found."
print("Test Case 1.4 Passed: Error message for location exceeding max characters.")
test_results.append("Test Case 1.4 Passed")
except Exception as e:
test_results.append(f"Test Case 1.4 Failed: {e}")

# Quit the driver
driver.quit()

# Summary of test results
print("\nTest Results Summary:")
for result in test_results:
print(result)