-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_search1.py
More file actions
108 lines (77 loc) · 3.3 KB
/
test_python_search1.py
File metadata and controls
108 lines (77 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
class ChromeSearch(unittest.TestCase):
"""
This class contains automated test cases for searching on the Python website using Selenium and Chrome webdriver.
"""
def setUp(self):
"""
This method initializes the Chrome webdriver and sets it up for the test cases.
"""
print("Initializing Chrome webdriver...")
self.driver = webdriver.Chrome()
def test_search_in_python_org_enter(self):
"""
This test case searches for "matplotlib" on the Python website using the search bar and hitting Enter.
"""
print("Testing search with Enter key...")
# Get the Chrome webdriver instance
driver = self.driver
# Open the Python website
driver.get("https://www.python.org")
# Verify the page title contains "Python"
self.assertIn("Python", driver.title)
# Find the search bar element using its name attribute
elem = driver.find_element(By.NAME, "q")
# Enter "matplotlib" in the search bar
elem.send_keys("matplotlib")
# Simulate pressing Enter to submit the search
elem.send_keys(Keys.RETURN)
# Wait for 3 seconds for the page to load
time.sleep(3)
# Assert that the current URL matches the expected search results URL
expected_url = "https://www.python.org/search/?q=matplotlib&submit="
actual_url = driver.current_url
self.assertEqual(actual_url, expected_url)
print("Search with Enter key test passed!")
def test_search_in_python_org_go(self):
"""
This test case searches for "python3" on the Python website using the search bar and clicking the "Go" button.
"""
print("Testing search with Go button...")
# Get the Chrome webdriver instance
driver = self.driver
# Open the Python website
driver.get("https://www.python.org")
# Verify the page title contains "Python"
self.assertIn("Python", driver.title)
# Find the search bar element using its name attribute
elem = driver.find_element(By.NAME, "q")
# Enter "python3" in the search bar
elem.send_keys("python3")
# Find the "Go" button element using its ID attribute
button = driver.find_element(By.ID, "submit")
# Click the "Go" button to submit the search
button.click()
# Wait for 3 seconds for the page to load
time.sleep(3)
# Assert that the current URL matches the expected search results URL
expected_url = "https://www.python.org/search/?q=python3&submit="
actual_url = driver.current_url
self.assertEqual(actual_url, expected_url)
print("Search with Go button test passed!")
def tearDown(self):
"""
This method closes the Chrome webdriver instance after each test case.
"""
print("Closing Chrome webdriver...")
self.driver.close()
if __name__ == "__main__":
"""
This block checks if the script is run directly (not imported as a module) and executes the test cases.
"""
print("Running test suite...")
unittest.main()