Compare commits
11 Commits
85a3faaa9f
...
db66b59513
Author | SHA1 | Date |
---|---|---|
|
db66b59513 | |
|
692d8cf453 | |
|
426d131f64 | |
|
e8fbccdcf7 | |
|
64b9c51fab | |
|
77a3adb546 | |
|
4f91659713 | |
|
083cbfd040 | |
|
0dc253bc19 | |
|
a4608db424 | |
|
0a63c5354a |
|
@ -14,4 +14,8 @@ app/db/wordfreqapp.db
|
||||||
app/static/donate-the-author.jpg
|
app/static/donate-the-author.jpg
|
||||||
app/static/donate-the-author-hidden.jpg
|
app/static/donate-the-author-hidden.jpg
|
||||||
app/model/__pycache__/
|
app/model/__pycache__/
|
||||||
|
app/test/__pycache__/
|
||||||
|
app/test/.pytest_cache/
|
||||||
|
app/test/pytest_report.html
|
||||||
|
app/test/assets
|
||||||
app/log.txt
|
app/log.txt
|
||||||
|
|
20
README.md
20
README.md
|
@ -129,6 +129,26 @@ We welcome feedback on EnglishPal. Feedback examples:
|
||||||
EnglishPal's bugs and improvement suggestions are recorded in [Bugzilla](http://118.25.96.118/bugzilla/buglist.cgi?bug_status=__all__&list_id=1302&order=Importance&product=EnglishPal&query_format=specific). Send (lanhui at zjnu.edu.cn) an email message for opening a Bugzilla account or reporting a bug.
|
EnglishPal's bugs and improvement suggestions are recorded in [Bugzilla](http://118.25.96.118/bugzilla/buglist.cgi?bug_status=__all__&list_id=1302&order=Importance&product=EnglishPal&query_format=specific). Send (lanhui at zjnu.edu.cn) an email message for opening a Bugzilla account or reporting a bug.
|
||||||
|
|
||||||
|
|
||||||
|
## End-to-end testing
|
||||||
|
|
||||||
|
We use the Selenium test framework to test our app.
|
||||||
|
|
||||||
|
In order to run the test, first we need to download a webdriver executable.
|
||||||
|
|
||||||
|
Microsoft Edge's webdriver can be downloaded from [microsoft-edge-tools-webdriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/). Make sure the version we download matches the version of the web browser installed on our laptop.
|
||||||
|
|
||||||
|
After extracting the downloaded zip file (e.g., edgedriver_win64.zip), rename msedgedriver.exe to MicrosoftWebDriver.exe.
|
||||||
|
|
||||||
|
Add MicrosoftWebDriver.exe's path to system's PATH variable.
|
||||||
|
|
||||||
|
Install the following dependencies too:
|
||||||
|
|
||||||
|
- pip install -U selenium==3.141.0
|
||||||
|
- pip install -U urllib3==1.26.2
|
||||||
|
|
||||||
|
Run English Pal first, then run the test using pytest as follows: pytest --html=pytest_report.html test_add_word.py
|
||||||
|
|
||||||
|
The above command will generate a HTML report file pytest_report.html after finishing executing test_add_word.py. Note: you need to install pytest-html package first: pip install pytest-html.
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,10 @@
|
||||||
alert('密码过于简单。(密码长度至少4位)');
|
alert('密码过于简单。(密码长度至少4位)');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (old_password.includes(' ') || new_password.includes(' ')) {
|
||||||
|
alert('输入不能包含空格!');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
$.post("/reset", {'old-password': old_password, 'new-password': new_password},
|
$.post("/reset", {'old-password': old_password, 'new-password': new_password},
|
||||||
function (response) {
|
function (response) {
|
||||||
if (response.status === '1') {
|
if (response.status === '1') {
|
||||||
|
|
|
@ -28,6 +28,10 @@ You're logged in already! <a href="/logout">Logout</a>.
|
||||||
alert('密码过于简单。(密码长度至少4位)');
|
alert('密码过于简单。(密码长度至少4位)');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (password.includes(' ') || password2.includes(' ')) {
|
||||||
|
alert('输入不能包含空格!');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
$.post("/signup", {'username': username, 'password': password},
|
$.post("/signup", {'username': username, 'password': password},
|
||||||
function (response) {
|
function (response) {
|
||||||
if (response.status === '0') {
|
if (response.status === '0') {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
import sqlite3
|
||||||
|
import time
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
from pathlib import Path
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def URL():
|
def URL():
|
||||||
|
@ -9,5 +11,24 @@ def URL():
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def driver():
|
def driver():
|
||||||
my_driver = webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
|
return webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
|
||||||
return my_driver
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def restore_sqlite_database():
|
||||||
|
'''
|
||||||
|
Automatically restore SQLite database file app/db/wordfreqapp.db
|
||||||
|
using SQL statements from app/static/wordfreqapp.sql
|
||||||
|
'''
|
||||||
|
con = sqlite3.connect('../db/wordfreqapp.db')
|
||||||
|
with con:
|
||||||
|
con.executescript('DROP TABLE IF EXISTS user;')
|
||||||
|
con.executescript('DROP TABLE IF EXISTS article;')
|
||||||
|
con.executescript(open('../static/wordfreqapp.sql', encoding='utf8').read())
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def restart_englishpal(restore_sqlite_database):
|
||||||
|
(Path(__file__).parent / '../main.py').touch()
|
||||||
|
time.sleep(1)
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
import uuid
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException
|
||||||
|
|
||||||
|
def signup(URL, driver):
|
||||||
|
username = 'TestUser' + str(uuid.uuid1()).split('-')[0].title()
|
||||||
|
password = '[Abc+123]'
|
||||||
|
|
||||||
|
driver.get(URL)
|
||||||
|
|
||||||
|
elem = driver.find_element_by_link_text('注册')
|
||||||
|
elem.click()
|
||||||
|
|
||||||
|
elem = driver.find_element_by_id('username')
|
||||||
|
elem.send_keys(username)
|
||||||
|
|
||||||
|
elem = driver.find_element_by_id('password')
|
||||||
|
elem.send_keys(password)
|
||||||
|
|
||||||
|
elem = driver.find_element_by_id('password2')
|
||||||
|
elem.send_keys(password)
|
||||||
|
|
||||||
|
elem = driver.find_element_by_class_name('btn') # 找到"注册"按钮
|
||||||
|
elem.click()
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(driver, 1).until(EC.alert_is_present())
|
||||||
|
driver.switch_to.alert.accept()
|
||||||
|
except (UnexpectedAlertPresentException, NoAlertPresentException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
return username, password
|
|
@ -1,68 +1,23 @@
|
||||||
# -*- coding: utf-8 -*-
|
import time
|
||||||
# Run the docker image using the following command:
|
from helper import signup
|
||||||
# docker run -d -p 4444:4444 selenium/standalone-chrome
|
|
||||||
from selenium import webdriver
|
|
||||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
||||||
|
|
||||||
import random, time
|
|
||||||
import string
|
|
||||||
|
|
||||||
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
|
|
||||||
driver.implicitly_wait(10)
|
|
||||||
|
|
||||||
HOME_PAGE = 'http://121.4.94.30:91/'
|
|
||||||
|
|
||||||
|
|
||||||
def has_punctuation(s):
|
def test_add_word(URL, driver):
|
||||||
return [c for c in s if c in string.punctuation] != []
|
|
||||||
|
|
||||||
def test_add_word():
|
|
||||||
try:
|
try:
|
||||||
driver.get(HOME_PAGE)
|
username, password = signup(URL, driver) # sign up a new account and automatically log in
|
||||||
assert 'English Pal -' in driver.page_source
|
time.sleep(1)
|
||||||
|
|
||||||
# login
|
|
||||||
elem = driver.find_element_by_link_text('登录')
|
|
||||||
elem.click()
|
|
||||||
|
|
||||||
uname = 'lanhui'
|
|
||||||
password = 'l0ve1t'
|
|
||||||
elem = driver.find_element_by_name('username')
|
|
||||||
elem.send_keys(uname)
|
|
||||||
|
|
||||||
elem = driver.find_element_by_name('password')
|
|
||||||
elem.send_keys(password)
|
|
||||||
|
|
||||||
elem = driver.find_element_by_xpath('//form[1]/p[3]/input[1]') # 找到登录按钮
|
|
||||||
elem.click()
|
|
||||||
|
|
||||||
assert 'EnglishPal Study Room for ' + uname in driver.title
|
|
||||||
|
|
||||||
# get essay content
|
|
||||||
elem = driver.find_element_by_id('text-content')
|
|
||||||
essay_content = elem.text
|
|
||||||
|
|
||||||
|
# enter the word in the text area
|
||||||
elem = driver.find_element_by_id('selected-words')
|
elem = driver.find_element_by_id('selected-words')
|
||||||
word = random.choice(essay_content.split())
|
word = 'devour'
|
||||||
while 'font>' in word or 'br>' in word or 'p>' in word or len(word) < 6 or has_punctuation(word):
|
|
||||||
word = random.choice(essay_content.split())
|
|
||||||
|
|
||||||
elem.send_keys(word)
|
elem.send_keys(word)
|
||||||
|
|
||||||
elem = driver.find_element_by_xpath('//form[1]//input[1]') # 找到get所有词频按钮
|
elem = driver.find_element_by_xpath('//form[1]//button[1]') # 找到"把生词加入我的生词库"按钮
|
||||||
elem.click()
|
elem.click()
|
||||||
|
|
||||||
elems = driver.find_elements_by_xpath("//input[@type='checkbox']")
|
elem = driver.find_element_by_name('add-btn') # 找到"加入我的生词簿"按钮
|
||||||
for elem in elems:
|
|
||||||
if elem.get_attribute('name') == 'marked':
|
|
||||||
elem.click()
|
elem.click()
|
||||||
|
|
||||||
elem = driver.find_element_by_name('add-btn') # 找到加入我的生词簿按钮
|
|
||||||
elem.click()
|
|
||||||
|
|
||||||
driver.refresh()
|
|
||||||
driver.refresh()
|
|
||||||
driver.refresh()
|
|
||||||
elems = driver.find_elements_by_xpath("//p[@class='new-word']/a")
|
elems = driver.find_elements_by_xpath("//p[@class='new-word']/a")
|
||||||
|
|
||||||
found = 0
|
found = 0
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
import pytest
|
||||||
|
from selenium import webdriver
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.common.keys import Keys
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from selenium.common.exceptions import NoSuchElementException, TimeoutException
|
||||||
|
|
||||||
|
# 使用 Pytest 的 fixture 初始化 WebDriver 和 URL
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def driver():
|
||||||
|
# 初始化WebDriver
|
||||||
|
driver = webdriver.Edge(executable_path=r'D:\codeapp\python-3.8.5\Lib\site-packages\selenium\webdriver\edge\MicrosoftWebDriver.exe')
|
||||||
|
driver.implicitly_wait(10)
|
||||||
|
yield driver # yield 语句之后的代码在测试用例执行完毕后运行
|
||||||
|
driver.quit() # 测试用例执行完毕后关闭 WebDriver
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def URL():
|
||||||
|
return 'http://127.0.0.1:5000'
|
||||||
|
|
||||||
|
# 测试登录页面输入密码包含空格的情况
|
||||||
|
def test_login_password_with_space(driver, URL):
|
||||||
|
try:
|
||||||
|
driver.get(URL+"/login")
|
||||||
|
|
||||||
|
# 输入用户名
|
||||||
|
username_elem = driver.find_element_by_id('username')
|
||||||
|
username_elem.send_keys("test_user")
|
||||||
|
|
||||||
|
# 输入包含空格的密码
|
||||||
|
password_elem = driver.find_element_by_id('password')
|
||||||
|
password_elem.send_keys("password with space")
|
||||||
|
|
||||||
|
# 提交登录表单
|
||||||
|
elem = driver.find_element_by_class_name('btn') # 找到提交按钮
|
||||||
|
elem.click()
|
||||||
|
|
||||||
|
# 显式等待直到警告框出现
|
||||||
|
WebDriverWait(driver, 10).until(EC.alert_is_present())
|
||||||
|
|
||||||
|
# 检查是否弹出警告框
|
||||||
|
alert = driver.switch_to.alert
|
||||||
|
assert "输入不能包含空格!" in alert.text
|
||||||
|
except (NoSuchElementException, TimeoutException) as e:
|
||||||
|
pytest.fail("页面元素未找到或超时: {}".format(e))
|
||||||
|
|
||||||
|
|
||||||
|
# 测试注册页面输入密码包含空格的情况
|
||||||
|
|
||||||
|
def test_signup_password_with_space(driver, URL):
|
||||||
|
try:
|
||||||
|
driver.get(URL+"/signup")
|
||||||
|
|
||||||
|
# 输入用户名
|
||||||
|
username_elem = driver.find_element_by_id('username')
|
||||||
|
username_elem.send_keys("new_user")
|
||||||
|
|
||||||
|
# 输入包含空格的密码
|
||||||
|
password_elem = driver.find_element_by_id('password')
|
||||||
|
password_elem.send_keys("password with space")
|
||||||
|
|
||||||
|
# 再次输入密码
|
||||||
|
password2_elem = driver.find_element_by_id('password2')
|
||||||
|
password2_elem.send_keys("password with space")
|
||||||
|
|
||||||
|
# 提交注册表单
|
||||||
|
elem = driver.find_element_by_class_name('btn') # 找到提交按钮
|
||||||
|
elem.click()
|
||||||
|
|
||||||
|
# 显式等待直到警告框出现
|
||||||
|
WebDriverWait(driver, 10).until(EC.alert_is_present())
|
||||||
|
|
||||||
|
# 检查是否弹出警告框
|
||||||
|
alert = driver.switch_to.alert
|
||||||
|
assert "输入不能包含空格!" in alert.text
|
||||||
|
except (NoSuchElementException, TimeoutException) as e:
|
||||||
|
pytest.fail("页面元素未找到或超时: {}".format(e))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 测试重设密码页面输入新密码包含空格的情况
|
||||||
|
|
||||||
|
def test_reset_password_with_space(driver, URL):
|
||||||
|
try:
|
||||||
|
driver.get(URL+"/reset")
|
||||||
|
|
||||||
|
# 输入用户名
|
||||||
|
username_elem = driver.find_element_by_id('username')
|
||||||
|
username_elem.send_keys("test_user")
|
||||||
|
|
||||||
|
# 输入包含空格的密码
|
||||||
|
password_elem = driver.find_element_by_id('password')
|
||||||
|
password_elem.send_keys("password with space")
|
||||||
|
|
||||||
|
# 提交重设密码表单
|
||||||
|
elem = driver.find_element_by_class_name('btn') # 找到提交按钮
|
||||||
|
elem.click()
|
||||||
|
|
||||||
|
# 显式等待直到警告框出现
|
||||||
|
WebDriverWait(driver, 10).until(EC.alert_is_present())
|
||||||
|
|
||||||
|
# 检查是否弹出警告框
|
||||||
|
alert = driver.switch_to.alert
|
||||||
|
assert "输入不能包含空格!" in alert.text
|
||||||
|
except (NoSuchElementException, TimeoutException) as e:
|
||||||
|
pytest.fail("页面元素未找到或超时: {}".format(e))
|
|
@ -1,35 +0,0 @@
|
||||||
from selenium import webdriver
|
|
||||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
||||||
from selenium.webdriver.common.keys import Keys
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
# 初始化WebDriver
|
|
||||||
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
|
|
||||||
driver.implicitly_wait(10)
|
|
||||||
|
|
||||||
|
|
||||||
# 测试登录页面输入密码包含空格的情况
|
|
||||||
def test_login_password_with_space():
|
|
||||||
try:
|
|
||||||
driver.get("http://121.4.94.30:91/login")
|
|
||||||
|
|
||||||
# 输入用户名
|
|
||||||
username_elem = driver.find_element_by_name('username')
|
|
||||||
username_elem.send_keys("test_user")
|
|
||||||
|
|
||||||
# 输入包含空格的密码
|
|
||||||
password_elem = driver.find_element_by_name('password')
|
|
||||||
password_elem.send_keys("password with space")
|
|
||||||
|
|
||||||
# 提交登录表单
|
|
||||||
password_elem.send_keys(Keys.RETURN)
|
|
||||||
|
|
||||||
# 等待一段时间确保页面加载完成
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
# 检查是否弹出警告框
|
|
||||||
alert = driver.switch_to.alert
|
|
||||||
assert "输入不能包含空格!" in alert.text
|
|
||||||
finally:
|
|
||||||
driver.quit()
|
|
|
@ -1,35 +0,0 @@
|
||||||
from selenium import webdriver
|
|
||||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
||||||
from selenium.webdriver.common.keys import Keys
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
# 初始化WebDriver
|
|
||||||
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
|
|
||||||
driver.implicitly_wait(10)
|
|
||||||
|
|
||||||
|
|
||||||
# 测试重设密码页面输入旧密码或新密码包含空格的情况
|
|
||||||
def test_reset_password_with_space():
|
|
||||||
try:
|
|
||||||
driver.get("http://121.4.94.30:91/reset")
|
|
||||||
|
|
||||||
# 输入旧密码
|
|
||||||
old_password_elem = driver.find_element_by_name('old_password')
|
|
||||||
old_password_elem.send_keys("old password with space")
|
|
||||||
|
|
||||||
# 输入新密码
|
|
||||||
new_password_elem = driver.find_element_by_name('new_password')
|
|
||||||
new_password_elem.send_keys("new password with space")
|
|
||||||
|
|
||||||
# 提交重设密码表单
|
|
||||||
new_password_elem.send_keys(Keys.RETURN)
|
|
||||||
|
|
||||||
# 等待一段时间确保页面加载完成
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
# 检查是否弹出警告框
|
|
||||||
alert = driver.switch_to.alert
|
|
||||||
assert "输入不能包含空格!" in alert.text
|
|
||||||
finally:
|
|
||||||
driver.quit()
|
|
|
@ -1,39 +0,0 @@
|
||||||
from selenium import webdriver
|
|
||||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
||||||
from selenium.webdriver.common.keys import Keys
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
# 初始化WebDriver
|
|
||||||
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
|
|
||||||
driver.implicitly_wait(10)
|
|
||||||
|
|
||||||
|
|
||||||
# 测试注册页面输入密码包含空格的情况
|
|
||||||
def test_signup_password_with_space():
|
|
||||||
try:
|
|
||||||
driver.get("http://121.4.94.30:91/signup")
|
|
||||||
|
|
||||||
# 输入用户名
|
|
||||||
username_elem = driver.find_element_by_name('username')
|
|
||||||
username_elem.send_keys("new_user")
|
|
||||||
|
|
||||||
# 输入包含空格的密码
|
|
||||||
password_elem = driver.find_element_by_name('password')
|
|
||||||
password_elem.send_keys("password with space")
|
|
||||||
|
|
||||||
# 再次输入密码
|
|
||||||
password2_elem = driver.find_element_by_name('password2')
|
|
||||||
password2_elem.send_keys("password with space")
|
|
||||||
|
|
||||||
# 提交注册表单
|
|
||||||
password2_elem.send_keys(Keys.RETURN)
|
|
||||||
|
|
||||||
# 等待一段时间确保页面加载完成
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
# 检查是否弹出警告框
|
|
||||||
alert = driver.switch_to.alert
|
|
||||||
assert "输入不能包含空格!" in alert.text
|
|
||||||
finally:
|
|
||||||
driver.quit()
|
|
Loading…
Reference in New Issue