Compare commits

..

10 Commits

6 changed files with 110 additions and 86 deletions

4
.gitignore vendored
View File

@ -14,4 +14,8 @@ app/db/wordfreqapp.db
app/static/donate-the-author.jpg
app/static/donate-the-author-hidden.jpg
app/model/__pycache__/
app/test/__pycache__/
app/test/.pytest_cache/
app/test/pytest_report.html
app/test/assets
app/log.txt

View File

@ -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.
## 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

View File

@ -1,6 +1,8 @@
import pytest
import sqlite3
import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from pathlib import Path
@pytest.fixture
def URL():
@ -9,5 +11,24 @@ def URL():
@pytest.fixture
def driver():
my_driver = webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
return my_driver
return webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
@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)

33
app/test/helper.py Normal file
View File

@ -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

View File

@ -1,68 +1,23 @@
# -*- coding: utf-8 -*-
# Run the docker image using the following command:
# 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/'
import time
from helper import signup
def has_punctuation(s):
return [c for c in s if c in string.punctuation] != []
def test_add_word():
def test_add_word(URL, driver):
try:
driver.get(HOME_PAGE)
assert 'English Pal -' in driver.page_source
# 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
username, password = signup(URL, driver) # sign up a new account and automatically log in
time.sleep(1)
# enter the word in the text area
elem = driver.find_element_by_id('selected-words')
word = random.choice(essay_content.split())
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())
word = 'devour'
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()
elems = driver.find_elements_by_xpath("//input[@type='checkbox']")
for elem in elems:
if elem.get_attribute('name') == 'marked':
elem = driver.find_element_by_name('add-btn') # 找到"加入我的生词簿"按钮
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")
found = 0

View File

@ -1,35 +1,26 @@
# -*- coding: utf-8 -*-
# Run the docker image using the following command:
# docker run -d -p 4444:4444 selenium/standalone-chrome
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import logging
import time
import pytest
# driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
# HOME_PAGE = 'http://121.4.94.30:91/'
# 我使用的是Chrome浏览器所以我又通过安装webdriver-manager插件来配置浏览器的驱动
# 我通过 'pip install webdriver-manager==4.00' 命令安装webdriver-manager并且设置其版本为4.00
driver = webdriver.Chrome(ChromeDriverManager().install())
HOME_PAGE = 'http://127.0.0.1:5000/'
driver.implicitly_wait(10)
@pytest.mark.parametrize("test_input",("test1","'test2'","“test3”","it's","hello,I'm linshan","Happy New Year","My favorite book is 《Harry Potter》。"))
@pytest.mark.parametrize("expected",(["test1"],["test2"],["test3"],["it's"],["hello","i'm","linshan"],["happy","new","year"],["potter","harry","my","favorite","book","is"]))
def test_bug553_LinShan(test_input,expected):
@pytest.mark.parametrize("test_input,expected",
[("test1", "test1"),
("'test2'", "test2"),
("“test3”", "test3"),
("it's", "it's"),
("hello,I'm linshan", ["hello","i'm","linshan"]),
("Happy New Year", ["happy","new","year"]),
("My favorite book is 《Harry Potter》。", ["potter","harry","my","favorite","book","is"])])
def test_bug553_LinShan(test_input,expected, driver, URL):
try:
# 打开对应地址的网页
driver.get(HOME_PAGE)
driver.get(URL)
# 浏览器最大窗口化
driver.maximize_window()
@ -40,11 +31,11 @@ def test_bug553_LinShan(test_input,expected):
# 将测试的数据输入到主页的textarea里
driver.find_element_by_xpath("//textarea[@name='content']").send_keys(Keys.CONTROL, "a")
driver.find_element_by_xpath("//textarea[@name='content']").send_keys(test_input)
time.sleep(2)
time.sleep(1)
# 点击按钮获取单词
driver.find_element_by_xpath("//input[@value='get文章中的词频']").click()
time.sleep(2)
time.sleep(1)
# 获取筛选后的单词
words = driver.find_elements_by_xpath("//p/a")
@ -56,7 +47,7 @@ def test_bug553_LinShan(test_input,expected):
# 返回上一页网页
driver.find_element_by_xpath("//input[@value='确定并返回']").click()
time.sleep(2)
time.sleep(0.1)
except Exception as e:
# 输出异常信息