项目简介:
本次实现的在线oj主要功能描述:用户可以进入首页,点击编程链接进入题单列表页,题目按照难度有分类,用户可自行选择任意的题目进行练习,提交代码后,会展示代码执行的结果,告知用户代码是否正确
哈密瓜迷在线OJ网址: 哈密瓜迷在线OJ
测试导图:
本次测试主要针对主要功能进行简单的自动化UI测试.使用到的工具有selenium和unittest,编程语言为python
测试实施:
默认欢迎界面:
import unittest
from selenium import webdriver
import os
import time
class TestWelcomePage(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def tearDown(self):
self.driver.quit()
def SavePicture(self, file_name):
if not os.path.exists(r'D:\PythonCode\pythonProject\ojTest\image'):
os.makedirs(r'D:\PythonCode\pythonProject\ojTest\image')
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
self.driver.get_screenshot_as_file(r'D:\PythonCode\pythonProject\ojTest\image' + now + '-' + file_name)
def test_display(self):
self.driver.get("http://123.207.198.20:8080/")
try:
self.driver.implicitly_wait(3)
elem = self.driver.find_element_by_css_selector("body > div > div > h1")
self.assertEqual(elem.text, "嗨! 朋友")
self.driver.implicitly_wait(3)
elem = self.driver.find_element_by_css_selector("body > div > div > p")
self.assertEqual(elem.text, "欢迎来到哈密瓜迷的在线OJ")
self.driver.implicitly_wait(3)
elem = self.driver.find_element_by_css_selector("body > div > div > a")
self.assertEqual(elem.text, "点击我编程啦!")
except AssertionError:
self.SavePicture("欢迎界面展示.png")
def test_link(self):
self.driver.get("http://123.207.198.20:8080/")
try:
self.driver.implicitly_wait(3)
elem = self.driver.find_element_by_css_selector("body > div > div > a")
elem.click()
self.driver.implicitly_wait(3)
self.assertEqual(self.driver.title, "在线OJ-题单")
except AssertionError:
self.SavePicture("欢迎界面跳转.png")
题单列表界面:
from selenium import webdriver
import unittest
import time
import os
class TestQuesListPage(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def tearDown(self):
self.driver.quit()
def SavePicture(self, file_name):
if not os.path.exists(r'D:\PythonCode\pythonProject\ojTest\image'):
os.makedirs(r'D:\PythonCode\pythonProject\ojTest\image')
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
self.driver.get_screenshot_as_file(r'D:\PythonCode\pythonProject\ojTest\image' + now + '-' + file_name)
def test_display(self):
self.driver.get("http://123.207.198.20:8080/topic_list")
try:
self.driver.implicitly_wait(3)
elems: list = self.driver.find_elements_by_css_selector(
"body > div > div.question_list > table > tbody > tr")
self.assertTrue(len(elems) > 2)
except AssertionError:
self.SavePicture("题单界面展示.png")
def test_link(self):
self.driver.get("http://123.207.198.20:8080/topic_list")
try:
self.driver.implicitly_wait(3)
elem = self.driver.find_element_by_css_selector("body > div > div.question_list > table > tbody > "
"tr:nth-child(2) > td:nth-child(2) > a")
elem.click()
self.assertEqual(self.driver.title, "1.判断回文数字")
except AssertionError:
self.SavePicture("题单界面跳转.png")
用户编程界面:
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
import time
class TestCodePage(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def tearDown(self):
self.driver.quit()
def SavePicture(self, file_name):
if not os.path.exists(r'D:\PythonCode\pythonProject\ojTest\image'):
os.makedirs(r'D:\PythonCode\pythonProject\ojTest\image')
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
self.driver.get_screenshot_as_file(r'D:\PythonCode\pythonProject\ojTest\image' + now + '-' + file_name)
@unittest.skip("skipping")
def test_display(self):
self.driver.get("http://123.207.198.20:8080/topic_gain/1")
try:
self.driver.implicitly_wait(3)
# elem = self.driver.find_element(By.CLASS_NAME, "btn-submit")
elem = self.driver.find_element_by_css_selector("body > div > div.part2 > button")
self.assertEqual(elem.text, "提交代码")
except AssertionError:
self.SavePicture("编程界面展示.png")
def test_commit(self):
self.driver.get("http://123.207.198.20:8080/topic_gain/1")
self.driver.maximize_window()
try:
self.driver.implicitly_wait(3)
self.driver.find_element_by_css_selector("body > div > div.part2 > button").click()
elem = self.driver.find_element_by_css_selector("body > div > div.part2 > div > p")
self.assertEqual(elem.text, "运行成功")
except AssertionError:
self.SavePicture("编程界面提交.png")
使用unittestreport生成html报告:
import unittest
from unittestreport import TestRunner
if __name__ == '__main__':
load = unittest.TestLoader()
suite = load.discover(r"D:\PythonCode\pythonProject\ojTest")
runner = TestRunner(suite,
tester='哈密瓜迷',
title='哈密瓜迷的在线OJ测试报告',
desc='简单的主要要功能的UI测试'
)
runner.run()