Skip to content

Commit 4e5a674

Browse files
authored
[py] Fix chromedriver/msedgedriver service tests (#16395)
1 parent 66d780b commit 4e5a674

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

py/test/selenium/webdriver/chrome/chrome_service_tests.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import os
1919
import subprocess
2020
import sys
21-
import time
2221
from unittest.mock import patch
2322

2423
import pytest
@@ -28,36 +27,35 @@
2827

2928

3029
@pytest.mark.no_driver_after_test
31-
def test_uses_chromedriver_logging(clean_driver, clean_options, driver_executable) -> None:
30+
def test_reuses_chromedriver_log(clean_driver, clean_options, driver_executable) -> None:
3231
log_file = "chromedriver.log"
33-
service_args = ["--append-log"]
3432

3533
service1 = Service(
3634
log_output=log_file,
37-
service_args=service_args,
3835
executable_path=driver_executable,
3936
)
4037

4138
service2 = Service(
4239
log_output=log_file,
43-
service_args=service_args,
40+
service_args=["--append-log"],
4441
executable_path=driver_executable,
4542
)
4643

47-
driver1 = None
48-
driver2 = None
44+
driver = None
4945
try:
50-
driver1 = clean_driver(options=clean_options, service=service1)
46+
driver = clean_driver(options=clean_options, service=service1)
5147
with open(log_file) as fp:
5248
lines = len(fp.readlines())
53-
driver2 = clean_driver(options=clean_options, service=service2)
49+
finally:
50+
if driver:
51+
driver.quit()
52+
try:
53+
driver = clean_driver(options=clean_options, service=service2)
5454
with open(log_file) as fp:
5555
assert len(fp.readlines()) >= 2 * lines
5656
finally:
57-
if driver1:
58-
driver1.quit()
59-
if driver2:
60-
driver2.quit()
57+
if driver:
58+
driver.quit()
6159
os.remove(log_file)
6260

6361

@@ -69,7 +67,9 @@ def test_log_output_as_filename(clean_driver, clean_options, driver_executable)
6967
assert "--log-path=chromedriver.log" in service.service_args
7068
driver = clean_driver(options=clean_options, service=service)
7169
with open(log_file) as fp:
72-
assert "Starting ChromeDriver" in fp.readline()
70+
out = fp.read()
71+
assert "Starting" in out
72+
assert "started successfully" in out
7373
finally:
7474
driver.quit()
7575
os.remove(log_file)
@@ -82,9 +82,10 @@ def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> N
8282
service = Service(log_output=log_file, executable_path=driver_executable)
8383
try:
8484
driver = clean_driver(options=clean_options, service=service)
85-
time.sleep(1)
8685
with open(log_name) as fp:
87-
assert "Starting ChromeDriver" in fp.readline()
86+
out = fp.read()
87+
assert "Starting" in out
88+
assert "started successfully" in out
8889
finally:
8990
driver.quit()
9091
log_file.close()
@@ -95,16 +96,17 @@ def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> N
9596
def test_log_output_as_stdout(clean_driver, clean_options, capfd, driver_executable) -> None:
9697
service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable)
9798
driver = clean_driver(options=clean_options, service=service)
98-
9999
out, err = capfd.readouterr()
100-
assert "Starting ChromeDriver" in out
100+
assert "Starting" in out
101+
assert "started successfully" in out
101102
driver.quit()
102103

103104

104105
@pytest.mark.no_driver_after_test
105106
def test_log_output_null_default(driver, capfd) -> None:
106107
out, err = capfd.readouterr()
107-
assert "Starting ChromeDriver" not in out
108+
assert "Starting" not in out
109+
assert "started successfully" not in out
108110
driver.quit()
109111

110112

py/test/selenium/webdriver/edge/edge_service_tests.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import os
1919
import subprocess
2020
import sys
21-
import time
2221
from unittest.mock import patch
2322

2423
import pytest
@@ -28,36 +27,35 @@
2827

2928

3029
@pytest.mark.no_driver_after_test
31-
def test_uses_edgedriver_logging(clean_driver, clean_options, driver_executable) -> None:
30+
def test_reuses_edgedriver_log(clean_driver, clean_options, driver_executable) -> None:
3231
log_file = "msedgedriver.log"
33-
service_args = ["--append-log"]
3432

3533
service1 = Service(
3634
log_output=log_file,
37-
service_args=service_args,
3835
executable_path=driver_executable,
3936
)
4037

4138
service2 = Service(
4239
log_output=log_file,
43-
service_args=service_args,
40+
service_args=["--append-log"],
4441
executable_path=driver_executable,
4542
)
4643

47-
driver1 = None
48-
driver2 = None
44+
driver = None
4945
try:
50-
driver1 = clean_driver(options=clean_options, service=service1)
46+
driver = clean_driver(options=clean_options, service=service1)
5147
with open(log_file) as fp:
5248
lines = len(fp.readlines())
53-
driver2 = clean_driver(options=clean_options, service=service2)
49+
finally:
50+
if driver:
51+
driver.quit()
52+
try:
53+
driver = clean_driver(options=clean_options, service=service2)
5454
with open(log_file) as fp:
5555
assert len(fp.readlines()) >= 2 * lines
5656
finally:
57-
if driver1:
58-
driver1.quit()
59-
if driver2:
60-
driver2.quit()
57+
if driver:
58+
driver.quit()
6159
os.remove(log_file)
6260

6361

@@ -69,7 +67,9 @@ def test_log_output_as_filename(clean_driver, clean_options, driver_executable)
6967
assert "--log-path=msedgedriver.log" in service.service_args
7068
driver = clean_driver(options=clean_options, service=service)
7169
with open(log_file) as fp:
72-
assert "Starting Microsoft Edge WebDriver" in fp.readline()
70+
out = fp.read()
71+
assert "Starting" in out
72+
assert "started successfully" in out
7373
finally:
7474
driver.quit()
7575
os.remove(log_file)
@@ -82,9 +82,10 @@ def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> N
8282
service = Service(log_output=log_file, executable_path=driver_executable)
8383
try:
8484
driver = clean_driver(options=clean_options, service=service)
85-
time.sleep(1)
8685
with open(log_name) as fp:
87-
assert "Starting msedgedriver" in fp.readline()
86+
out = fp.read()
87+
assert "Starting" in out
88+
assert "started successfully" in out
8889
finally:
8990
driver.quit()
9091
log_file.close()
@@ -95,16 +96,17 @@ def test_log_output_as_file(clean_driver, clean_options, driver_executable) -> N
9596
def test_log_output_as_stdout(clean_driver, clean_options, capfd, driver_executable) -> None:
9697
service = Service(log_output=subprocess.STDOUT, executable_path=driver_executable)
9798
driver = clean_driver(options=clean_options, service=service)
98-
9999
out, err = capfd.readouterr()
100-
assert "Starting msedgedriver" in out
100+
assert "Starting" in out
101+
assert "started successfully" in out
101102
driver.quit()
102103

103104

104105
@pytest.mark.no_driver_after_test
105106
def test_log_output_null_default(driver, capfd) -> None:
106107
out, err = capfd.readouterr()
107-
assert "Starting Microsoft Edge WebDriver" not in out
108+
assert "Starting" not in out
109+
assert "started successfully" not in out
108110
driver.quit()
109111

110112

0 commit comments

Comments
 (0)