forked from learnpython/learnpython.in.ua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_selenium.py
More file actions
65 lines (49 loc) · 1.75 KB
/
Copy pathtest_selenium.py
File metadata and controls
65 lines (49 loc) · 1.75 KB
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
import os
import time
from unittest import TestCase
from flask import url_for
from selenium import webdriver
from werkzeug.routing import BuildError
from learnpython.app import app
SELENIUM_BROWSERS = {
'chrome': webdriver.Chrome,
'firefox': webdriver.Firefox,
'ie': webdriver.Ie,
'opera': webdriver.Opera,
}
class TestViewsWithSelenium(TestCase):
def setUp(self):
app.testing = True
self._ctx = app.test_request_context()
self._ctx.push()
def tearDown(self):
self._ctx.pop()
self.browser.quit()
@property
def browser(self):
if not hasattr(self, '_browser_cache'):
browser_name = os.environ.get('SELENIUM_BROWSER', 'firefox')
self.assertIn(browser_name, SELENIUM_BROWSERS)
browser = SELENIUM_BROWSERS[browser_name]()
setattr(self, '_browser_cache', browser)
return getattr(self, '_browser_cache')
@property
def host(self):
default = 'http://127.0.0.1:5001'
return os.environ.get('SELENIUM_URL', default).rstrip('/')
def url(self, url_rule, *args, **kwargs):
try:
url = url_for(url_rule, *args, **kwargs)
except BuildError:
url = url_rule
return self.host + url
def test_index(self):
index_url = self.url('index')
self.browser.get(index_url)
self.assertEqual(self.browser.current_url, index_url)
time.sleep(1)
element = self.browser.find_element_by_link_text('Learn Python')
self.assertEqual(element.get_attribute('href'), index_url)
def test_static(self):
self.browser.get(self.url('static', filename='css/screen.css'))
self.browser.get(self.url('static', filename='does_not_exists.exe'))