01. Intro
The selenium WebDriver API provides a class to handle JavaScript alerts.
02. Method / Property
Method / Property | Name | Description | Example |
Property | text | This gets a text from the alert window | alert.text |
Method | accept() | This will accept the alert box that is clicked on the OK button | alert.accept() |
Method | dismiss() | This will accept the alert box that is clicked on the Cancel button | alert.dismiss() |
Method | send_keys(*value) | This simulates typing into the element | alert.send_keys('test') |
03. TestCase
04. Code
1. The function: test_a_search_product
def test_a_search_product(self):
# get the search textbox
search_field = self.driver.find_element(By.ID, 'search')
search_field.click()
# Enter keyword and submit
search_field.send_keys('Phone')
search_field.submit()
2. The function: test_compare_products_add
def test_compare_products_add(self):
# search_product = self.driver.find_element(By.LINK_TEXT, 'Madison Overear Headphones')
self.driver.find_element(By.LINK_TEXT, 'Add to Compare').click()
compare_product = self.driver.find_element(By.XPATH, '//*[@id="compare-items"]/li/p/a').text
self.assertEqual('MADISON OVEREAR HEADPHONES', compare_product)
3. The function: test_compare_products_removal_alert
def test_compare_products_removal_alert(self):
# click on Remove all btn
self.driver.find_element(By.LINK_TEXT, 'Clear All').click()
# switch to the alert
alert = self.driver.switch_to.alert
# get the text from alert
alert_text = alert.text
# check alert text
self.assertEqual('Are you sure you would like to remove all products from your comparison?', alert_text)
# click on Cancle button
alert.dismiss()
self.driver.find_element(By.LINK_TEXT, 'Clear All').click()
# click on OK button
alert.accept()
# check if compare products is empty
emp_text = self.driver.find_element(By.CLASS_NAME, 'empty').text
self.assertEqual("You have no items to compare.", emp_text)
05. Result
Reference: Learning Selenium Testing Tools with Python by Unmesh Gundecha
'Testing Automation > 01. Selenium' 카테고리의 다른 글
07. Selenium | Data-driven testing (0) | 2022.03.09 |
---|---|
06. Selenium | Wait ( Implicit / Explicit) (0) | 2022.03.05 |
04. Selenium | Browser navigation (0) | 2022.03.04 |
03. Selenium | Browser control (0) | 2022.01.14 |
02. Selenium | Finding Element (0) | 2022.01.08 |