I have requirement where i have to take language from the command line and do validation based on selected language here is my code conftest.py
def pytest_addoption(parser):
parser.addoption(
"--language",
action="store",
default="0",
help="set language")
language_list = {"0": "en", "1": "ko_KR", "2": "zh_CN", "3": "ar"}
@pytest.fixture(scope="class")
def language(request):
language = request.config.option.language
print("fixtures lang", language)
if language not in language_list:
print(language_list)
raise Exception("Please set a valid language")
print("conftest", type(language))
return language
I want to use this langugae in testfile so i am calling it like this way test_login.py
class TestAccountOwnerLoginClass:
@pytest.fixture(autouse=True)
def _setup(
self,
):
self.login_page_tab_instance = LoginPageClass(self.driver)
@allure.title(
"Go to login page and validate correct account owner url is displayed"
)
@allure.severity(allure.severity_level.NORMAL)
@pytest.mark.mandatory
@pytest.mark.run(order=1.0)
def test_visit_login_page(self):
link = os.getenv("LOGIN_LINK")
self.login_page_tab_instance.go_to_page(link)
assert self.login_page_tab_instance.get_current_url()
@allure.title("Go to login page and select language from drop down ")
@allure.severity(allure.severity_level.NORMAL)
@pytest.mark.run(order=1.1)
@pytest.mark.low
def test_select_language(self, language):
assert self.login_page_tab_instance.select_language(int(language))
@allure.title("Validate correct page title is displayed")
@allure.severity(allure.severity_level.MINOR)
@pytest.mark.run(order=1.2)
@pytest.mark.low
def test_title_displayed_properly(self):
assert self.login_page_tab_instance.check_title_text_display()
This is the function where i want to use the entered languague base.py
def fetch_local_constants(self, key):
"""Return constants value based on the language input"""
print("Fetch language", self.language)
if not constants.constant_dict[key][language_list]:
print(language_list[language])
raise Exception("The Constant does not exist")
if not constants.constant_dict[key][language_list[language]]:
raise Exception(" The constant translation does not exists")
return constants.constant_dict[key][language_list[language]]
def check_title_text_display(self):
time.sleep(2)
print(self.selenium.title)
return self.fetch_local_constants("LOGIN_PAGE_TITLE") in self.selenium.title
constant.py
constant_dict = {
"LOGIN_PAGE_TITLE": {"en": "Login",
"ko_KR": "로그인 | iSchoolConnect"},
"ENTER_VALID_EMAIL": {"en": "Please enter a valid email ID",
"ko_KR": "유효한 이메일 ID를 입력하십시오"},
"PASSWORD_RESET_EMAIL_SENT_TEXT": {
"en": "Password Reset Email Sent",
"ko_KR": "密码重置电子邮件已发送", },
"PASSWORD_MATCH_VALIDATION": {
"en": "Password Reset Email Sent",
"ko_KR": "密码重置电子邮件已发送", },
"ENTER_REGISTERED_EMAIL": {
"en": "Please enter your registered email ID",
"ko_KR": "등록 된 이메일 ID를 입력하세요"},
"PASSWORD_REQUIRED": {
"en": "Please enter your password",
"ko_KR": "비밀번호를 입력하세요"},
"PASSWORD_CHARACTERS_VALIDATION": {
"en": "Password has to be at least 8 characters",
"ko_KR": "비밀번호는 8 자 이상이어야합니다."},}
however i am getting an error:
pages/authentication/login_page.py:42: in check_title_text_display
return self.fetch_local_constants("LOGIN_PAGE_TITLE") in self.selenium.title
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <pages.authentication.login_page.LoginPageClass object at 0x7ff0a8328f10>, key = 'LOGIN_PAGE_TITLE'
def fetch_local_constants(self, key):
"""Return constants value based on the language input"""
print("Fetch language", self.language)
> if not constants.constant_dict[key][language_list]:
E TypeError: unhashable type: 'dict'
pages/base_page.py:628: TypeError
==================================================================================================== short test summary info =====================================================================================================
FAILED tests/authenication/login/isc/test_account_owner.py::TestAccountOwnerLoginClass::test_title_displayed_properly - TypeError: unhashable type: 'dict'