Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
amdomanska committed Apr 10, 2024
1 parent e90eaff commit dc9a111
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 20 deletions.
3 changes: 2 additions & 1 deletion doajtest/fixtures/messages.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ERROR_OA_STATEMENT = "DOAJ only indexes open access journals which comply with the statement above. Please check and update the open access statement of your journal. You may return to this application at any time."
ERROR_OA_STATEMENT = "DOAJ only indexes open access journals which comply with the statement above. Please check and update the open access statement of your journal. You may return to this application at any time."
ERROR_OA_STATEMENT_URL = "Enter the URL for the journal’s Open Access statement page"
1 change: 1 addition & 0 deletions doajtest/selenium_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SeleniumTestCase(DoajTestCase):

@classmethod
def setUpClass(cls) -> None:
print("setUpClass")
super().setUpClass()
cls.originals = patch_config(cls.app_test, {
"DEBUG": False,
Expand Down
74 changes: 74 additions & 0 deletions doajtest/seleniumtest/application_form/test_fields_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


class Interact():

@classmethod
def clickButton(cls, selenium, js_click, button_class_selector=None, button_id_selector=None):
next_button = selenium.find_element(By.CLASS_NAME, button_class_selector if button_class_selector else button_id_selector)
assert next_button
selenium.execute_script("arguments[0].scrollIntoView(true);", next_button)
if button_class_selector:
js_click(f'.{button_class_selector}')
elif button_id_selector:
js_click(f'#{button_id_selector}')
else:
raise ValueError("Either 'button_class_selector' or 'button_id_selector' must be provided")


class TestFieldsCommon():

@classmethod
def find_question(cls, selenium, field_name):
question = selenium.find_element(By.CLASS_NAME, f'{field_name}__container');
assert question, f'{field_name} question container not found'
return question

@classmethod
def get_error_message(cls, selenium, field_name):
error_div_id = f"{field_name}_checkbox-errors"
error_xpath = f"//div[@id='{error_div_id}']/ul/li/p/small"

try:
error_element = WebDriverWait(selenium, 1).until(
EC.presence_of_element_located((By.XPATH, error_xpath))
)
if error_element.is_displayed():
return error_element.text.strip()
else:
return None
except TimeoutException:
return None

@classmethod
def test_if_required_text_or_number_field(self, field_name, expected_error_value=None):
# Step 1: Find the question with "field_name"
question = self.find_question(field_name)
assert question is not None, f"Question with field name '{field_name}' not found"

# Step 2: Assure the value = ""
# Assuming 'value' is a direct child of the question element
input_element = question.find_element(By.CSS_SELECTOR, 'input')
input_type = input_element.get_attribute('type')
if input_type == 'text' or input_type == 'number':
input_element.clear()
else:
raise ValueError(
f"Unexpected input type '{input_type}' for field '{field_name}'. Expected 'text' or 'number'.")

assert input_element.get_attribute('required') is not None, f"Field '{field_name}' is not marked as required"

# Step 3: Click Next Button
self.clickNextButton()

# Step 4: Assure the required error exists
error_message = self.get_error_message(field_name)
assert error_message is not None, f"Required error for field '{field_name}' not displayed"

# Step 5: Compare the error value to the provided value (optional)
if expected_error_value is not None:
assert error_message == expected_error_value, \
f"Error message for field '{field_name}' does not match expected value"
41 changes: 22 additions & 19 deletions doajtest/seleniumtest/application_form/test_oa_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,43 @@
from selenium.common.exceptions import NoSuchElementException

from doajtest.fixtures.url_path import URL_APPLY
from doajtest.fixtures.messages import ERROR_OA_STATEMENT
from doajtest.seleniumtest.application_form.test_fields_common import TestFieldsCommon
from doajtest.seleniumtest.application_form.test_fields_common import Interact
from doajtest.fixtures.messages import ERROR_OA_STATEMENT, ERROR_OA_STATEMENT_URL
from portality import models

from doajtest.fixtures.accounts import create_publisher_a


class ApplicationForm_OACompliance(SeleniumTestCase):

# go to application page before each test
def setUp(self):
super().setUp()
self.goto_application_page()

def goto_application_page(self, acc: models.Account = None):
publisher = acc or create_publisher_a()
selenium_helpers.login_by_acc(self.selenium, publisher)
selenium_helpers.goto(self.selenium, URL_APPLY)
return

def clickNext(self):
next_button_selector = ".nextBtn"
next_button = self.selenium.find_element(By.CLASS_NAME, 'nextBtn')
assert next_button
self.selenium.execute_script("arguments[0].scrollIntoView(true);", next_button)
self.js_click(next_button_selector)
def clickNextButton(self):
Interact.clickButton(selenium=self.selenium, js_click=self.js_click, button_class_selector="nextBtn",)

def test_oa_statement(self):
self.goto_application_page()
oa_statement_question = self.selenium.find_element(By.CLASS_NAME, 'boai__container')
assert oa_statement_question, "No OA Question container found"
# self.goto_application_page()
TestFieldsCommon.find_question(self.selenium, "boai")
no_radio_button_selector = "#boai-1"
self.js_click(no_radio_button_selector)
self.clickNext()
xpath_expression = f"//*[contains(text(), '{ERROR_OA_STATEMENT}')]"
oa_error = self.selenium.find_element(By.XPATH, xpath_expression)
assert oa_error, "OA error not displayed"
self.clickNextButton()
error = TestFieldsCommon.get_error_message(self.selenium, "boai")
assert error == ERROR_OA_STATEMENT

yes_radio_button_selector = "#boai-0"
self.js_click(yes_radio_button_selector)
self.clickNext()
xpath_expression = f"//*[contains(text(), '{ERROR_OA_STATEMENT}')]"
with pytest.raises(NoSuchElementException):
oa_error = self.selenium.find_element(By.XPATH, xpath_expression)
self.clickNextButton()
error = TestFieldsCommon.get_error_message(self.selenium, "boai")
assert error == None

# def test_oa_statement_url(self):
# TestFieldsCommon.test_if_required_text_or_number_field(field_name="oa_statement_url", error=ERROR_OA_STATEMENT_URL)

0 comments on commit dc9a111

Please sign in to comment.