Organized js instrument tests somewhat more representably, including adding specific tests showing the use of a mock class to instrument non-existing secondary attributes

This commit is contained in:
Fredrik Wollsén 2019-08-15 15:49:39 +03:00
Родитель 778861d013
Коммит 10fee839de
6 изменённых файлов: 223 добавлений и 14 удалений

Просмотреть файл

@ -19,13 +19,14 @@ METHOD_CALLS = {
('nonExistingMethod1', 'call', '["hello","{\\\"world\\\":true}"]'),
}
TEST_PAGE = ("instrument_existing_window_property_"
"with_partially_existing_attributes.html")
TOP_URL = (
u"%s/js_instrument/instrument_non_existing_properties.html" % (
util.BASE_TEST_URL)
u"%s/js_instrument/%s" % (util.BASE_TEST_URL, TEST_PAGE)
)
class TestJSInstrumentNonExistingProperties(OpenWPMTest):
class TestJSInstrumentExistingWindowPropertyWithPartiallyExistingAttributes(OpenWPMTest):
def get_config(self, data_dir=""):
manager_params, browser_params = self.get_test_config(data_dir)
@ -56,8 +57,7 @@ class TestJSInstrumentNonExistingProperties(OpenWPMTest):
def test_instrument_object(self):
""" Ensure instrumentObject logs all property gets, sets, and calls """
db = self.visit(
'/js_instrument/instrument_non_existing_properties.html')
db = self.visit('/js_instrument/%s' % TEST_PAGE)
rows = db_utils.get_javascript_entries(db, all_columns=True)
# Check calls of non-recursive instrumentation

Просмотреть файл

@ -0,0 +1,64 @@
from __future__ import absolute_import
import re
from ..automation.utilities import db_utils
from . import utilities as util
from .openwpmtest import OpenWPMTest
GETS_AND_SETS = {
("existingProp", "get", "foo"),
("existingProp", "set", "blah1"),
("existingProp", "get", "blah1"),
("nonExistingProp1", "get", "undefined"),
("nonExistingProp1", "set", "blah2"),
("nonExistingProp1", "get", "blah2"),
}
METHOD_CALLS = {
('nonExistingMethod1', 'call', '["hello","{\\\"world\\\":true}"]'),
}
TEST_PAGE = "instrument_mock_window_property.html"
TOP_URL = (
u"%s/js_instrument/%s" % (util.BASE_TEST_URL, TEST_PAGE)
)
class TestJSInstrumentMockWindowProperty(OpenWPMTest):
def get_config(self, data_dir=""):
manager_params, browser_params = self.get_test_config(data_dir)
browser_params[0]['js_instrument'] = True
manager_params['testing'] = True
return manager_params, browser_params
def _check_calls(self, rows, symbol_prefix, doc_url, top_url):
"""Helper to check method calls and accesses in each frame"""
observed_gets_and_sets = set()
observed_calls = set()
for row in rows:
if not row['symbol'].startswith(symbol_prefix):
continue
symbol = re.sub(symbol_prefix, '', row['symbol'])
assert row['document_url'] == doc_url
assert row['top_level_url'] == top_url
if row['operation'] == 'get' or row['operation'] == 'set':
observed_gets_and_sets.add(
(symbol, row['operation'], row['value'])
)
else:
observed_calls.add(
(symbol, row['operation'], row['arguments'])
)
assert observed_calls == METHOD_CALLS
assert observed_gets_and_sets == GETS_AND_SETS
def test_instrument_object(self):
""" Ensure instrumentObject logs all property gets, sets, and calls """
db = self.visit('/js_instrument/%s' % TEST_PAGE)
rows = db_utils.get_javascript_entries(db, all_columns=True)
# Check calls of non-recursive instrumentation
self._check_calls(rows, 'window.alreadyInstantiatedMockClassInstance', TOP_URL, TOP_URL)
self._check_calls(rows, 'window.newMockClassInstance', TOP_URL, TOP_URL)

Просмотреть файл

@ -19,13 +19,13 @@ METHOD_CALLS = {
('nonExistingMethod1', 'call', '["hello","{\\\"world\\\":true}"]'),
}
TEST_PAGE = "instrument_non_existing_window_property.html"
TOP_URL = (
u"%s/js_instrument/instrument_non_existing_object.html" % (
util.BASE_TEST_URL)
u"%s/js_instrument/%s" % (util.BASE_TEST_URL, TEST_PAGE)
)
class TestJSInstrumentNonExistingObject(OpenWPMTest):
class TestJSInstrumentNonExistingWindowProperty(OpenWPMTest):
def get_config(self, data_dir=""):
manager_params, browser_params = self.get_test_config(data_dir)
@ -56,8 +56,7 @@ class TestJSInstrumentNonExistingObject(OpenWPMTest):
def test_instrument_object(self):
""" Ensure instrumentObject logs all property gets, sets, and calls """
db = self.visit(
'/js_instrument/instrument_non_existing_object.html')
db = self.visit('/js_instrument/%s' % TEST_PAGE)
rows = db_utils.get_javascript_entries(db, all_columns=True)
# Check calls of non-recursive instrumentation

Просмотреть файл

@ -1,10 +1,10 @@
<!doctype html>
<html>
<head>
<title>Test page for JS Instrument's instrumentObject method - specifically testing non-existing properties</title>
<title>Test page for JS Instrument's instrumentObject method - specifically instrumenting existing window property with partially existing attributes</title>
</head>
<body>
<h3>Test page for JS Instrument's instrumentObject method - specifically testing non-existing properties</h3>
<h3>Test page for JS Instrument's instrumentObject method - specifically instrumenting existing window property with partially existing attributes</h3>
<p> This page defines several types of objects and tests the js
instrument's instrumentObject() method. <br/>
NOTE: The platform config option `manager_params['testing']` must be set

Просмотреть файл

@ -0,0 +1,146 @@
<!doctype html>
<html>
<head>
<title>Test page for JS Instrument's instrumentObject method - specifically instrumenting a non-existing window property's attributes via a mock class / object</title>
</head>
<body>
<h3>Test page for JS Instrument's instrumentObject method - specifically instrumenting a non-existing window property's attributes via a mock class / object</h3>
<p> This page defines several types of objects and tests the js
instrument's instrumentObject() method. <br/>
NOTE: The platform config option `manager_params['testing']` must be set
to True otherwise the instrumentObject method won't be exposed to this
test script.
</p>
<script type="text/javascript">
function interactWithTestObjects() {
// access the class itself
console.log("MockClass",window.MockClass);
/*
* Interact with instrumented instance of the non-instrumented Mock class
*/
// get object
console.log("alreadyInstantiatedMockClassInstance",window.alreadyInstantiatedMockClassInstance);
// call object as method
try {
window.alreadyInstantiatedMockClassInstance('hello', {'world': true});
} catch (e) {
console.log("call object as method - caught exception: ", e);
}
// get and set nonExistingProp1 (data property 1)
try {
console.log("nonExistingProp1 before set",window.alreadyInstantiatedMockClassInstance.nonExistingProp1);
} catch (e) {
console.log("nonExistingProp1 before set - caught exception: ", e);
}
try {
window.alreadyInstantiatedMockClassInstance.nonExistingProp1 = 'blah1';
} catch (e) {
console.log("nonExistingProp1 set - caught exception: ", e);
}
try {
console.log("nonExistingProp1 after set",window.alreadyInstantiatedMockClassInstance.nonExistingProp1);
} catch (e) {
console.log("nonExistingProp1 after set - caught exception: ", e);
}
// call non-existing object method
try {
window.alreadyInstantiatedMockClassInstance.nonExistingMethod1('hello', {'world': true});
} catch (e) {
console.log("call object method - caught exception: ", e);
}
/*
* Interact with instance of the instrumented Mock class
*/
// get object
console.log("newMockClassInstance",window.newMockClassInstance);
// call object as method
try {
window.newMockClassInstance('hello', {'world': true});
} catch (e) {
console.log("call object as method - caught exception: ", e);
}
// get and set nonExistingProp1 (data property 1)
try {
console.log("nonExistingProp1 before set",window.newMockClassInstance.nonExistingProp1);
} catch (e) {
console.log("nonExistingProp1 before set - caught exception: ", e);
}
try {
window.newMockClassInstance.nonExistingProp1 = 'blah1';
} catch (e) {
console.log("nonExistingProp1 set - caught exception: ", e);
}
try {
console.log("nonExistingProp1 after set",window.newMockClassInstance.nonExistingProp1);
} catch (e) {
console.log("nonExistingProp1 after set - caught exception: ", e);
}
// call non-existing object method
try {
window.newMockClassInstance.nonExistingMethod1('hello', {'world': true});
} catch (e) {
console.log("call object method - caught exception: ", e);
}
}
// Set up Mock class
window.MockClass = function() {};
// Set up instance of the non-instrumented Mock class
window.alreadyInstantiatedMockClassInstance = new MockClass();
// Instrument mock class non-recursively
console.log("Instrumenting window.MockClass!");
// Instrument the window.MockClass property itself (not its object properties)
window.instrumentObject(window, "window", {
propertiesToInstrument: ["MockClass"],
});
// Instrument window.MockClass object properties
window.instrumentObject(window.MockClass.prototype, "window.MockClass", {
propertiesToInstrument: [
"existingProp",
"nonExistingProp1",
"nonExistingMethod1",
],
});
// Instrument the window.alreadyInstantiatedMockClassInstance property itself (not its object properties)
window.instrumentObject(window, "window", {
propertiesToInstrument: ["alreadyInstantiatedMockClassInstance"],
});
// Instrument window.alreadyInstantiatedMockClassInstance object properties
window.instrumentObject(window.alreadyInstantiatedMockClassInstance, "window.alreadyInstantiatedMockClassInstance", {
propertiesToInstrument: [
"existingProp",
"nonExistingProp1",
"nonExistingMethod1",
],
});
// Set up instance of the instrumented Mock class
window.newMockClassInstance = new MockClass();
// Instrument the window.newMockClassInstance property itself (not its object properties)
window.instrumentObject(window, "window", {
propertiesToInstrument: ["newMockClassInstance"],
});
interactWithTestObjects();
</script>
</body>
</html>

Просмотреть файл

@ -1,10 +1,10 @@
<!doctype html>
<html>
<head>
<title>Test page for JS Instrument's instrumentObject method - specifically testing non-existing properties</title>
<title>Test page for JS Instrument's instrumentObject method - specifically instrumenting non-existing window property</title>
</head>
<body>
<h3>Test page for JS Instrument's instrumentObject method - specifically testing non-existing properties</h3>
<h3>Test page for JS Instrument's instrumentObject method - specifically instrumenting non-existing window property</h3>
<p> This page defines several types of objects and tests the js
instrument's instrumentObject() method. <br/>
NOTE: The platform config option `manager_params['testing']` must be set