From 3c118a53e2116a28aa55ad82dbea4a66bd75f462 Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Fri, 3 Dec 2021 09:08:45 +0800 Subject: [PATCH 1/3] add conftest.py --- app/test/conftest.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app/test/conftest.py diff --git a/app/test/conftest.py b/app/test/conftest.py new file mode 100644 index 0000000..29f6431 --- /dev/null +++ b/app/test/conftest.py @@ -0,0 +1,13 @@ +import pytest +from selenium import webdriver +from selenium.webdriver.common.desired_capabilities import DesiredCapabilities + +@pytest.fixture +def URL(): + return 'http://127.0.0.1:5000' # URL of the program + + +@pytest.fixture +def driver(): + my_driver = webdriver.Edge() # uncomment this line if you wish to run the test on your laptop + return my_driver -- 2.17.1 From f8a5cc2a40a51492e8fcb78d572037cec759f582 Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Fri, 3 Dec 2021 09:16:18 +0800 Subject: [PATCH 2/3] test: use localhost to test --- app/test/test_add_word.py | 15 +++++++-------- .../test_add_word_and_essay_does_not_change.py | 15 +++++++-------- app/test/test_delete_word.py | 14 +++++--------- app/test/test_login.py | 16 +++++++--------- app/test/test_login_security_fix.py | 11 +++++------ app/test/test_next_essay.py | 13 +++++-------- app/test/test_page_position.py | 13 ++++--------- app/test/test_signup.py | 14 +++++++------- 8 files changed, 47 insertions(+), 64 deletions(-) diff --git a/app/test/test_add_word.py b/app/test/test_add_word.py index a08c376..ffac643 100644 --- a/app/test/test_add_word.py +++ b/app/test/test_add_word.py @@ -6,19 +6,18 @@ from selenium.webdriver.common.desired_capabilities import DesiredCapabilities import random, time import string +import pytest -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) - -HOME_PAGE = 'http://121.4.94.30:91/' - +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) def has_punctuation(s): return [c for c in s if c in string.punctuation] != [] - -def test_add_word(): + +@pytest.mark.usefixtures +def test_add_word(URL, driver): try: - driver.get(HOME_PAGE) + driver.get(URL) assert 'English Pal -' in driver.page_source # login diff --git a/app/test/test_add_word_and_essay_does_not_change.py b/app/test/test_add_word_and_essay_does_not_change.py index 2746657..8ee81d0 100644 --- a/app/test/test_add_word_and_essay_does_not_change.py +++ b/app/test/test_add_word_and_essay_does_not_change.py @@ -3,22 +3,21 @@ # docker run -d -p 4444:4444 selenium/standalone-chrome from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - +import pytest 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/' +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) def has_punctuation(s): return [c for c in s if c in string.punctuation] != [] - -def test_add_word_and_essay_does_not_change(): + +@pytest.mark.usefixtures +def test_add_word_and_essay_does_not_change(URL, driver): try: - driver.get(HOME_PAGE) + driver.get(URL) assert 'English Pal -' in driver.page_source # login diff --git a/app/test/test_delete_word.py b/app/test/test_delete_word.py index 9dcad81..59128a7 100644 --- a/app/test/test_delete_word.py +++ b/app/test/test_delete_word.py @@ -3,25 +3,22 @@ # docker run -d -p 4444:4444 selenium/standalone-chrome from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - +import pytest import random, time import string # 调用本地chromedriver # driver = webdriver.Chrome(executable_path="D:\ChromeDriver\chromedriver.exe") # driver.get("http://127.0.0.1:5000/") -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) # driver.maximize_window() # HOME_PAGE = "http://127.0.0.1:5000/" - -HOME_PAGE = 'http://121.4.94.30:91/' - - +@pytest.mark.usefixtures def test_delete_word(): try: - driver.get(HOME_PAGE) + driver.get(URL) assert 'English Pal -' in driver.page_source # login elem = driver.find_element_by_link_text('登录') @@ -58,4 +55,3 @@ def test_delete_word(): finally: driver.quit() -# test_delete_word() diff --git a/app/test/test_login.py b/app/test/test_login.py index bb4df51..d711cb0 100644 --- a/app/test/test_login.py +++ b/app/test/test_login.py @@ -3,19 +3,17 @@ # docker run -d -p 4444:4444 selenium/standalone-chrome from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - +import pytest import random, string -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) - -HOME_PAGE = 'http://121.4.94.30:91/' +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) - -def test_login(): +@pytest.mark.usefixtures +def test_login(URL, driver): try: - driver.get(HOME_PAGE) + driver.get(URL) driver.save_screenshot('./app/test/test_login_pic0.png') assert 'English Pal -' in driver.page_source @@ -41,7 +39,7 @@ def test_login(): assert uname in driver.page_source # logout - driver.get(HOME_PAGE + 'logout') + driver.get(URL + 'logout') driver.save_screenshot('./app/test/test_login_pic3.png') # login diff --git a/app/test/test_login_security_fix.py b/app/test/test_login_security_fix.py index 461aaba..f17122a 100644 --- a/app/test/test_login_security_fix.py +++ b/app/test/test_login_security_fix.py @@ -3,17 +3,16 @@ # docker run -d -p 4444:4444 selenium/standalone-chrome from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - +import pytest import random, string -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) - -HOME_PAGE = 'http://121.4.94.30:91/' +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) +@pytest.mark.usefixtures def test_login_security_fix(): try: - driver.get(HOME_PAGE) + driver.get(URL) elem = driver.find_element_by_link_text('登录') elem.click() diff --git a/app/test/test_next_essay.py b/app/test/test_next_essay.py index 9814e58..3d16577 100644 --- a/app/test/test_next_essay.py +++ b/app/test/test_next_essay.py @@ -3,17 +3,14 @@ # docker run -d -p 4444:4444 selenium/standalone-chrome from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - +import pytest import random, string, time -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) -HOME_PAGE = 'http://121.4.94.30:91/' - - - -def test_next(): +@pytest.mark.usefixtures +def test_next(URL, driver): try: driver.get(HOME_PAGE) assert 'English Pal -' in driver.page_source diff --git a/app/test/test_page_position.py b/app/test/test_page_position.py index be6ece3..4eb8ef9 100644 --- a/app/test/test_page_position.py +++ b/app/test/test_page_position.py @@ -6,15 +6,10 @@ Click the Familiar or Unfamiliar button (current word frequency is 1), and the p from random import randint from selenium import webdriver +import pytest from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.desired_capabilities import DesiredCapabilities -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) - -HOME_PAGE = 'http://121.4.94.30:91/' - - def click_by_random(text): elements = driver.find_elements_by_link_text(text) # 点击单词表中的第一个单词的熟悉按钮 elements[randint(0, len(elements) - 1)].click() @@ -33,10 +28,10 @@ def get_scrollTop(): roll_height = driver.execute_script(js) return roll_height - -def test_page_position(): +@pytest.mark.usefixtures +def test_page_position(URL, driver): try: - driver.get(HOME_PAGE) + driver.get(URL) # login driver.find_element_by_link_text('登录').click() diff --git a/app/test/test_signup.py b/app/test/test_signup.py index a2664c5..d034107 100644 --- a/app/test/test_signup.py +++ b/app/test/test_signup.py @@ -3,19 +3,19 @@ # docker run -d -p 4444:4444 selenium/standalone-chrome from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - +import pytest import random, string -driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) -driver.implicitly_wait(10) - -HOME_PAGE = 'http://121.4.94.30:91/' +#driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) +#driver.implicitly_wait(10) -def test_signup(): + +@pytest.mark.usefixtures +def test_signup(URL, driver): try: - driver.get(HOME_PAGE) + driver.get(URL) driver.save_screenshot('test_signup_pic0.png') assert 'English Pal -' in driver.page_source -- 2.17.1 From 246ddcc62acd82943f58205b49ca5afa0b89316a Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Fri, 3 Dec 2021 09:18:34 +0800 Subject: [PATCH 3/3] Remove HOME_PAGE as we use URL now. --- app/test/test_next_essay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/test/test_next_essay.py b/app/test/test_next_essay.py index 3d16577..fb242d9 100644 --- a/app/test/test_next_essay.py +++ b/app/test/test_next_essay.py @@ -12,7 +12,7 @@ import random, string, time @pytest.mark.usefixtures def test_next(URL, driver): try: - driver.get(HOME_PAGE) + driver.get(URL) assert 'English Pal -' in driver.page_source # login -- 2.17.1