-
Notifications
You must be signed in to change notification settings - Fork 5
/
Session13_Alert_Dialog_Snackbar_Tooltip.py
112 lines (98 loc) · 4.37 KB
/
Session13_Alert_Dialog_Snackbar_Tooltip.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
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
109
110
111
112
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.action_chains import ActionChains
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.maximize_window()
driver.implicitly_wait(3)
alert = Alert(driver)
actions = ActionChains(driver)
############## Alert ###############
# 1) Alert - Get text
driver.get("https://the-internet.herokuapp.com/javascript_alerts")
driver.find_element(By.XPATH, "//button[text()='Click for JS Alert']").click()
print(alert.text)
sleep(3)
# 2) Alert - Accept
driver.find_element(By.XPATH, "//button[text()='Click for JS Confirm']").click()
sleep(1)
alert.accept()
# driver.find_element(By.XPATH, "//*[text()='You clicked: Ok']") # or we can check the DOM like following
dom = driver.page_source
assert 'You clicked: Ok' in dom
sleep(3)
# 3) Alert - Dismiss
driver.find_element(By.XPATH, "//button[text()='Click for JS Confirm']").click()
sleep(1)
alert.dismiss()
# driver.find_element(By.XPATH, "//*[text()='You clicked: Cancel']")
dom = driver.page_source
assert 'You clicked: Cancel' in dom
sleep(3)
# 4) Alert - Send text
driver.find_element(By.XPATH, "//button[text()='Click for JS Prompt']").click()
sleep(1)
alert.send_keys("This is AutomationCamp")
alert.accept()
assert "This is AutomationCamp" in driver.page_source
sleep(3)
############## Popup (Dialog) ###############
driver.get("https://material.angular.io/components/dialog/examples")
cdk_button = driver.find_element(By.XPATH, "//*[@class='mat-button-wrapper' and text()='CDK']")
offset = cdk_button.location
driver.find_element(By.XPATH, "//button[@class='mat-focus-indicator mat-button mat-button-base']").click()
# Validations
driver.find_element(By.XPATH, "//h3[text()='Develop across all platforms']")
driver.find_element(By.XPATH, "//button//*[text()='Install']")
driver.find_element(By.XPATH, "//button//*[text()='Cancel']")
# Closing the dialog by click on context
actions.move_by_offset(offset['x'], offset['y']).pause(0.5).click().perform()
driver.find_element(By.XPATH, "//button[@class='mat-focus-indicator mat-button mat-button-base']").click()
############## Snackbar ###############
driver.get("https://material.angular.io/components/snack-bar/examples")
driver.find_element(By.XPATH, "//input[@id='mat-input-0']").clear()
driver.find_element(By.XPATH, "//input[@id='mat-input-0']").send_keys(1)
driver.find_element(By.XPATH, "//*[@class='mat-button-wrapper' and normalize-space(text())='Pizza party']/ancestor::button").click()
# 1:
driver.find_element(By.XPATH, "//*[@class='cdk-overlay-container']//*[contains(text(), 'Pizza party')]")
sleep(3)
print("Test Passed")
# 2:
driver.find_element(By.XPATH, "//*[@class='cdk-overlay-container']/following::*[contains(text(), 'Pizza party')]")
#3:
# - Step 1 - Debug the dom
dom = driver.page_source
print(dom)
# - Step 2
driver.find_element(By.XPATH, "//snack-bar-container")
driver.find_element(By.XPATH, "//snack-bar-container//*[contains(text(), 'Pizza party')]")
sleep(3)
print("Test Passed")
############## Tooltip ###############
def check_tooltip_is_visible(elements: list, check_text):
for el in elements:
try:
text = el.text
assert text == check_text
return
except:
pass
raise Exception("Tooltip message cannot found")
driver.get("https://material.angular.io/components/tooltip/examples#tooltip-message")
input_element = driver.find_element(By.XPATH, "//*[@id='mat-input-2']")
input_element.clear()
driver.find_element(By.XPATH, "//*[@id='mat-input-2']").send_keys("AutomationCamp-Session 13")
hover_element = driver.find_element(By.XPATH, "(//button[@class='mat-focus-indicator mat-tooltip-trigger mat-raised-button mat-button-base'])[4]")
actions.move_to_element(hover_element).perform()
tooltip_elements1 = driver.find_elements(By.XPATH, "//*[@class='cdk-overlay-container']/descendant::*")
check_tooltip_is_visible(tooltip_elements1, "AutomationCamp-Session 13")
assert len(tooltip_elements1) > 0
actions.move_to_element(input_element).perform()
tooltip_elements2 = driver.find_elements(By.XPATH, "//*[@class='cdk-overlay-container']/descendant::*")
assert len(tooltip_elements2) == 0
sleep(2)
print("Test is PASSED")