/
ch_5_alerts.py
50 lines (39 loc) · 1.5 KB
/
ch_5_alerts.py
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
"""
File name : alerts
Author : Aleksander Boldyrev
Python Version: 3.4
IDE : PyCharm Community Edition
ch_5
"""
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
import unittest
class AlertTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get('http://the-internet.herokuapp.com/')
def test_js_alert(self):
# click the JavaScript Alerts to open test-page
self.driver.find_element_by_link_text('JavaScript Alerts').click()
# wait for Alert Button to be visible
confirm_button = WebDriverWait(self.driver, 10)\
.until(expected_conditions.visibility_of_element_located(
(By.XPATH, "//li/button[text()='Click for JS Confirm']")))
# click on Alert Button,
# this will display an alert to the user
confirm_button.click()
# wait for the alert to present
alert = WebDriverWait(self.driver, 10)\
.until(expected_conditions.alert_is_present())
# get the text from alert
alert_text = alert.text
# check alert text
self.assertEqual('I am a JS Confirm', alert_text)
# click on Ok button
alert.accept()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)