Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion build_dist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env python

import os, sys, shutil, subprocess, argparse
import argparse
import os
import shutil
import subprocess
import sys


THIS_DIR = os.path.dirname(os.path.abspath(__file__))
DIST_DIR = os.path.join(THIS_DIR, "dist")
Expand Down
7 changes: 4 additions & 3 deletions src/Selenium2Library/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import absolute_import
import os
from keywords import *
from version import VERSION
from utils import LibraryListener
from .keywords import *
from .version import VERSION
from .utils import LibraryListener

__version__ = VERSION

Expand Down
25 changes: 13 additions & 12 deletions src/Selenium2Library/keywords/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from _logging import _LoggingKeywords
from _runonfailure import _RunOnFailureKeywords
from _browsermanagement import _BrowserManagementKeywords
from _element import _ElementKeywords
from _tableelement import _TableElementKeywords
from _formelement import _FormElementKeywords
from _selectelement import _SelectElementKeywords
from _javascript import _JavaScriptKeywords
from _cookie import _CookieKeywords
from _screenshot import _ScreenshotKeywords
from _waiting import _WaitingKeywords
from _alert import _AlertKeywords
from __future__ import absolute_import
from ._logging import _LoggingKeywords
from ._runonfailure import _RunOnFailureKeywords
from ._browsermanagement import _BrowserManagementKeywords
from ._element import _ElementKeywords
from ._tableelement import _TableElementKeywords
from ._formelement import _FormElementKeywords
from ._selectelement import _SelectElementKeywords
from ._javascript import _JavaScriptKeywords
from ._cookie import _CookieKeywords
from ._screenshot import _ScreenshotKeywords
from ._waiting import _WaitingKeywords
from ._alert import _AlertKeywords

__all__ = [
"_LoggingKeywords",
Expand Down
273 changes: 137 additions & 136 deletions src/Selenium2Library/keywords/_alert.py
Original file line number Diff line number Diff line change
@@ -1,136 +1,137 @@
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from keywordgroup import KeywordGroup


class _AlertKeywords(KeywordGroup):

def __init__(self):
self._cancel_on_next_confirmation = False

# Public

def input_text_into_prompt(self, text):
"""Types the given `text` into alert box. """
alert = None
try:
alert = self._wait_alert()
alert.send_keys(text)
except WebDriverException:
raise RuntimeError('There were no alerts')

def alert_should_be_present(self, text=''):
"""Verifies an alert is present and dismisses it.

If `text` is a non-empty string, then it is also verified that the
message of the alert equals to `text`.

Will fail if no alert is present. Note that following keywords
will fail unless the alert is dismissed by this
keyword or another like `Get Alert Message`.
"""
alert_text = self.get_alert_message()
if text and alert_text != text:
raise AssertionError("Alert text should have been "
"'%s' but was '%s'"
% (text, alert_text))

def choose_cancel_on_next_confirmation(self):
"""Cancel will be selected the next time `Confirm Action` is used."""
self._cancel_on_next_confirmation = True

def choose_ok_on_next_confirmation(self):
"""Undo the effect of using keywords `Choose Cancel On Next Confirmation`. Note
that Selenium's overridden window.confirm() function will normally
automatically return true, as if the user had manually clicked OK, so
you shouldn't need to use this command unless for some reason you need
to change your mind prior to the next confirmation. After any
confirmation, Selenium will resume using the default behavior for
future confirmations, automatically returning true (OK) unless/until
you explicitly use `Choose Cancel On Next Confirmation` for each
confirmation.

Note that every time a confirmation comes up, you must
consume it by using a keywords such as `Get Alert Message`, or else
the following selenium operations will fail.
"""
self._cancel_on_next_confirmation = False

def confirm_action(self):
"""Dismisses currently shown confirmation dialog and returns it's message.

By default, this keyword chooses 'OK' option from the dialog. If
'Cancel' needs to be chosen, keyword `Choose Cancel On Next
Confirmation` must be called before the action that causes the
confirmation dialog to be shown.

Examples:
| Click Button | Send | # Shows a confirmation dialog |
| ${message}= | Confirm Action | # Chooses Ok |
| Should Be Equal | ${message} | Are your sure? |
| | | |
| Choose Cancel On Next Confirmation | | |
| Click Button | Send | # Shows a confirmation dialog |
| Confirm Action | | # Chooses Cancel |
"""
text = self._close_alert(not self._cancel_on_next_confirmation)
self._cancel_on_next_confirmation = False
return text

def get_alert_message(self, dismiss=True):
"""Returns the text of current JavaScript alert.

By default the current JavaScript alert will be dismissed.
This keyword will fail if no alert is present. Note that
following keywords will fail unless the alert is
dismissed by this keyword or another like `Get Alert Message`.
"""
if dismiss:
return self._close_alert()
else:
return self._read_alert()

def dismiss_alert(self, accept=True):
""" Returns true if alert was confirmed, false if it was dismissed

This keyword will fail if no alert is present. Note that
following keywords will fail unless the alert is
dismissed by this keyword or another like `Get Alert Message`.
"""
return self._handle_alert(accept)

# Private

def _close_alert(self, confirm=True):
try:
text = self._read_alert()
self._handle_alert(confirm)
return text
except WebDriverException:
raise RuntimeError('There were no alerts')

def _read_alert(self):
alert = None
try:
alert = self._wait_alert()
# collapse new lines chars
return ' '.join(alert.text.splitlines())
except WebDriverException:
raise RuntimeError('There were no alerts')

def _handle_alert(self, confirm=True):
try:
alert = self._wait_alert()
if not confirm:
alert.dismiss()
return False
else:
alert.accept()
return True
except WebDriverException:
raise RuntimeError('There were no alerts')

def _wait_alert(self):
return WebDriverWait(self._current_browser(), 1).until(
EC.alert_is_present())
from __future__ import absolute_import
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from .keywordgroup import KeywordGroup


class _AlertKeywords(KeywordGroup):

def __init__(self):
self._cancel_on_next_confirmation = False

# Public

def input_text_into_prompt(self, text):
"""Types the given `text` into alert box. """
alert = None
try:
alert = self._wait_alert()
alert.send_keys(text)
except WebDriverException:
raise RuntimeError('There were no alerts')

def alert_should_be_present(self, text=''):
"""Verifies an alert is present and dismisses it.

If `text` is a non-empty string, then it is also verified that the
message of the alert equals to `text`.

Will fail if no alert is present. Note that following keywords
will fail unless the alert is dismissed by this
keyword or another like `Get Alert Message`.
"""
alert_text = self.get_alert_message()
if text and alert_text != text:
raise AssertionError("Alert text should have been "
"'%s' but was '%s'"
% (text, alert_text))

def choose_cancel_on_next_confirmation(self):
"""Cancel will be selected the next time `Confirm Action` is used."""
self._cancel_on_next_confirmation = True

def choose_ok_on_next_confirmation(self):
"""Undo the effect of using keywords `Choose Cancel On Next Confirmation`. Note
that Selenium's overridden window.confirm() function will normally
automatically return true, as if the user had manually clicked OK, so
you shouldn't need to use this command unless for some reason you need
to change your mind prior to the next confirmation. After any
confirmation, Selenium will resume using the default behavior for
future confirmations, automatically returning true (OK) unless/until
you explicitly use `Choose Cancel On Next Confirmation` for each
confirmation.

Note that every time a confirmation comes up, you must
consume it by using a keywords such as `Get Alert Message`, or else
the following selenium operations will fail.
"""
self._cancel_on_next_confirmation = False

def confirm_action(self):
"""Dismisses currently shown confirmation dialog and returns it's message.

By default, this keyword chooses 'OK' option from the dialog. If
'Cancel' needs to be chosen, keyword `Choose Cancel On Next
Confirmation` must be called before the action that causes the
confirmation dialog to be shown.

Examples:
| Click Button | Send | # Shows a confirmation dialog |
| ${message}= | Confirm Action | # Chooses Ok |
| Should Be Equal | ${message} | Are your sure? |
| | | |
| Choose Cancel On Next Confirmation | | |
| Click Button | Send | # Shows a confirmation dialog |
| Confirm Action | | # Chooses Cancel |
"""
text = self._close_alert(not self._cancel_on_next_confirmation)
self._cancel_on_next_confirmation = False
return text

def get_alert_message(self, dismiss=True):
"""Returns the text of current JavaScript alert.

By default the current JavaScript alert will be dismissed.
This keyword will fail if no alert is present. Note that
following keywords will fail unless the alert is
dismissed by this keyword or another like `Get Alert Message`.
"""
if dismiss:
return self._close_alert()
else:
return self._read_alert()

def dismiss_alert(self, accept=True):
""" Returns true if alert was confirmed, false if it was dismissed

This keyword will fail if no alert is present. Note that
following keywords will fail unless the alert is
dismissed by this keyword or another like `Get Alert Message`.
"""
return self._handle_alert(accept)

# Private

def _close_alert(self, confirm=True):
try:
text = self._read_alert()
self._handle_alert(confirm)
return text
except WebDriverException:
raise RuntimeError('There were no alerts')

def _read_alert(self):
alert = None
try:
alert = self._wait_alert()
# collapse new lines chars
return ' '.join(alert.text.splitlines())
except WebDriverException:
raise RuntimeError('There were no alerts')

def _handle_alert(self, confirm=True):
try:
alert = self._wait_alert()
if not confirm:
alert.dismiss()
return False
else:
alert.accept()
return True
except WebDriverException:
raise RuntimeError('There were no alerts')

def _wait_alert(self):
return WebDriverWait(self._current_browser(), 1).until(
EC.alert_is_present())
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_browsermanagement.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import absolute_import
import os
import robot
from robot.errors import DataError
from selenium import webdriver
from Selenium2Library import webdrivermonkeypatches
from Selenium2Library.utils import BrowserCache
from Selenium2Library.locators import WindowManager
from keywordgroup import KeywordGroup
from .keywordgroup import KeywordGroup
from selenium.common.exceptions import NoSuchWindowException

ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
Expand Down
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_cookie.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from keywordgroup import KeywordGroup
from __future__ import absolute_import
from .keywordgroup import KeywordGroup

class _CookieKeywords(KeywordGroup):

Expand Down
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_element.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import absolute_import
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from Selenium2Library import utils
from Selenium2Library.locators import ElementFinder
from Selenium2Library.locators import CustomLocator
from keywordgroup import KeywordGroup
from .keywordgroup import KeywordGroup

try:
basestring # attempt to evaluate basestring
Expand Down
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_formelement.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import os
from keywordgroup import KeywordGroup
from .keywordgroup import KeywordGroup
from selenium.common.exceptions import WebDriverException

class _FormElementKeywords(KeywordGroup):
Expand Down
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_javascript.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import os
from keywordgroup import KeywordGroup
from .keywordgroup import KeywordGroup


class _JavaScriptKeywords(KeywordGroup):
Expand Down
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import absolute_import
import os
import sys
from robot.api import logger
from keywordgroup import KeywordGroup
from .keywordgroup import KeywordGroup
from robot.libraries.BuiltIn import BuiltIn

try:
Expand Down
3 changes: 2 additions & 1 deletion src/Selenium2Library/keywords/_runonfailure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
from robot.libraries import BuiltIn
from keywordgroup import KeywordGroup
from .keywordgroup import KeywordGroup

BUILTIN = BuiltIn.BuiltIn()

Expand Down
Loading