Skip to content

Commit

Permalink
Remove validation of locators and strategies in python client
Browse files Browse the repository at this point in the history
Validation should be performed by the driver implementation
  • Loading branch information
davehunt committed May 20, 2016
1 parent 874dfa8 commit 6dab772
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 87 deletions.
7 changes: 0 additions & 7 deletions py/selenium/webdriver/common/by.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,3 @@ class By(object):
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"

@classmethod
def is_valid(cls, by):
for attr in dir(cls):
if by == getattr(cls, attr):
return True
return False
5 changes: 0 additions & 5 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from .mobile import Mobile
from .file_detector import FileDetector, LocalFileDetector
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import InvalidSelectorException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.html5.application_cache import ApplicationCache

Expand Down Expand Up @@ -730,8 +729,6 @@ def find_element(self, by=By.ID, value=None):
:rtype: WebElement
"""
if not By.is_valid(by) or not isinstance(value, str):
raise InvalidSelectorException("Invalid locator values passed in")
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
Expand All @@ -756,8 +753,6 @@ def find_elements(self, by=By.ID, value=None):
:rtype: list of WebElement
"""
if not By.is_valid(by) or not isinstance(value, str):
raise InvalidSelectorException("Invalid locator values passed in")
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
Expand Down
7 changes: 0 additions & 7 deletions py/selenium/webdriver/remote/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

from .command import Command
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import InvalidSelectorException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.utils import keys_to_typing

Expand Down Expand Up @@ -457,9 +456,6 @@ def _execute(self, command, params=None):
return self._parent.execute(command, params)

def find_element(self, by=By.ID, value=None):
if not By.is_valid(by) or not isinstance(value, str):
raise InvalidSelectorException("Invalid locator values passed in")

if self._w3c:
if by == By.ID:
by = By.CSS_SELECTOR
Expand All @@ -477,9 +473,6 @@ def find_element(self, by=By.ID, value=None):
{"using": by, "value": value})['value']

def find_elements(self, by=By.ID, value=None):
if not By.is_valid(by) or not isinstance(value, str):
raise InvalidSelectorException("Invalid locator values passed in")

if self._w3c:
if by == By.ID:
by = By.CSS_SELECTOR
Expand Down
80 changes: 12 additions & 68 deletions py/test/selenium/webdriver/common/children_finding_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
# specific language governing permissions and limitations
# under the License.


import unittest
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import InvalidSelectorException
from selenium.webdriver.common.by import By

import pytest
from selenium.common.exceptions import (
WebDriverException,
NoSuchElementException)


class ChildrenFindingTests(unittest.TestCase):

Expand Down Expand Up @@ -156,77 +158,19 @@ def test_should_be_able_to_find_multiple_elements_by_css_selector(self):
'*[name="selectomatic"]')
self.assertEqual(2, len(elements))

def test_should_throw_an_error_if_user_passes_in_integer(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_element(By.ID, 333333)
self.fail("_should have thrown _web_driver Exception")
except InvalidSelectorException:
pass #_this is expected

def test_should_throw_an_error_if_user_passes_in_tuple(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_element((By.ID, 333333))
self.fail("_should have thrown _web_driver Exception")
except InvalidSelectorException:
pass #_this is expected

def test_should_throw_an_error_if_user_passes_inNone(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_element(By.ID, None)
self.fail("_should have thrown _web_driver Exception")
except InvalidSelectorException:
pass #_this is expected

def test_should_throw_an_error_if_user_passes_in_invalid_by(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_element("css", "body")
self.fail("_should have thrown _web_driver Exception")
except InvalidSelectorException:
pass #_this is expected

def test_should_throw_an_error_if_user_passes_in_integer_when_find_elements(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_elements(By.ID, 333333)
self.fail("_should have thrown _web_driver Exception")
except InvalidSelectorException:
pass #_this is expected

def test_should_throw_an_error_if_user_passes_in_tuple_when_find_elements(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_elements((By.ID, 333333))
self.fail("_should have thrown _web_driver Exception")
except InvalidSelectorException:
pass #_this is expected

def test_should_throw_an_error_if_user_passes_inNone_when_find_elements(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_elements(By.ID, None)
self.fail("should have thrown webdriver Exception")
except InvalidSelectorException:
pass #_this is expected
with pytest.raises(WebDriverException) as excinfo:
element.find_element("foo", "bar")
assert 'Unsupported locator strategy: foo' in str(excinfo.value)

def test_should_throw_an_error_if_user_passes_in_invalid_by_when_find_elements(self):
self._load_page("nestedElements")
element = self.driver.find_element_by_name("form2")
try:
element.find_elements("css", "body")
self.fail("Should have thrown WebDriver Exception")
except InvalidSelectorException:
pass #_this is expected
with pytest.raises(WebDriverException) as excinfo:
element.find_elements("foo", "bar")
assert 'Unsupported locator strategy: foo' in str(excinfo.value)

def _page_url(self, name):
return self.webserver.where_is(name + '.html')
Expand Down

0 comments on commit 6dab772

Please sign in to comment.