diff options
Diffstat (limited to 'yaksh/selenium_test')
-rw-r--r-- | yaksh/selenium_test/selenium_test.py | 141 | ||||
-rw-r--r-- | yaksh/selenium_test/test_load.py | 101 | ||||
-rw-r--r-- | yaksh/selenium_test/test_questions.json | 50 |
3 files changed, 292 insertions, 0 deletions
diff --git a/yaksh/selenium_test/selenium_test.py b/yaksh/selenium_test/selenium_test.py new file mode 100644 index 0000000..6a32b61 --- /dev/null +++ b/yaksh/selenium_test/selenium_test.py @@ -0,0 +1,141 @@ +from selenium import webdriver +from selenium.webdriver.common.keys import Keys + +import multiprocessing +import argparse + +class SeleniumTest(): + def __init__(self, url, quiz_name): + self.driver = webdriver.Firefox() + self.quiz_name = quiz_name + self.url = url + + def run_load_test(self, url, username, password): + try: + self.driver.delete_all_cookies() + self.driver.get(self.url) + self.login(username, password) + self.open_quiz() + self.close_quiz() + self.logout() + self.driver.close() + except Exception as e: + with open("/tmp/yaksh_load_test_log.txt", "ab") as f: + f.write('Username: {0}\nError: {1}\n'.format(username, e)) + self.driver.close() + + def login(self, username, password): + # get the username, password and submit form elements + username_elem = self.driver.find_element_by_id("id_username") + password_elem = self.driver.find_element_by_id("id_password") + submit_login_elem = self.driver.find_element_by_css_selector('button.btn') + + # Type in the username, password and submit form + username_elem.send_keys(username) + password_elem.send_keys(password) + submit_login_elem.click() + + def submit_answer(self, question_label, answer, loop_count=1): + self.driver.implicitly_wait(2) + for count in range(loop_count): + self.driver.find_element_by_link_text(question_label).click() + submit_answer_elem = self.driver.find_element_by_id("check") + self.driver.execute_script('editor.setValue({})'.format(answer)) + submit_answer_elem.click() + + def test_c_question(self, question_label): + # Incorrect Answer + loop_count = 10 + answer = '\"int add(int a, int b, int c)\\n{return;}\"' + self.submit_answer(question_label, answer, loop_count) + + # Infinite Loop + loop_count = 3 + answer = '\"int add(int a, int b, int c)\\n{while(1){}}\"' + self.submit_answer(question_label, answer, loop_count) + + # Correct Answer + loop_count = 1 + answer = '\"int add(int a, int b, int c)\\n{return a + b + c;}\"' + self.submit_answer(question_label, answer, loop_count) + + def test_python_question(self, question_label): + # Incorrect Answer + loop_count = 10 + answer = '\"def is_palindrome(s):\\n return s\"' + self.submit_answer(question_label, answer, loop_count) + + # Infinite Loop + loop_count = 3 + answer = '\"while True:\\n pass"' + self.submit_answer(question_label, answer, loop_count) + + # Correct Answer + loop_count = 1 + answer = '\"def is_palindrome(s):\\n return s[::-1] == s\"' + self.submit_answer(question_label, answer, loop_count) + + def test_bash_question(self, question_label): + # Incorrect Answer + loop_count = 10 + answer = '\"#!/bin/bash\\nls\"' + self.submit_answer(question_label, answer, loop_count) + + # Infinite Loop + loop_count = 3 + answer = '\"#!/bin/bash\\nwhile [ 1 ]; do : ; done\"' + self.submit_answer(question_label, answer, loop_count) + + # Correct Answer + loop_count = 1 + answer = '\"#!/bin/bash\\ncat $1| cut -d: -f2 | paste -d: $3 - $2\"' + self.submit_answer(question_label, answer, loop_count) + + def open_quiz(self): + # open quiz link + quiz_link_elem = self.driver.find_element_by_link_text(self.quiz_name).click() + self.driver.implicitly_wait(2) + + # Get page elements + start_exam_elem = self.driver.find_element_by_name("start") + start_exam_elem.click() + + quit_exam_elem = self.driver.find_element_by_name("quit") + + self.test_c_question(question_label=3) + self.test_python_question(question_label=1) + self.test_bash_question(question_label=2) + + def close_quiz(self): + self.driver.implicitly_wait(5) + quit_link_elem = self.driver.find_element_by_id("login_again") + quit_link_elem.click() + + def logout(self): + self.driver.implicitly_wait(5) + logout_link_elem = self.driver.find_element_by_id("logout") + logout_link_elem.click() + +def user_gen(url, ids): + return [(url, 'User%d'%x, 'User%d'%x) for x in ids] + +def wrap_run_load_test(args): + url = "http://yaksh.fossee.aero.iitb.ac.in/exam/" + quiz_name = "sel quiz 1" + selenium_test = SeleniumTest(url=url, quiz_name=quiz_name) + return selenium_test.run_load_test(*args) + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('start', type=int, help="Starting user id") + parser.add_argument("-n", "--number", type=int, default=10, help="number of users") + opts = parser.parse_args() + + url = "http://yaksh.fossee.aero.iitb.ac.in/exam/" + quiz_name = "yaksh demo quiz" + selenium_test = SeleniumTest(url=url, quiz_name=quiz_name) + pool = multiprocessing.Pool(opts.number) + pool.map(wrap_run_load_test, user_gen(url, range(opts.start, opts.start + opts.number))) + pool.close() + pool.join() + diff --git a/yaksh/selenium_test/test_load.py b/yaksh/selenium_test/test_load.py new file mode 100644 index 0000000..c552137 --- /dev/null +++ b/yaksh/selenium_test/test_load.py @@ -0,0 +1,101 @@ +import os +import signal +import subprocess +from datetime import datetime +import pytz +from threading import Thread +from selenium.webdriver.firefox.webdriver import WebDriver + +from django.contrib.staticfiles.testing import StaticLiveServerTestCase + +from yaksh.models import User, Profile, Question, Quiz, Course, QuestionPaper, TestCase +from selenium_test import SeleniumTest + +from yaksh.code_server import ServerPool, SERVER_POOL_PORT, SERVER_PORTS +from yaksh import settings +from yaksh.xmlrpc_clients import CodeServerProxy + +CUR_DIR = os.path.dirname(os.path.abspath(__file__)) + +class MySeleniumTests(StaticLiveServerTestCase): + @classmethod + def setUpClass(cls): + super(MySeleniumTests, cls).setUpClass() + # setup a demo code server + settings.code_evaluators['python']['standardtestcase'] = \ + "yaksh.python_assertion_evaluator.PythonAssertionEvaluator" + settings.code_evaluators['c']['standardtestcase'] = \ + "yaksh.cpp_code_evaluator.CppCodeEvaluator" + settings.code_evaluators['bash']['standardtestcase'] = \ + "yaksh.bash_code_evaluator.BashCodeEvaluator" + code_server_pool = ServerPool(ports=SERVER_PORTS, pool_port=SERVER_POOL_PORT) + cls.code_server_pool = code_server_pool + cls.code_server_thread = t = Thread(target=code_server_pool.run) + t.start() + + # Create set of demo users and profiles + mod_user = User.objects.create_user(username='yaksh_demo_mod', + password='yaksh_demo_mod', + email='yaksh_demo_mod@test.com' + ) + + user = User.objects.create_user(username='demo_yaksh_user', + password='demo_yaksh_user', + email='demo_yaksh_user@test.com' + ) + Profile.objects.create(user=user, + roll_number='demo_rn', + institute='IIT', + department='Chemical', + position='Student' + ) + + # create a course + course = Course.objects.create(name="Demo Load Course", + enrollment="Open Course", + creator=mod_user + ) + course.students.add(user) + + # create a Quiz + quiz = Quiz.objects.create( + start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc), + end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc), + duration=30, active=True, + attempts_allowed=1, time_between_attempts=0, + description='yaksh demo quiz', pass_criteria=0, + language='Python', prerequisite=None, + course=course + ) + + # create a question set + question = Question() + with open(os.path.join(CUR_DIR, 'test_questions.json'), 'r') as f: + question_list = f.read() + question.load_from_json(question_list, mod_user) + + # create question paper + question_paper = QuestionPaper.objects.create(quiz=quiz, + total_marks=5, + shuffle_questions=False + ) + # add fixed set of questions to the question paper + question_paper.fixed_questions.add(*Question.objects.all()) + + @classmethod + def tearDownClass(cls): + User.objects.all().delete() + Question.objects.all().delete() + Quiz.objects.all().delete() + Course.objects.all().delete() + + cls.code_server_pool.stop() + cls.code_server_thread.join() + + super(MySeleniumTests, cls).tearDownClass() + + def test_load(self): + url = '%s%s' % (self.live_server_url, '/exam/login/') + quiz_name = "yaksh demo quiz" + selenium_test = SeleniumTest(url=url, quiz_name=quiz_name) + selenium_test.run_load_test(url=url, username='demo_yaksh_user', password='demo_yaksh_user') diff --git a/yaksh/selenium_test/test_questions.json b/yaksh/selenium_test/test_questions.json new file mode 100644 index 0000000..f1cf02f --- /dev/null +++ b/yaksh/selenium_test/test_questions.json @@ -0,0 +1,50 @@ +[ + { + "snippet": "", + "testcase": [ + { + "test_case": "assert is_palindrome(\"hello\") == False" + }, + { + "test_case": "assert is_palindrome(\"nitin\") == True" + } + ], + "points": 3.0, + "test_case_type": "standardtestcase", + "description": "Write a function <code>is_palindrome(arg)</code> which will take one string argument. Return true if the argument is palindrome & false otherwise.\r\n<br><br>\r\nFor Example:\r\n<br>\r\n<code>is_palindrome(\"Hello\")</code> should return <code>False</code>\r\n<br>\r\n<br><br>", + "language": "python", + "active": true, + "type": "code", + "summary": "Python, check palindrome (Code)" + }, + { + "snippet": "#!/bin/bash", + "testcase": [ + { + "test_case": "/src/yaksh/bash_files/sample.sh,/src/yaksh/bash_files/sample.args" + } + ], + "points": 1.0, + "test_case_type": "standardtestcase", + "description": "Write a bash script that takes exactly two arguments and returns the sum of the numbers", + "language": "bash", + "active": true, + "type": "code", + "summary": "Bash Question Concatenate Files(Code)" + }, + { + "snippet": "", + "testcase": [ + { + "test_case": "c_cpp_files/main2.c" + } + ], + "points": 1.0, + "test_case_type": "standardtestcase", + "description": "Write a program to add 3 nos", + "language": "c", + "active": true, + "type": "code", + "summary": "selenium test" + } +] |