Created
June 29, 2022 17:58
-
-
Save SarahElson/ff8b78a1c60bf624e3b4f4411085dd38 to your computer and use it in GitHub Desktop.
How To Get Current URL In Selenium Python
This file contains hidden or 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
import json | |
import pytest | |
import selenium.webdriver | |
from os import environ | |
from selenium import webdriver | |
from selenium.common.exceptions import WebDriverException | |
from selenium.webdriver.remote.remote_connection import RemoteConnection | |
@pytest.fixture | |
def config(scope='session'): | |
# Read the file | |
with open('config.json') as config_file: | |
config = json.load(config_file) | |
# Assert values are acceptable | |
assert config['browser'] in ['Firefox', 'Chrome', 'Headless Chrome'] | |
assert isinstance(config['implicit_wait'], int) | |
assert config['implicit_wait'] > 0 | |
# Return config so it can be used | |
return config | |
@pytest.fixture | |
def browser(config): | |
# Initialize the WebDriver instance | |
if config['browser'] == 'Firefox': | |
b = selenium.webdriver.Firefox() | |
elif config['browser'] == 'Chrome': | |
b = selenium.webdriver.Chrome() | |
elif config['browser'] == 'Headless Chrome': | |
opts = selenium.webdriver.ChromeOptions() | |
opts.add_argument('headless') | |
b = selenium.webdriver.Chrome(options=opts) | |
else: | |
raise Exception(f'Browser "{config["browser"]}" is not supported') | |
# Make its calls wait for elements to appear | |
b.implicitly_wait(config['implicit_wait']) | |
# Return the WebDriver instance for the setup | |
yield b | |
# Quit the WebDriver instance for the cleanup | |
b.quit() | |
@pytest.fixture(scope='function') | |
def browser(request): | |
desired_caps = {} | |
browser = { | |
'LT:Options' : { | |
"user" : environ.get('LT_USERNAME', None), | |
"accessKey" : environ.get('LT_ACCESS_KEY', None), | |
"build" : "lamdatest_build", | |
"name" : "lamdatest_demo", | |
"platformName" : "Windows 11", | |
}, | |
"browserName" : "Firefox", | |
"browserVersion" : "99.0", | |
} | |
desired_caps.update(browser) | |
test_name = request.node.name | |
build = environ.get('BUILD', "Sample PY Build") | |
username = browser.get('LT:Options', {}).get('user') | |
access_key = browser.get('LT:Options', {}).get('accessKey') | |
selenium_endpoint = "http://{}:{}@hub.lambdatest.com/wd/hub".format(username, access_key) | |
executor = RemoteConnection(selenium_endpoint, resolve_ip=False) | |
browser = webdriver.Remote( | |
command_executor=executor, | |
desired_capabilities=desired_caps | |
) | |
yield browser | |
def fin(): | |
#browser.execute_script("lambda-status=".format(str(not request.node.rep_call.failed if "passed" else "failed").lower())) | |
if request.node.rep_call.failed: | |
browser.execute_script("lambda-status=failed") | |
else: | |
browser.execute_script("lambda-status=passed") | |
browser.quit() | |
request.addfinalizer(fin) | |
@pytest.hookimpl(tryfirst=True, hookwrapper=True) | |
def pytest_runtest_makereport(item, call): | |
# this sets the result as a test attribute for LambdaTest reporting. | |
# execute all other hooks to obtain the report object | |
outcome = yield | |
rep = outcome.get_result() | |
# set an report attribute for each phase of a call, which can | |
# be "setup", "call", "teardown" | |
setattr(item, "rep_" + rep.when, rep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment