Refactoring
This commit is contained in:
parent
6f9086f07f
commit
e955a1bf21
|
@ -1,176 +0,0 @@
|
|||
live_js = r"""
|
||||
(function () {
|
||||
var headers = {
|
||||
"Etag": 1, "Last-Modified": 1, "Content-Length": 1, "Content-Type": 1 },
|
||||
resources = {},
|
||||
pendingRequests = {},
|
||||
currentLinkElements = {},
|
||||
oldLinkElements = {},
|
||||
interval = 1000,
|
||||
loaded = false,
|
||||
active = { "html": 1, "css": 1, "js": 1 };
|
||||
var Live = {
|
||||
heartbeat: function () {
|
||||
if (document.body) {
|
||||
if (!loaded) Live.loadresources();
|
||||
Live.checkForChanges();
|
||||
}
|
||||
setTimeout(Live.heartbeat, interval);
|
||||
},
|
||||
loadresources: function () {
|
||||
function isLocal(url) {
|
||||
var loc = document.location,
|
||||
reg = new RegExp("^\\.|^\/(?!\/)|^[\\w]((?!://).)*$|"
|
||||
+ loc.protocol + "//" + loc.host);
|
||||
return url.match(reg);
|
||||
}
|
||||
var scripts = document.getElementsByTagName("script"),
|
||||
links = document.getElementsByTagName("link"),
|
||||
uris = [];
|
||||
for (var i = 0; i < scripts.length; i++) {
|
||||
var script = scripts[i], src = script.getAttribute("src");
|
||||
if (src && isLocal(src))
|
||||
uris.push(src);
|
||||
if (src && src.match(/\blive.js#/)) {
|
||||
for (var type in active)
|
||||
active[type] = src.match("[#,|]" + type) != null
|
||||
if (src.match("notify"))
|
||||
alert("Live.js is loaded.");
|
||||
}
|
||||
}
|
||||
if (!active.js) uris = [];
|
||||
if (active.html) uris.push(document.location.href);
|
||||
for (var i = 0; i < links.length && active.css; i++) {
|
||||
var link = links[i], rel = link.getAttribute("rel"
|
||||
),href = link.getAttribute("href", 2);
|
||||
if (href && rel && rel.match(new RegExp("stylesheet", "i")
|
||||
) && isLocal(href)) {
|
||||
uris.push(href);
|
||||
currentLinkElements[href] = link;
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < uris.length; i++) {
|
||||
var url = uris[i];
|
||||
Live.getHead(url, function (url, info) {
|
||||
resources[url] = info;
|
||||
});
|
||||
}
|
||||
var head = document.getElementsByTagName("head")[0],
|
||||
style = document.createElement("style"),
|
||||
rule = "transition: all .3s ease-out;"
|
||||
css = [".livejs-loading * { ",
|
||||
rule, " -webkit-", rule, "-moz-", rule, "-o-", rule, "}"].join('');
|
||||
style.setAttribute("type", "text/css");
|
||||
head.appendChild(style);
|
||||
style.styleSheet ? style.styleSheet.cssText = css :
|
||||
style.appendChild(document.createTextNode(css));
|
||||
loaded = true;
|
||||
},
|
||||
checkForChanges: function () {
|
||||
for (var url in resources) {
|
||||
if (pendingRequests[url])
|
||||
continue;
|
||||
Live.getHead(url, function (url, newInfo) {
|
||||
var oldInfo = resources[url],
|
||||
hasChanged = false;
|
||||
resources[url] = newInfo;
|
||||
for (var header in oldInfo) {
|
||||
var oldValue = oldInfo[header],
|
||||
newValue = newInfo[header],
|
||||
contentType = newInfo["Content-Type"];
|
||||
switch (header.toLowerCase()) {
|
||||
case "etag":
|
||||
if (!newValue) break;
|
||||
default:
|
||||
hasChanged = oldValue != newValue;
|
||||
break;
|
||||
}
|
||||
if (hasChanged) {
|
||||
Live.refreshResource(url, contentType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
refreshResource: function (url, type) {
|
||||
switch (type.toLowerCase()) {
|
||||
case "text/css":
|
||||
var link = currentLinkElements[url],
|
||||
html = document.body.parentNode,
|
||||
head = link.parentNode,
|
||||
next = link.nextSibling,
|
||||
newLink = document.createElement("link");
|
||||
html.className = html.className.replace(/\s*livejs\-loading/gi, ''
|
||||
) + ' livejs-loading';
|
||||
newLink.setAttribute("type", "text/css");
|
||||
newLink.setAttribute("rel", "stylesheet");
|
||||
newLink.setAttribute("href", url + "?now=" + new Date() * 1);
|
||||
next ? head.insertBefore(newLink, next) : head.appendChild(newLink);
|
||||
currentLinkElements[url] = newLink;
|
||||
oldLinkElements[url] = link;
|
||||
Live.removeoldLinkElements();
|
||||
break;
|
||||
case "text/html":
|
||||
if (url != document.location.href)
|
||||
return;
|
||||
case "text/javascript":
|
||||
case "application/javascript":
|
||||
case "application/x-javascript":
|
||||
document.location.reload();
|
||||
}
|
||||
},
|
||||
removeoldLinkElements: function () {
|
||||
var pending = 0;
|
||||
for (var url in oldLinkElements) {
|
||||
try {
|
||||
var link = currentLinkElements[url],
|
||||
oldLink = oldLinkElements[url],
|
||||
html = document.body.parentNode,
|
||||
sheet = link.sheet || link.styleSheet,
|
||||
rules = sheet.rules || sheet.cssRules;
|
||||
if (rules.length >= 0) {
|
||||
oldLink.parentNode.removeChild(oldLink);
|
||||
delete oldLinkElements[url];
|
||||
setTimeout(function () {
|
||||
html.className = html.className.replace(
|
||||
/\s*livejs\-loading/gi, '');
|
||||
}, 100);
|
||||
}
|
||||
} catch (e) {
|
||||
pending++;
|
||||
}
|
||||
if (pending) setTimeout(Live.removeoldLinkElements, 50);
|
||||
}
|
||||
},
|
||||
getHead: function (url, callback) {
|
||||
pendingRequests[url] = true;
|
||||
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() :
|
||||
new ActiveXObject("Microsoft.XmlHttp");
|
||||
xhr.open("HEAD", url, true);
|
||||
xhr.onreadystatechange = function () {
|
||||
delete pendingRequests[url];
|
||||
if (xhr.readyState == 4 && xhr.status != 304) {
|
||||
xhr.getAllResponseHeaders();
|
||||
var info = {};
|
||||
for (var h in headers) {
|
||||
var value = xhr.getResponseHeader(h);
|
||||
if (h.toLowerCase() == "etag" && value)
|
||||
value = value.replace(/^W\//, '');
|
||||
if (h.toLowerCase() == "content-type" && value)
|
||||
value = value.replace(/^(.*?);.*?$/i, "$1");
|
||||
info[h] = value;
|
||||
}
|
||||
callback(url, info);
|
||||
}
|
||||
}
|
||||
xhr.send();
|
||||
}
|
||||
};
|
||||
if (document.location.protocol != "file:") {
|
||||
if (!window.liveJsLoaded)
|
||||
Live.heartbeat();
|
||||
window.liveJsLoaded = true;
|
||||
}
|
||||
})();
|
||||
"""
|
|
@ -1,8 +1,6 @@
|
|||
"""
|
||||
Manager for dealing with uploading/managing files on Amazon S3
|
||||
Methods for uploading/managing files on Amazon S3.
|
||||
"""
|
||||
from boto.s3.connection import S3Connection
|
||||
from boto.s3.key import Key
|
||||
from seleniumbase.config import settings
|
||||
|
||||
already_uploaded_files = []
|
||||
|
@ -10,10 +8,9 @@ already_uploaded_files = []
|
|||
|
||||
class S3LoggingBucket(object):
|
||||
"""
|
||||
A class to upload log files from tests to Amazon S3.
|
||||
A class for uploading log files from tests to Amazon S3.
|
||||
Those files can then be shared easily.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
log_bucket=settings.S3_LOG_BUCKET,
|
||||
|
@ -21,14 +18,17 @@ class S3LoggingBucket(object):
|
|||
selenium_access_key=settings.S3_SELENIUM_ACCESS_KEY,
|
||||
selenium_secret_key=settings.S3_SELENIUM_SECRET_KEY,
|
||||
):
|
||||
from boto.s3.connection import S3Connection
|
||||
|
||||
self.conn = S3Connection(selenium_access_key, selenium_secret_key)
|
||||
self.bucket = self.conn.get_bucket(log_bucket)
|
||||
self.bucket_url = bucket_url
|
||||
|
||||
def get_key(self, _name):
|
||||
def get_key(self, file_name):
|
||||
""" Create a new Key instance with the given name. """
|
||||
return Key(bucket=self.bucket, name=_name)
|
||||
from boto.s3.key import Key
|
||||
|
||||
return Key(bucket=self.bucket, name=file_name)
|
||||
|
||||
def get_bucket(self):
|
||||
""" Return the bucket being used. """
|
||||
|
@ -37,7 +37,7 @@ class S3LoggingBucket(object):
|
|||
def upload_file(self, file_name, file_path):
|
||||
"""Upload a given file from the file_path to the bucket
|
||||
with the new name/path file_name."""
|
||||
upload_key = Key(bucket=self.bucket, name=file_name)
|
||||
upload_key = self.get_key(file_name)
|
||||
content_type = "text/plain"
|
||||
if file_name.endswith(".html"):
|
||||
content_type = "text/html"
|
||||
|
|
|
@ -2228,7 +2228,7 @@ class BaseCase(unittest.TestCase):
|
|||
|
||||
def switch_to_default_content(self):
|
||||
"""Brings driver control outside the current iframe.
|
||||
(If driver control is inside an iframe, the driver control
|
||||
(If the driver control is inside an iframe, the driver control
|
||||
will be set to one level above the current frame. If the driver
|
||||
control is not currently in an iframe, nothing will happen.)"""
|
||||
self.__check_scope()
|
||||
|
@ -5224,8 +5224,9 @@ class BaseCase(unittest.TestCase):
|
|||
"""Same as self.wait_for_element_absent()
|
||||
Waits for an element to no longer appear in the HTML of a page.
|
||||
A hidden element still counts as appearing in the page HTML.
|
||||
If an element with "hidden" status is acceptable,
|
||||
use wait_for_element_not_visible() instead."""
|
||||
If waiting for elements to be hidden instead of nonexistent,
|
||||
use wait_for_element_not_visible() instead.
|
||||
"""
|
||||
self.__check_scope()
|
||||
if not timeout:
|
||||
timeout = settings.LARGE_TIMEOUT
|
||||
|
@ -5241,6 +5242,10 @@ class BaseCase(unittest.TestCase):
|
|||
):
|
||||
"""Same as self.assert_element_absent()
|
||||
Will raise an exception if the element stays present.
|
||||
A hidden element counts as a present element, which fails this assert.
|
||||
If you want to assert that elements are hidden instead of nonexistent,
|
||||
use assert_element_not_visible() instead.
|
||||
(Note that hidden elements are still present in the HTML of the page.)
|
||||
Returns True if successful. Default timeout = SMALL_TIMEOUT."""
|
||||
self.__check_scope()
|
||||
if not timeout:
|
||||
|
@ -8121,9 +8126,10 @@ class BaseCase(unittest.TestCase):
|
|||
self, selector, by=By.CSS_SELECTOR, timeout=None
|
||||
):
|
||||
"""Waits for an element to no longer appear in the HTML of a page.
|
||||
A hidden element still counts as appearing in the page HTML.
|
||||
If an element with "hidden" status is acceptable,
|
||||
use wait_for_element_not_visible() instead."""
|
||||
A hidden element counts as a present element, which fails this assert.
|
||||
If waiting for elements to be hidden instead of nonexistent,
|
||||
use wait_for_element_not_visible() instead.
|
||||
"""
|
||||
self.__check_scope()
|
||||
if not timeout:
|
||||
timeout = settings.LARGE_TIMEOUT
|
||||
|
@ -8137,8 +8143,12 @@ class BaseCase(unittest.TestCase):
|
|||
def assert_element_absent(
|
||||
self, selector, by=By.CSS_SELECTOR, timeout=None
|
||||
):
|
||||
"""Similar to wait_for_element_absent() - returns nothing.
|
||||
"""Similar to wait_for_element_absent()
|
||||
As above, will raise an exception if the element stays present.
|
||||
A hidden element counts as a present element, which fails this assert.
|
||||
If you want to assert that elements are hidden instead of nonexistent,
|
||||
use assert_element_not_visible() instead.
|
||||
(Note that hidden elements are still present in the HTML of the page.)
|
||||
Returns True if successful. Default timeout = SMALL_TIMEOUT."""
|
||||
self.__check_scope()
|
||||
if not timeout:
|
||||
|
@ -8169,7 +8179,7 @@ class BaseCase(unittest.TestCase):
|
|||
def assert_element_not_visible(
|
||||
self, selector, by=By.CSS_SELECTOR, timeout=None
|
||||
):
|
||||
"""Similar to wait_for_element_not_visible() - returns nothing.
|
||||
"""Similar to wait_for_element_not_visible()
|
||||
As above, will raise an exception if the element stays visible.
|
||||
Returns True if successful. Default timeout = SMALL_TIMEOUT."""
|
||||
self.__check_scope()
|
||||
|
|
|
@ -0,0 +1,181 @@
|
|||
###############################################################################
|
||||
# live_js - Reload web pages when changes are detected on the server side.
|
||||
###############################################################################
|
||||
|
||||
live_js = r"""
|
||||
(function () {
|
||||
var headers = {
|
||||
"Etag": 1, "Last-Modified": 1, "Content-Length": 1, "Content-Type": 1 },
|
||||
resources = {},
|
||||
pendingRequests = {},
|
||||
currentLinkElements = {},
|
||||
oldLinkElements = {},
|
||||
interval = 1000,
|
||||
loaded = false,
|
||||
active = { "html": 1, "css": 1, "js": 1 };
|
||||
var Live = {
|
||||
heartbeat: function () {
|
||||
if (document.body) {
|
||||
if (!loaded) Live.loadresources();
|
||||
Live.checkForChanges();
|
||||
}
|
||||
setTimeout(Live.heartbeat, interval);
|
||||
},
|
||||
loadresources: function () {
|
||||
function isLocal(url) {
|
||||
var loc = document.location,
|
||||
reg = new RegExp("^\\.|^\/(?!\/)|^[\\w]((?!://).)*$|"
|
||||
+ loc.protocol + "//" + loc.host);
|
||||
return url.match(reg);
|
||||
}
|
||||
var scripts = document.getElementsByTagName("script"),
|
||||
links = document.getElementsByTagName("link"),
|
||||
uris = [];
|
||||
for (var i = 0; i < scripts.length; i++) {
|
||||
var script = scripts[i], src = script.getAttribute("src");
|
||||
if (src && isLocal(src))
|
||||
uris.push(src);
|
||||
if (src && src.match(/\blive.js#/)) {
|
||||
for (var type in active)
|
||||
active[type] = src.match("[#,|]" + type) != null
|
||||
if (src.match("notify"))
|
||||
alert("Live.js is loaded.");
|
||||
}
|
||||
}
|
||||
if (!active.js) uris = [];
|
||||
if (active.html) uris.push(document.location.href);
|
||||
for (var i = 0; i < links.length && active.css; i++) {
|
||||
var link = links[i], rel = link.getAttribute("rel"
|
||||
),href = link.getAttribute("href", 2);
|
||||
if (href && rel && rel.match(new RegExp("stylesheet", "i")
|
||||
) && isLocal(href)) {
|
||||
uris.push(href);
|
||||
currentLinkElements[href] = link;
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < uris.length; i++) {
|
||||
var url = uris[i];
|
||||
Live.getHead(url, function (url, info) {
|
||||
resources[url] = info;
|
||||
});
|
||||
}
|
||||
var head = document.getElementsByTagName("head")[0],
|
||||
style = document.createElement("style"),
|
||||
rule = "transition: all .3s ease-out;"
|
||||
css = [".livejs-loading * { ",
|
||||
rule, " -webkit-", rule, "-moz-", rule, "-o-", rule, "}"].join('');
|
||||
style.setAttribute("type", "text/css");
|
||||
head.appendChild(style);
|
||||
style.styleSheet ? style.styleSheet.cssText = css :
|
||||
style.appendChild(document.createTextNode(css));
|
||||
loaded = true;
|
||||
},
|
||||
checkForChanges: function () {
|
||||
for (var url in resources) {
|
||||
if (pendingRequests[url])
|
||||
continue;
|
||||
Live.getHead(url, function (url, newInfo) {
|
||||
var oldInfo = resources[url],
|
||||
hasChanged = false;
|
||||
resources[url] = newInfo;
|
||||
for (var header in oldInfo) {
|
||||
var oldValue = oldInfo[header],
|
||||
newValue = newInfo[header],
|
||||
contentType = newInfo["Content-Type"];
|
||||
switch (header.toLowerCase()) {
|
||||
case "etag":
|
||||
if (!newValue) break;
|
||||
default:
|
||||
hasChanged = oldValue != newValue;
|
||||
break;
|
||||
}
|
||||
if (hasChanged) {
|
||||
Live.refreshResource(url, contentType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
refreshResource: function (url, type) {
|
||||
switch (type.toLowerCase()) {
|
||||
case "text/css":
|
||||
var link = currentLinkElements[url],
|
||||
html = document.body.parentNode,
|
||||
head = link.parentNode,
|
||||
next = link.nextSibling,
|
||||
newLink = document.createElement("link");
|
||||
html.className = html.className.replace(/\s*livejs\-loading/gi, ''
|
||||
) + ' livejs-loading';
|
||||
newLink.setAttribute("type", "text/css");
|
||||
newLink.setAttribute("rel", "stylesheet");
|
||||
newLink.setAttribute("href", url + "?now=" + new Date() * 1);
|
||||
next ? head.insertBefore(newLink, next) : head.appendChild(
|
||||
newLink);
|
||||
currentLinkElements[url] = newLink;
|
||||
oldLinkElements[url] = link;
|
||||
Live.removeoldLinkElements();
|
||||
break;
|
||||
case "text/html":
|
||||
if (url != document.location.href)
|
||||
return;
|
||||
case "text/javascript":
|
||||
case "application/javascript":
|
||||
case "application/x-javascript":
|
||||
document.location.reload();
|
||||
}
|
||||
},
|
||||
removeoldLinkElements: function () {
|
||||
var pending = 0;
|
||||
for (var url in oldLinkElements) {
|
||||
try {
|
||||
var link = currentLinkElements[url],
|
||||
oldLink = oldLinkElements[url],
|
||||
html = document.body.parentNode,
|
||||
sheet = link.sheet || link.styleSheet,
|
||||
rules = sheet.rules || sheet.cssRules;
|
||||
if (rules.length >= 0) {
|
||||
oldLink.parentNode.removeChild(oldLink);
|
||||
delete oldLinkElements[url];
|
||||
setTimeout(function () {
|
||||
html.className = html.className.replace(
|
||||
/\s*livejs\-loading/gi, '');
|
||||
}, 100);
|
||||
}
|
||||
} catch (e) {
|
||||
pending++;
|
||||
}
|
||||
if (pending) setTimeout(Live.removeoldLinkElements, 50);
|
||||
}
|
||||
},
|
||||
getHead: function (url, callback) {
|
||||
pendingRequests[url] = true;
|
||||
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() :
|
||||
new ActiveXObject("Microsoft.XmlHttp");
|
||||
xhr.open("HEAD", url, true);
|
||||
xhr.onreadystatechange = function () {
|
||||
delete pendingRequests[url];
|
||||
if (xhr.readyState == 4 && xhr.status != 304) {
|
||||
xhr.getAllResponseHeaders();
|
||||
var info = {};
|
||||
for (var h in headers) {
|
||||
var value = xhr.getResponseHeader(h);
|
||||
if (h.toLowerCase() == "etag" && value)
|
||||
value = value.replace(/^W\//, '');
|
||||
if (h.toLowerCase() == "content-type" && value)
|
||||
value = value.replace(/^(.*?);.*?$/i, "$1");
|
||||
info[h] = value;
|
||||
}
|
||||
callback(url, info);
|
||||
}
|
||||
}
|
||||
xhr.send();
|
||||
}
|
||||
};
|
||||
if (document.location.protocol != "file:") {
|
||||
if (!window.liveJsLoaded)
|
||||
Live.heartbeat();
|
||||
window.liveJsLoaded = true;
|
||||
}
|
||||
})();
|
||||
"""
|
Loading…
Reference in New Issue