Open a web page using Selenium WebDriver
Preconditions (3)
✅ Expected Result: The browser opens and loads the page at 'https://example.com'. The current URL matches 'https://example.com'.
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import unittest class TestOpenURL(unittest.TestCase): def setUp(self): options = Options() options.add_argument('--headless') # Run browser in headless mode service = Service() # Assumes chromedriver is in PATH self.driver = webdriver.Chrome(service=service, options=options) def test_open_example_com(self): url = 'https://example.com' self.driver.get(url) current_url = self.driver.current_url self.assertEqual(current_url, url, f"Expected URL to be {url} but got {current_url}") def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test uses Python's unittest framework and Selenium WebDriver.
setUp(): Initializes Chrome browser in headless mode to run without opening a window. It uses Service() assuming chromedriver is in PATH.
test_open_example_com(): Calls get() with the URL 'https://example.com'. Then it checks the current URL matches exactly using assertEqual. This confirms the page loaded correctly.
tearDown(): Closes the browser after the test to free resources.
This structure is clear and follows best practices: setup, test, teardown. The assertion is simple and direct.
Now add data-driven testing to open three different URLs and verify each loads correctly.
driver.get(url) do in a test script?driver.get(url)driver.quit(), refreshing by driver.refresh(), and finding elements by driver.find_element().get(), used as driver.get(url).from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()get(), prints the page title, then closes the browser with quit().from selenium import webdriver
driver = webdriver.Firefox()
driver.get("www.example.com")
driver.quit()driver.get() multiple times in a row opens each URL sequentially in the same browser window.Pass the test and this pattern moves unknown