oTree Forum >

Writing a test.py for apps using JS

#1 by Aysegul_Engin

Hello all, 

I am struggling to write automated test for an App that uses JS and extra models. 

I used "questions_from_csv_complex: Read quiz questions from a CSV (complex version)" from oTree snippets by Chris Wickens. As the underlying structure. I just extended the options of the questions from 3 to 4 the rest is the same. 

I cannot seem to get pass the TypeError: player.raw_responses is None. 

I could not find any example tests.py for some inspiration. 

Can anyone give me a hint please? 

kind regards 
Ayşegül

#2 by Chris_oTree

Hi, can you show the full traceback and the relevant code?

#3 by Aysegul_Engin

Sure, thank you for the help. 

Here is the full traceback: 
Creating 'trivia_complex_auto_test_browser' session (test case 0)
Traceback (most recent call last):
  File "/Users/Aysegul/python_venv/oTree/bin/otree", line 8, in <module>
    sys.exit(execute_from_command_line())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/main.py", line 112, in execute_from_command_line
    call_command(cmd, *argv[2:])
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/cli/base.py", line 32, in call_command
    module.Command().outer_handle(args)
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/cli/base.py", line 10, in outer_handle
    return self.handle(**vars(options))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/cli/bots.py", line 54, in handle
    run_all_bots_for_session_config(
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/bots/runner.py", line 158, in run_all_bots_for_session_config
    run_bots(session_id, case_number=case_number)
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/bots/runner.py", line 125, in run_bots
    runner.play()
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/bots/runner.py", line 48, in play
    submission = bot.get_next_submit()
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/bots/bot.py", line 237, in get_next_submit
    return next(self.submits_generator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/bots/bot.py", line 245, in get_submits
    for submission in generator:
  File "/Users/Aysegul/Documents/Personal_Documents/oTree/oTree_Sync/overconfidence_dss/trivia_from_csv_complex/tests.py", line 11, in play_round
    submitted_answer = self.player.raw_responses()[random.choice(["optionA","optionB","optionC","optionD"])]
                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Aysegul/python_venv/oTree/lib/python3.11/site-packages/otree/database.py", line 597, in __getattribute__
    raise TypeError(msg)
TypeError: player.raw_responses is None. Accessing a null field is generally considered an error. Or, if this is intentional, use field_maybe_none()    

Please find the relevant code in the attachment.

#4 by Chris_oTree (edited )

tests.py should be something like this:

from . import *
import json
import random


class PlayerBot(Bot):

    def play_round(self):
        trials = Trial.filter(player=self.player)
        raw_responses_py = {str(trial.id): random.choice('ABCD') for trial in trials}
        raw_responses_json = json.dumps(raw_responses_py)
        yield Submission(Trivia, dict(raw_responses=raw_responses_json), check_html=False)

        if self.subsession.round_number == C.NUM_ROUNDS:
            yield Results, dict(feedback_confidence=6)

Your test's yield statement needs to submit data in the exact same format that the page submits it to the server. In this case, that would be a JSON encoded string into player.raw_responses. The way you can see this for yourself is to put "print(repr(player.raw_responses))" in your before_next_page. Whatever gets printed out there is the format of data your bot should submit.

#5 by Aysegul_Engin

Thank you Chris. That clarifies things a lot for me. Thank you for your support. 

kind regards, 
Ayşegül

#6 by Aysegul_Engin

Thank you Chris. That clarifies things a lot for me. Thank you for your support. 

kind regards, 
Ayşegül

Write a reply

Set forum username