Skip to content

Commit

Permalink
✨️ add Alert class #166
Browse files Browse the repository at this point in the history
  • Loading branch information
defnngj committed Feb 21, 2023
1 parent 95aca12 commit d93af74
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
1 change: 0 additions & 1 deletion seldom/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
seldom requests
"""
import json
import warnings
from typing import Any
import requests
from seldom.running.config import Seldom
Expand Down
52 changes: 51 additions & 1 deletion seldom/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os
import time
import warnings
import platform
from selenium.webdriver import Chrome
from selenium.webdriver.remote.webdriver import WebDriver as SeleniumWebDriver
Expand Down Expand Up @@ -217,6 +218,46 @@ def space(self) -> None:
log.info(f"✅ {self.web_elem.info}, space.")
self.elem.send_keys(Keys.SPACE)

class Alert:

@property
def text(self) -> str:
"""
Gets the text of the Alert.
"""
log.info(f"✅ alert text: {Seldom.driver.switch_to.alert.text}.")
return Seldom.driver.switch_to.alert.text

@staticmethod
def dismiss() -> None:
"""
Dismisses the alert available.
"""
log.info("✅ dismiss alert.")
return Seldom.driver.switch_to.alert.dismiss()

@staticmethod
def accept():
"""
Accepts the alert available.
Usage::
Alert(driver).accept() # Confirm a alert dialog.
"""
log.info("✅ accept alert.")
return Seldom.driver.switch_to.alert.accept()

@staticmethod
def send_keys(text: str) -> None:
"""
Send Keys to the Alert.
:Args:
- text: The text to be sent to Alert.
"""
log.info(f"✅ input alert '{text}'.")
return Seldom.driver.switch_to.alert.send_keys(text)

@staticmethod
def visit(url: str) -> None:
"""
Expand Down Expand Up @@ -591,6 +632,12 @@ def get_url(self) -> str:
log.info(f"✅ get current url: {Seldom.driver.current_url}.")
return Seldom.driver.current_url

@property
def alert(self) -> Alert:
"""return Alert class"""
alert = self.Alert()
return alert

@property
def get_alert_text(self) -> str:
"""
Expand All @@ -599,6 +646,7 @@ def get_alert_text(self) -> str:
Usage:
self.get_alert_text()
"""
warnings.warn("use self.alert.text instead", DeprecationWarning, stacklevel=2)
log.info(f"✅ alert text: {Seldom.driver.switch_to.alert.text}.")
return Seldom.driver.switch_to.alert.text

Expand All @@ -621,6 +669,7 @@ def accept_alert() -> None:
Usage:
self.accept_alert()
"""
warnings.warn("use self.alert.accept() instead", DeprecationWarning, stacklevel=2)
log.info("✅ accept alert.")
Seldom.driver.switch_to.alert.accept()

Expand All @@ -632,6 +681,7 @@ def dismiss_alert() -> None:
Usage:
self.dismiss_alert()
"""
warnings.warn("use self.alert.dismiss() instead", DeprecationWarning, stacklevel=2)
log.info("✅ dismiss alert.")
Seldom.driver.switch_to.alert.dismiss()

Expand Down Expand Up @@ -724,7 +774,7 @@ def screenshots(self, file_path: str = None) -> None:

def element_screenshot(self, file_path: str = None, index: int = 0, **kwargs) -> None:
"""
Saves a element screenshot of the element to a PNG image file.
Saves an element screenshot of the element to a PNG image file.
Usage:
self.element_screenshot(css="#id")
Expand Down

0 comments on commit d93af74

Please sign in to comment.