Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testdriver support for generate_test_report WebDriver command. #15305

Merged
merged 3 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions reporting/bufferSize.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Reporting: Buffer size</title>
<link rel="author" title="Paul Meyer" href="[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
// Test the buffer size (100) of ReportingObserver.
async_test(async function(test) {
paulmeyer90 marked this conversation as resolved.
Show resolved Hide resolved
for (i = 0; i != 110; ++i)
await test_driver.generate_test_report("" + i);

var observer = new ReportingObserver(function(reports) {
test.step(function() {
// Only (the most recent) 100 reports should be observed, even though
// 110 were buffered.
LukeZielinski marked this conversation as resolved.
Show resolved Hide resolved
assert_equals(reports.length, 100);
for(i = 0; i != 100; ++i) {
assert_equals(reports[i].body.message, "" + (i + 10));
}
});

test.done();
}, {buffered: true});
observer.observe();
}, "Buffer size");
</script>
25 changes: 25 additions & 0 deletions reporting/disconnect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Reporting: Disconnect</title>
<link rel="author" title="Paul Meyer" href="[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
async_test(async function(test) {
var observer = new ReportingObserver(function(reports, observer) {
test.step(function() {
assert_equals(reports.length, 1);
assert_equals(reports[0].body.message, "Test message.");
});
test.done();
});
observer.observe();

// The observer should still receive this report even though disconnect()
// is called immediately afterwards.
await test_driver.generate_test_report("Test message.");
observer.disconnect();
}, "Disconnect");
</script>
27 changes: 27 additions & 0 deletions reporting/generateTestReport.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Reporting: Generate Test Report</title>
<link rel="author" title="Paul Meyer" href="[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
// Test that the "generate_test_report" API works.
async_test(function(test) {
var observer = new ReportingObserver(function(reports) {
test.step(function() {
assert_equals(reports.length, 1);
// Ensure that the contents of the report are valid.
assert_equals(reports[0].type, "test");
assert_true(reports[0].url.endsWith("reporting/generateTestReport.html"));
assert_equals(reports[0].body.message, "Test message.");
});
test.done();
});
observer.observe();

// This should result in a "test" type report being generated and observed.
test_driver.generate_test_report("Test message.");
}, "Generate Test Report");
</script>
30 changes: 30 additions & 0 deletions reporting/nestedReport.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Reporting: Nested report</title>
<link rel="author" title="Paul Meyer" href="[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
// Test that reports can be generated within a ReportingObserver
// callback. These reports should be received by the same observer.
async_test(function(test) {
var step = 0;
var observer = new ReportingObserver(async function(reports, observer) {
test.step(function() {
assert_equals(reports.length, 1);
assert_equals(reports[0].body.message, "" + step);
});

++step;
if (step == 3)
test.done();

test_driver.generate_test_report("" + step);
});
observer.observe();

test_driver.generate_test_report("0");
}, "Nested report");
</script>
32 changes: 32 additions & 0 deletions reporting/order.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Reporting: Order</title>
<link rel="author" title="Paul Meyer" href="[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<p id="error">No error</p>
<script>
var count = 0;
async_test(function(test) {
var observer = new ReportingObserver(function(reports) {
test.step(function() {
// Reports should be received in the same order that they were
// generated.
for(i in reports) {
assert_equals(reports[i].body.message, "" + count++);
}
});

if (count == 10)
test.done();
});
observer.observe();

for (i = 0; i != 10; ++i)
test_driver.generate_test_report("" + i);
}, "Order");
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions resources/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@
*/
action_sequence: function(actions) {
return window.test_driver_internal.action_sequence(actions);
},

/**
* Generates a test report on the current page
*
* The generate_test_report function generates a report (to be observed
* by ReportingObserver) for testing purposes, as described in
* {@link https://w3c.github.io/reporting/#generate-test-report-command}
*
* @returns {Promise} fulfilled after the report is generated, or
* rejected if the report generation fails
*/
generate_test_report: function(message) {
return window.test_driver_internal.generate_test_report(message);
}
};

Expand Down Expand Up @@ -281,6 +295,17 @@
*/
action_sequence: function(actions) {
return Promise.reject(new Error("unimplemented"));
},

/**
* Generates a test report on the current page
*
* @param {String} message - the message to be contained in the report
* @returns {Promise} fulfilled after the report is generated, or
* rejected if the report generation fails
*/
generate_test_report: function(message) {
return Promise.reject(new Error("unimplemented"));
}
};
})();
13 changes: 12 additions & 1 deletion tools/wptrunner/wptrunner/executors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ def __init__(self, logger, protocol, test_window):
self.actions = {
"click": ClickAction(self.logger, self.protocol),
"send_keys": SendKeysAction(self.logger, self.protocol),
"action_sequence": ActionSequenceAction(self.logger, self.protocol)
"action_sequence": ActionSequenceAction(self.logger, self.protocol),
"generate_test_report": GenerateTestReportAction(self.logger, self.protocol)
}

def __call__(self, result):
Expand Down Expand Up @@ -593,3 +594,13 @@ def __call__(self, payload):
def get_element(self, selector):
element = self.protocol.select.element_by_selector(selector)
return element

class GenerateTestReportAction(object):
def __init__(self, logger, protocol):
self.logger = logger
self.protocol = protocol

def __call__(self, payload):
message = payload["message"]
self.logger.debug("Generating test report: %s" % message)
self.protocol.generate_test_report.generate_test_report(message)
15 changes: 13 additions & 2 deletions tools/wptrunner/wptrunner/executors/executorwebdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
ClickProtocolPart,
SendKeysProtocolPart,
ActionSequenceProtocolPart,
TestDriverProtocolPart)
TestDriverProtocolPart,
GenerateTestReportProtocolPart)
from ..testrunner import Stop

import webdriver as client
Expand Down Expand Up @@ -188,14 +189,24 @@ def send_message(self, message_type, status, message=None):
self.webdriver.execute_script("window.postMessage(%s, '*')" % json.dumps(obj))


class WebDriverGenerateTestReportProtocolPart(GenerateTestReportProtocolPart):
def setup(self):
self.webdriver = self.parent.webdriver

def generate_test_report(self, message):
json_message = {"message": message}
self.webdriver.send_session_command("POST", "reporting/generate_test_report", json_message)


class WebDriverProtocol(Protocol):
implements = [WebDriverBaseProtocolPart,
WebDriverTestharnessProtocolPart,
WebDriverSelectorProtocolPart,
WebDriverClickProtocolPart,
WebDriverSendKeysProtocolPart,
WebDriverActionSequenceProtocolPart,
WebDriverTestDriverProtocolPart]
WebDriverTestDriverProtocolPart,
WebDriverGenerateTestReportProtocolPart]

def __init__(self, executor, browser, capabilities, **kwargs):
super(WebDriverProtocol, self).__init__(executor, browser)
Expand Down
15 changes: 15 additions & 0 deletions tools/wptrunner/wptrunner/executors/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def element(self, element):
:param element: A protocol-specific handle to an element."""
pass


class SendKeysProtocolPart(ProtocolPart):
"""Protocol part for performing trusted clicks"""
__metaclass__ = ABCMeta
Expand All @@ -280,6 +281,20 @@ def send_keys(self, element, keys):
pass


class GenerateTestReportProtocolPart(ProtocolPart):
"""Protocol part for generating test reports"""
__metaclass__ = ABCMeta

name = "generate_test_report"

@abstractmethod
def generate_test_report(self, message):
"""Generate a test report.

:param message: The message to be contained in the report."""
pass


class ActionSequenceProtocolPart(ProtocolPart):
"""Protocol part for performing trusted clicks"""
__metaclass__ = ABCMeta
Expand Down
9 changes: 9 additions & 0 deletions tools/wptrunner/wptrunner/testdriver-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@
window.__wptrunner_message_queue.push({"type": "action", "action": "action_sequence", "actions": actions});
return pending_promise;
};

window.test_driver_internal.generate_test_report = function(message) {
const pending_promise = new Promise(function(resolve, reject) {
pending_resolve = resolve;
pending_reject = reject;
});
window.__wptrunner_message_queue.push({"type": "action", "action": "generate_test_report", "message": message});
return pending_promise;
};
})();