I am testing (pytest) an application on Flask. I have an href
link that I want to test (it makes a transition to another page). The problem is that this link I have by convention appears on the page if the user is authorized with login_user()
(flask-login), so naturally the context of the Flask application is required. For the below presented “client” fixture there is both context and authorization, how to do the same for the “driver” fixture or is there a better approach?
There are such fixtures available:
@pytest.fixture(scope="module")
def new_user():
"""Fixture creates a UserAdmin user."""
user = UserAdmin(id=1,
username="jenya_1",
password=("sha256:600000$9YhgntcGJ2U5uYRk$"
"df894224d9d00ac8aaf6a8fe2d6beb312d1540661"
"954abb880021945d2887863"))
return user
@pytest.fixture(scope="session")
def app():
"""Creating an application instance."""
app = create_app()
app.config.update({
"TestingConfig": True,
})
app.testing = True
yield app
'''with app.test_request_context():
db.session.remove()
db.drop_all()'''
@pytest.fixture()
def client(app, new_user):
"""Creating a test client."""
with app.test_client() as client:
login_user(new_user)
yield client
@pytest.fixture()
def runner(app):
"""Creating a client runner."""
return app.test_cli_runner()
@pytest.fixture(scope="module")
def driver():
"""Initializing the browser driver."""
driver = webdriver.Chrome()
yield driver
driver.quit()
For the test, I use this function (due to lack of context, it doesn’t work):
def test_href_home_page(self, driver):
driver.get("http://localhost:5000")
element = WebDriverWait(driver,
10).until(
EC.presence_of_element_located(
(By.CLASS_NAME, 'btn-MENU')))
element.click()
assert 'Make a choice!' in driver.title
- I tried adding context and authorization for “drive” in the same way as for “client” – it didn’t help.
- I tried adding the “with client:” construct to the test function, that didn’t help either.