Advertisement
Guest User

tests_al

a guest
Apr 8th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import subprocess
  2. import os
  3. import pytest
  4. import time
  5. from selenium import webdriver
  6. from selenium.webdriver.common.keys import Keys
  7. import time
  8.  
  9.  
  10. @pytest.fixture(scope="session")  # possibly, it should be "function" scope, but
  11. def driver():                     # starting a new driver each time could be costly
  12.     # setup
  13.  
  14.     options = webdriver.ChromeOptions()
  15.     options.add_argument("--headless")
  16.     options.add_argument('--no-sandbox')
  17.     options.add_argument('--window-size=1420,1080')
  18.     options.add_argument('--disable-gpu')
  19.     _driver = webdriver.Chrome(options=options, executable_path='./chromedriver')
  20.  
  21.     # driver is ready to be used
  22.     yield _driver
  23.  
  24.     # teardown
  25.     _driver.quit()
  26.  
  27.  
  28. @pytest.fixture(scope="session")
  29. def server_starting():
  30.  
  31.     print('starting server')
  32.     current_path = os.path.join(os.getcwd(), os.pardir)
  33.     front_path = os.path.join(current_path, 'front-end')
  34.  
  35.     back_path = os.path.join(current_path, 'back-end')
  36.  
  37.     subprocess.call(['python3', 'manage.py', 'migrate'], cwd=back_path)
  38.     print('migrations applied')
  39.     front_start = subprocess.Popen(['npm', 'start'], cwd=front_path)
  40.     back_start = subprocess.Popen(['python3', 'manage.py', 'runserver'], cwd=back_path)
  41.     time.sleep(15.)  # some extra time for servers to start
  42.  
  43.     yield "ok"  # indication of started servers
  44.  
  45.     # teardown
  46.  
  47.     front_start.kill()
  48.     back_start.kill()
  49.  
  50.  
  51.  
  52. def test_login_page(server_starting, driver):
  53.     """
  54.    check that out page has "Alemira" as title
  55.    """
  56.     print(driver.current_url)
  57.     driver.get("http://127.0.0.1:35000/login")
  58.     with open('tmp', 'w') as fout:
  59.         print("title", driver.title, file=fout)
  60.         print('url', driver.current_url, file=fout)
  61.     print("title", driver.title)
  62.     assert "Alemira" in driver.title
  63.  
  64.  
  65. def test_01(server_starting, driver):
  66.     driver.get("http://127.0.0.1:35000/login")
  67.     with open('tmp', 'w') as fout:
  68.         print("title", driver.title, file=fout)
  69.         print('url', driver.current_url, file=fout)
  70.     print(driver.current_url)
  71.     input_field = driver.find_element_by_id('id_username')
  72.     input_field.send_keys('Aglaya')
  73.     input_field.send_keys(Keys.ENTER)
  74.  
  75.     time.sleep(2)
  76.  
  77.     input_field = driver.find_element_by_name('id_password')
  78.     input_field.send_keys('12345')
  79.     input_field.send_keys(Keys.ENTER)
  80.  
  81.     error = driver.find_element_by_class_name("alert")
  82.     assert "please" in error.text.lower()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement