Skip to content

Commit

Permalink
Merge pull request #574 from Amsterdam-Music-Lab/feature-aj/categoriz…
Browse files Browse the repository at this point in the history
…ation-selenium-test

Selenium test for categorization experiment
  • Loading branch information
albertas-jn authored Oct 7, 2023
2 parents c41139b + cfd00fb commit f9b5a58
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backend/experiment/rules/categorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from .base import Base
import random

from django.conf import settings


SCORE_AVG_MIN_TRAINING = 0.8

Expand Down Expand Up @@ -288,7 +290,7 @@ def plan_experiment(self, session):
participant=session.participant)

# Check if this participant already has a previous session
if current_sessions.count() > 1:
if current_sessions.count() > 1 and not settings.TESTING:
json_data = 'REPEAT'
else:
# Check wether a group falls behind in the count
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/Question/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ const Question = ({
)}
<h3 className={classNames({title: emphasizeTitle})}>{question.question}</h3>
<div className="question">{render(question.view)}</div>
{question.expected_response &&
/* Will only be visible when the backend settings has TESTING=True */
<p className="expected-response">{question.expected_response}</p>
}
</div>
);
};
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/Question/Question.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@


}

.expected-response {
display:none;
}
58 changes: 58 additions & 0 deletions tests/tests-selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def tearDown(self):
self.driver.quit()
#warnings.simplefilter("default", ResourceWarning)


def test_beatalignment(self):

self.driver.get("{}/{}".format(self.config['url']['root'], self.config['experiment_slugs']['beat_alignment']))
Expand Down Expand Up @@ -167,5 +168,62 @@ def test_eurovision(self):
self.driver.find_element(By.XPATH, '//*[text()="Play again"]')


def test_categorization(self):

self.driver.get("{}/{}".format(self.config['url']['root'], self.config['experiment_slugs']['categorization']))

# Explainer 1
self.driver.find_element(By.XPATH, "//div[text()=\"Ok\"]").click()

# If consent present, agree
if self.driver.find_element(By.TAG_NAME,"h4").text.lower() == "informed consent":
self.driver.find_element(By.XPATH, '//div[text()="I agree"]').click()

# What is your age?
el = WebDriverWait(self.driver, 3).until(presence_of_element_located((By.CSS_SELECTOR,"input[type='number']")))
el.send_keys(18)
self.driver.find_element(By.XPATH, '//*[text()="Continue"]').click()

# What is your gender
self.driver.find_element(By.CSS_SELECTOR, ".radio:nth-child(1)").click()
self.driver.find_element(By.XPATH, '//*[text()="Continue"]').click()

# What is your native language
select = Select(self.driver.find_element(By.TAG_NAME, 'select'))
select.select_by_value('nl')
self.driver.find_element(By.XPATH, '//*[text()="Continue"]').click()

# Please select your level of musical experience
self.driver.find_element(By.CSS_SELECTOR, ".radio:nth-child(1)").click()
self.driver.find_element(By.XPATH, '//*[text()="Continue"]').click()

# Explainer 2
WebDriverWait(self.driver, 5) \
.until(presence_of_element_located((By.XPATH, "//div[text()=\"Ok\"]"))) \
.click()

training_rounds = 20
testing_rounds = 80
training = True
for n in (training_rounds, testing_rounds):
for i in range(n):

WebDriverWait(self.driver, 5) \
.until(presence_of_element_located((By.CSS_SELECTOR, ".aha__play-button"))) \
.click()

expected_response = self.driver.execute_script('return document.querySelector(".expected-response").textContent')
button_to_click = self.driver.execute_script(f'return document.querySelector(\'input[value="{expected_response}"]\').parentElement')
WebDriverWait(self.driver, 5) \
.until(lambda x: False if "disabled" in button_to_click.get_attribute("class") else button_to_click) \
.click()

if training:
WebDriverWait(self.driver, 5) \
.until(presence_of_element_located((By.XPATH, "//div[text()=\"Ok\"]"))) \
.click()
training = False


if __name__ == '__main__':
unittest.main()

0 comments on commit f9b5a58

Please sign in to comment.