summaryrefslogtreecommitdiff
path: root/app/test/test_add_word.py
blob: c8d6d5726fc141e4dfad400bc1fb880ff4867f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- 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.CHROME)

HOME_PAGE = 'http://121.4.94.30:91/'


def has_punctuation(s):
    return [c for c in s if c in string.punctuation] != []
    
def test_add_word():
    driver.get(HOME_PAGE + 'logout')
    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
    driver.save_screenshot('./app/test/test_add_word_pic0.png')    
    elem = driver.find_element_by_id('text-content')
    essay_content = elem.text

    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) < 5 or has_punctuation(word):
        word = random.choice(essay_content.split())        
    elem.send_keys(word)
    driver.save_screenshot('./app/test/test_add_word_pic1.png')        
    elem = driver.find_element_by_xpath('//form[2]//input[1]') # 找到get所有词频按钮
    elem.click()

    elems = driver.find_elements_by_xpath("//input[@type='checkbox']")
    for elem in elems:
        if elem.get_attribute('name') == 'marked':
            elem.click()

    driver.save_screenshot('./app/test/test_add_word_pic2.png')
    elem = driver.find_element_by_xpath('//form[1]/input[1]') # 找到加入我的生词簿按钮
    elem.click()
 
    elems = driver.find_elements_by_xpath("//p[@id='new-word']")
    elems[0].location_once_scrolled_into_view
    driver.save_screenshot('./app/test/test_add_word_pic3.png')    

    found = 0
    for elem in elems:
        if word in elem.text:
            found = 1
            break

    assert found == 1
    
    driver.quit()