-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow WebDriverElement to be used with Browser().execute_script()
- Loading branch information
Showing
6 changed files
with
315 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
tests/tests_webdriver/test_javascript/test_evaluate_script.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import pytest | ||
|
||
from selenium.common.exceptions import JavascriptException | ||
|
||
|
||
def test_evaluate_script_valid(browser, app_url): | ||
"""Scenario: Evaluating JavaScript Returns The Code's Result | ||
When I evaluate JavaScript code | ||
Then the result of the evaluation is returned | ||
""" | ||
browser.visit(app_url) | ||
|
||
document_href = browser.evaluate_script("document.location.href") | ||
assert app_url == document_href | ||
|
||
|
||
def test_evaluate_script_valid_args(browser, app_url): | ||
"""Scenario: Execute Valid JavaScript With Arguments | ||
When I execute valid JavaScript code which modifies the DOM | ||
And I send arguments to the web browser | ||
Then the arguments are available for use | ||
""" | ||
browser.visit(app_url) | ||
|
||
browser.evaluate_script( | ||
"document.querySelector('body').innerHTML = arguments[0] + arguments[1]", | ||
"A String And ", | ||
"Another String", | ||
) | ||
|
||
elem = browser.find_by_tag("body").first | ||
assert elem.value == "A String And Another String" | ||
|
||
|
||
def test_evaluate_script_valid_args_element(browser, app_url): | ||
"""Scenario: Execute Valid JavaScript | ||
When I execute valid JavaScript code | ||
And I send an Element to the browser as an argument | ||
Then the modifications are seen in the document | ||
""" | ||
browser.visit(app_url) | ||
|
||
elem = browser.find_by_id("firstheader").first | ||
elem_text = browser.evaluate_script("arguments[0].innerHTML", elem) | ||
assert elem_text == "Example Header" | ||
|
||
|
||
def test_evaluate_script_invalid(browser, app_url): | ||
"""Scenario: Evaluate Invalid JavaScript. | ||
When I evaluate invalid JavaScript code | ||
Then an error is raised | ||
""" | ||
browser.visit(app_url) | ||
|
||
with pytest.raises(JavascriptException): | ||
browser.evaluate_script("invalid.thisIsNotGood()") | ||
|
||
|
||
def test_evaluate_script_invalid_args(browser, app_url): | ||
"""Scenario: Execute Valid JavaScript | ||
When I execute valid JavaScript code which modifies the DOM | ||
And I send an object to the browser which is not JSON serializable | ||
Then an error is raised | ||
""" | ||
browser.visit(app_url) | ||
|
||
def unserializable(): | ||
"You can't JSON serialize a function." | ||
|
||
with pytest.raises(TypeError): | ||
browser.evaluate_script("arguments[0]", unserializable) |
Oops, something went wrong.