-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add examples of new features for Selenium 4
- Loading branch information
1 parent
ee95fc2
commit c0a496b
Showing
8 changed files
with
180 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from selenium.webdriver.common.by import By | ||
|
||
|
||
# Property and Attribute often return effectively the same value. The biggest difference in Ruby is | ||
# that Attribute always returns a String and Property returns the applicable primitive | ||
# Attribute is defined in html spec: https://dom.spec.whatwg.org/#concept-element-attribute | ||
# Property is defined in ecma spec: https://262.ecma-international.org/5.1/#sec-4.3.26 | ||
# | ||
# Element#attribute guesses which value you want from an element's attribute or property value and returns that | ||
# | ||
# Since this doesn't make sense in a specification, w3c defines 2 new endpoints, made available in Selenium as: | ||
# Element#dom_attribute and Element#property | ||
# The old behavior with the existing method is still available, but executes a large javascript blob | ||
# New behavior should be preferred for performance and preciseness | ||
|
||
def test_boolean(driver): | ||
driver.get("http://watir.com/examples/forms_with_input_elements.html") | ||
|
||
element = driver.find_element(by=By.CSS_SELECTOR, value="#new_user_interests_books") | ||
|
||
assert element.get_attribute('checked') == 'true' | ||
assert element.get_dom_attribute('checked') == 'true' | ||
assert element.get_property('checked') is True | ||
|
||
|
||
def test_number(driver): | ||
driver.get("http://watir.com/examples/forms_with_input_elements.html") | ||
element = driver.find_element(by=By.CSS_SELECTOR, value="#new_user_interests_books") | ||
|
||
# Note that the name of the property and the attribute may be different | ||
# another example is `class` vs `className` | ||
|
||
assert element.get_attribute('tabindex') == '1' | ||
assert element.get_attribute('tabIndex') == '1' | ||
assert element.get_dom_attribute('tabindex') == '1' | ||
assert element.get_dom_attribute('tabIndex') == '1' | ||
assert element.get_property('tabindex') is None | ||
assert element.get_property('tabIndex') == 1 | ||
|
||
|
||
def test_update_boolean(driver): | ||
driver.get("http://watir.com/examples/forms_with_input_elements.html") | ||
|
||
element = driver.find_element(by=By.CSS_SELECTOR, value="#new_user_interests_books") | ||
element.click() | ||
|
||
assert element.get_attribute('checked') is None | ||
assert element.get_dom_attribute('checked') == 'true' | ||
assert element.get_property('checked') is False | ||
|
||
|
||
def test_update_string(driver): | ||
driver.get("http://watir.com/examples/forms_with_input_elements.html") | ||
|
||
element = driver.find_element(by=By.CSS_SELECTOR, value="#new_user_occupation") | ||
|
||
assert element.get_attribute('value') == "Developer" | ||
assert element.get_dom_attribute('value') == "Developer" | ||
assert element.get_property('value') == "Developer" | ||
|
||
element.clear() | ||
element.send_keys("Engineer") | ||
|
||
assert element.get_attribute('value') == "Engineer" | ||
assert element.get_dom_attribute('value') == "Developer" | ||
assert element.get_property('value') == "Engineer" | ||
|
||
|
||
def test_processed_values(driver): | ||
driver.get("http://watir.com/examples/non_control_elements.html") | ||
|
||
element = driver.find_element(by=By.CSS_SELECTOR, value="#link_3") | ||
|
||
assert element.get_attribute('href') == "http://watir.com/examples/forms_with_input_elements.html" | ||
assert element.get_dom_attribute('href') == "forms_with_input_elements.html" | ||
assert element.get_property('href') == "http://watir.com/examples/forms_with_input_elements.html" | ||
|
||
|
||
def test_case_sensitive(driver): | ||
driver.get("http://watir.com/examples/forms_with_input_elements.html") | ||
|
||
element = driver.find_element(by=By.CSS_SELECTOR, value="#new_user_email") | ||
|
||
assert element.get_attribute('nAme') == "new_user_email" | ||
assert element.get_dom_attribute('nAme') == "new_user_email" | ||
assert element.get_property('nAme') is None |
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,18 @@ | ||
from selenium.webdriver.edge.options import Options as EdgeOptions | ||
|
||
|
||
# Selenium 3 only supported EdgeHTML without any custom methods | ||
# Selenium 4 supports same capabilities as Chrome | ||
|
||
def test_edge_options(): | ||
options = EdgeOptions() | ||
options.add_argument('foo') | ||
options.binary_location = '/path/to/edge' | ||
options.add_encoded_extension('encoded') | ||
|
||
caps = options.to_capabilities() | ||
|
||
edgeOptions = caps['ms:edgeOptions'] | ||
assert edgeOptions['args'] == ['foo'] | ||
assert edgeOptions['binary'] == '/path/to/edge' | ||
assert edgeOptions['extensions'] == ['encoded'] |
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,14 @@ | ||
from selenium.webdriver.edge.options import Options as EdgeOptions | ||
|
||
|
||
def test_new_window(driver): | ||
driver.switch_to.new_window('window') | ||
driver.set_window_position(100, 400) | ||
|
||
assert len(driver.window_handles) == 2 | ||
|
||
|
||
def test_new_tab(driver): | ||
driver.switch_to.new_window('tab') | ||
|
||
assert len(driver.window_handles) == 2 |
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,13 @@ | ||
from base64 import b64decode | ||
|
||
from selenium.webdriver.common.timeouts import Timeouts | ||
from selenium.webdriver.edge.options import Options as EdgeOptions | ||
|
||
|
||
def test_getting_timeouts(headless_driver): | ||
headless_driver.get("https://www.saucedemo.com/v1/inventory.html") | ||
|
||
pdf = b64decode(headless_driver.print_page()) | ||
|
||
with open("../resources/python_print_page.pdf", 'wb') as f: | ||
f.write(pdf) |
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,13 @@ | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.relative_locator import locate_with | ||
|
||
|
||
def test_london(driver): | ||
driver.get('https://www.diemol.com/selenium-4-demo/relative-locators-demo.html') | ||
|
||
element = driver.find_elements(locate_with(By.TAG_NAME, "li") | ||
.to_left_of({By.ID: "berlin"}) | ||
.below({By.ID: "warsaw"}))[0] | ||
|
||
driver.execute_script("arguments[0].style.filter='blur(8px)'", element) | ||
assert element.get_attribute('id') == 'london' |
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,12 @@ | ||
from selenium.webdriver.common.timeouts import Timeouts | ||
from selenium.webdriver.edge.options import Options as EdgeOptions | ||
|
||
|
||
def test_getting_timeouts(driver): | ||
timeouts = Timeouts() | ||
timeouts.implicit_wait = 1 | ||
driver.timeouts = timeouts | ||
|
||
assert driver.timeouts.implicit_wait == 1 | ||
assert driver.timeouts.page_load == 300 | ||
assert driver.timeouts.script == 30 |
Empty file.