Reputation: 75
Observation:
I'm running my data driven UI automation using selenium with pytest framework and There are 60 testcases.
Now Scope is at session level and login to the application is happening only one time in setup module.
so now the scenario is, testcases execution is starting:
setup and teardown at session level:
@pytest.fixture(scope="session") setup module(): login_to_the_application() Teardown module(): kill_browser_instance()
Testcase starting:
@pytest.mark.ui def test_verify_table_data(setup_module, data): #data is global variable created and setup as fixture in conftest.py file.
db_records = fetching_records_for_data_from_db(data)
#db_records consist 200 rows of data and this we will validate from ui one by one row.
for record in db_records():
value = validating_row_data_in_ui_table(record)
if value is True:
print(pass)
else:
print(fail)
so this complete flow is only executing till 4 to 5 data and then automatically it is displaying the message like You must close the browser to complete the logout process.
Not sure how to handle this situation. any help or lead will be good. thanks in advance.
Upvotes: 0
Views: 165
Reputation: 15556
Here's a full script, runnable with pytest
, that has the setUp()
and tearDown()
methods defined so the the browser is automatically opened and closed at the start and end of tests: (It's an alternative to using fixtures, as pytest
can be combined with unittest.TestCase
, which may help structure things better for making sure the browser is closed at the end of the test.)
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from unittest import TestCase
class SeleniumClass(TestCase):
def setUp(self):
self.driver = None
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
if "linux" in sys.platform:
options.add_argument("--headless=new")
options.add_experimental_option(
"excludeSwitches", ["enable-automation", "enable-logging"],
)
prefs = {
"credentials_enable_service": False,
"profile.password_manager_enabled": False,
}
options.add_experimental_option("prefs", prefs)
self.driver = webdriver.Chrome(options=options)
def tearDown(self):
if self.driver:
try:
if self.driver.service.process:
self.driver.quit()
except Exception:
pass
def test_login(self):
self.driver.get("https://www.saucedemo.com")
by_css = By.CSS_SELECTOR # "css selector"
element = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((by_css, "#user-name"))
)
element.send_keys("standard_user")
element = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((by_css, "#password"))
)
element.send_keys("secret_sauce")
element.submit()
WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((by_css, "div.inventory_list"))
)
time.sleep(1)
# When run with "python" instead of "pytest" or "python -m unittest"
if __name__ == "__main__":
from unittest import main
main()
Upvotes: 0