Skip to content

Commit 7e3ac94

Browse files
dosasdosas
andauthored
Selenium 4.0 (#265)
* Support Selenium 4 Co-authored-by: dosas <[email protected]>
1 parent 39cac4e commit 7e3ac94

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

pytest_selenium/drivers/cloud.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ class Provider(object):
1717
def name(self):
1818
return type(self).__name__
1919

20+
@property
21+
def config_name(self):
22+
return ".{0}".format(self.name.lower())
23+
24+
@property
25+
def config_file_path(self):
26+
return os.path.join(os.path.expanduser("~"), self.config_name)
27+
2028
@property
2129
def config(self):
22-
name = ".{0}".format(self.name.lower())
2330
config = configparser.ConfigParser()
24-
config.read([name, os.path.join(os.path.expanduser("~"), name)])
31+
config.read([self.config_name, self.config_file_path])
2532
return config
2633

2734
def get_credential(self, key, envs):

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ install_requires =
3535
pytest-base-url
3636
pytest-html>=1.14.0
3737
pytest-variables>=1.5.0
38-
selenium>=3.0.0,<4.0.0
38+
selenium>=3.0.0,<4.0.0;python_version=='3.6'
39+
selenium>=4.0.0;python_version>'3.6'
3940
requests
4041
tenacity>=6.0.0,<7.0.0
4142
python_requires = >=3.6.1

testing/conftest.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def testdir(request, httpserver_base_url):
2828

2929
conftest = """
3030
import pytest
31+
from selenium.webdriver.common.by import By
3132
@pytest.fixture
3233
def webtext(base_url, selenium):
3334
selenium.get(base_url)
34-
return selenium.find_element_by_tag_name('h1').text
35+
return selenium.find_element(By.TAG_NAME, 'h1').text
3536
"""
3637

3738
if item.get_closest_marker("chrome"):
@@ -51,9 +52,13 @@ def chrome_options(chrome_options):
5152
filterwarnings =
5253
error::DeprecationWarning
5354
ignore:--firefox-\w+ has been deprecated:DeprecationWarning
55+
ignore:capabilities and desired_capabilities have been deprecated, please pass in a Service object:DeprecationWarning
56+
ignore:firefox_profile has been deprecated, please use an Options object:DeprecationWarning
57+
ignore:Setting a profile has been deprecated. Please use the set_preference and install_addons methods:DeprecationWarning
58+
ignore:Getting a profile has been deprecated.:DeprecationWarning
5459
ignore:desired_capabilities has been deprecated
5560
ignore:service_log_path has been deprecated
56-
""",
61+
""", # noqa: E501
5762
)
5863

5964
def runpytestqa(*args, **kwargs):

testing/test_browserstack.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import pytest
99

10+
from pytest_selenium.drivers.cloud import Provider
11+
1012
pytestmark = pytest.mark.nondestructive
1113

1214

@@ -66,7 +68,6 @@ def test_missing_access_key_file(failure, monkeypatch, tmpdir):
6668
],
6769
)
6870
def test_invalid_credentials_env(failure, monkeypatch, tmpdir, username, key):
69-
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
7071
monkeypatch.setenv(username, "foo")
7172
monkeypatch.setenv(key, "bar")
7273
out = failure()
@@ -75,8 +76,9 @@ def test_invalid_credentials_env(failure, monkeypatch, tmpdir, username, key):
7576

7677

7778
def test_invalid_credentials_file(failure, monkeypatch, tmpdir):
78-
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
79-
tmpdir.join(".browserstack").write("[credentials]\nusername=foo\nkey=bar")
79+
cfg_file = tmpdir.join(".browserstack")
80+
cfg_file.write("[credentials]\nusername=foo\nkey=bar")
81+
monkeypatch.setattr(Provider, "config_file_path", str(cfg_file))
8082
out = failure()
8183
messages = ["Invalid username or password", "basic auth failed"]
8284
assert any(message in out for message in messages)

testing/test_firefox.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def test_profile(testdir, httpserver):
4949
file_test = testdir.makepyfile(
5050
"""
5151
import pytest
52+
from selenium.webdriver.common.by import By
5253
@pytest.mark.nondestructive
5354
def test_profile(base_url, selenium):
5455
selenium.get(base_url)
55-
header = selenium.find_element_by_tag_name('h1')
56-
anchor = selenium.find_element_by_tag_name('a')
56+
header = selenium.find_element(By.TAG_NAME, 'h1')
57+
anchor = selenium.find_element(By.TAG_NAME, 'a')
5758
header_color = header.value_of_css_property('color')
5859
anchor_color = anchor.value_of_css_property('color')
5960
assert header_color == 'rgb(255, 0, 0)'
@@ -81,11 +82,12 @@ def test_profile_with_preferences(testdir, httpserver):
8182
file_test = testdir.makepyfile(
8283
"""
8384
import pytest
85+
from selenium.webdriver.common.by import By
8486
@pytest.mark.nondestructive
8587
def test_preferences(base_url, selenium):
8688
selenium.get(base_url)
87-
header = selenium.find_element_by_tag_name('h1')
88-
anchor = selenium.find_element_by_tag_name('a')
89+
header = selenium.find_element(By.TAG_NAME, 'h1')
90+
anchor = selenium.find_element(By.TAG_NAME, 'a')
8991
header_color = header.value_of_css_property('color')
9092
anchor_color = anchor.value_of_css_property('color')
9193
assert header_color == 'rgb(255, 0, 0)'
@@ -114,6 +116,7 @@ def test_extension(testdir):
114116
"""
115117
import time
116118
import pytest
119+
from selenium.webdriver.common.by import By
117120
from selenium.common.exceptions import StaleElementReferenceException
118121
from selenium.webdriver.support.ui import WebDriverWait
119122
@pytest.mark.nondestructive
@@ -122,7 +125,7 @@ def test_extension(selenium):
122125
extensions = WebDriverWait(
123126
selenium, timeout=10,
124127
ignored_exceptions=StaleElementReferenceException).until(
125-
lambda s: s.find_element_by_id(
128+
lambda s: s.find_element(By.ID,
126129
'extensions-tbody').text)
127130
assert 'Test Extension (empty)' in extensions
128131
"""
@@ -136,15 +139,16 @@ def test_preferences_marker(testdir, httpserver):
136139
file_test = testdir.makepyfile(
137140
"""
138141
import pytest
142+
from selenium.webdriver.common.by import By
139143
@pytest.mark.nondestructive
140144
@pytest.mark.firefox_preferences({
141145
'browser.anchor_color': '#FF69B4',
142146
'browser.display.foreground_color': '#FF0000',
143147
'browser.display.use_document_colors': False})
144148
def test_preferences(base_url, selenium):
145149
selenium.get(base_url)
146-
header = selenium.find_element_by_tag_name('h1')
147-
anchor = selenium.find_element_by_tag_name('a')
150+
header = selenium.find_element(By.TAG_NAME, 'h1')
151+
anchor = selenium.find_element(By.TAG_NAME, 'a')
148152
header_color = header.value_of_css_property('color')
149153
anchor_color = anchor.value_of_css_property('color')
150154
assert header_color == 'rgb(255, 0, 0)'

testing/test_saucelabs.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import pytest
1010

11+
from pytest_selenium.drivers.cloud import Provider
12+
1113
pytestmark = [pytest.mark.skip_selenium, pytest.mark.nondestructive]
1214

1315

@@ -68,7 +70,6 @@ def test_missing_api_key_file(failure, monkeypatch, tmpdir):
6870
],
6971
)
7072
def test_invalid_credentials_env(failure, monkeypatch, tmpdir, username, key):
71-
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
7273
monkeypatch.setenv(username, "foo")
7374
monkeypatch.setenv(key, "bar")
7475
out = failure()
@@ -77,8 +78,9 @@ def test_invalid_credentials_env(failure, monkeypatch, tmpdir, username, key):
7778

7879

7980
def test_invalid_credentials_file(failure, monkeypatch, tmpdir):
80-
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
81-
tmpdir.join(".saucelabs").write("[credentials]\nusername=foo\nkey=bar")
81+
cfg_file = tmpdir.join(".saucelabs")
82+
cfg_file.write("[credentials]\nusername=foo\nkey=bar")
83+
monkeypatch.setattr(Provider, "config_file_path", str(cfg_file))
8284
out = failure()
8385
messages = ["Sauce Labs Authentication Error", "basic auth failed", "Unauthorized"]
8486
assert any(message in out for message in messages)

testing/test_testingbot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from functools import partial
99

10+
from pytest_selenium.drivers.cloud import Provider
11+
1012
"'as TB' to avoid pytest trying to collect the class"
1113
from pytest_selenium.drivers.testingbot import HOST, PORT, TestingBot as TB
1214

@@ -66,7 +68,6 @@ def test_missing_secret_file(failure, monkeypatch, tmpdir):
6668
[("TESTINGBOT_KEY", "TESTINGBOT_SECRET"), ("TESTINGBOT_PSW", "TESTINGBOT_USR")],
6769
)
6870
def test_invalid_credentials_env(failure, monkeypatch, tmpdir, key, secret):
69-
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
7071
monkeypatch.setenv(key, "foo")
7172
monkeypatch.setenv(secret, "bar")
7273
out = failure()
@@ -75,8 +76,9 @@ def test_invalid_credentials_env(failure, monkeypatch, tmpdir, key, secret):
7576

7677

7778
def test_invalid_credentials_file(failure, monkeypatch, tmpdir):
78-
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
79-
tmpdir.join(".testingbot").write("[credentials]\nkey=foo\nsecret=bar")
79+
cfg_file = tmpdir.join(".testingbot")
80+
cfg_file.write("[credentials]\nkey=foo\nsecret=bar")
81+
monkeypatch.setattr(Provider, "config_file_path", str(cfg_file))
8082
out = failure()
8183
messages = ["incorrect TestingBot credentials", "basic auth failed"]
8284
assert any(message in out for message in messages)

0 commit comments

Comments
 (0)