puppeteer: Migrate copy paste test from casper.

This commit is contained in:
Dinesh 2020-08-20 16:15:18 +00:00 committed by Tim Abbott
parent a32c5e5525
commit 5a73c2ca42
2 changed files with 159 additions and 213 deletions

View File

@ -1,213 +0,0 @@
"use strict";
var common = require("../casper_lib/common.js");
common.start_and_log_in();
casper.then(function () {
casper.test.info("Sending messages");
});
// setup environment: several messages to different topics
common.then_send_many([
{stream: "Verona", subject: "copy-paste-subject #1", content: "copy paste test A"},
{stream: "Verona", subject: "copy-paste-subject #1", content: "copy paste test B"},
{stream: "Verona", subject: "copy-paste-subject #2", content: "copy paste test C"},
{stream: "Verona", subject: "copy-paste-subject #2", content: "copy paste test D"},
{stream: "Verona", subject: "copy-paste-subject #2", content: "copy paste test E"},
{stream: "Verona", subject: "copy-paste-subject #3", content: "copy paste test F"},
{stream: "Verona", subject: "copy-paste-subject #3", content: "copy paste test G"},
]);
common.wait_for_receive(function () {
common.expected_messages(
"zhome",
[
"Verona > copy-paste-subject #1",
"Verona > copy-paste-subject #2",
"Verona > copy-paste-subject #3",
],
[
"<p>copy paste test A</p>",
"<p>copy paste test B</p>",
"<p>copy paste test C</p>",
"<p>copy paste test D</p>",
"<p>copy paste test E</p>",
"<p>copy paste test F</p>",
"<p>copy paste test G</p>",
]
);
});
casper.then(function () {
casper.test.info("Messages were sent successfully");
});
function get_message_node(message) {
return $('.message_row .message_content:contains("' + message + '")').get(0);
}
function copy_messages(start_message, end_message) {
return casper.evaluate(
function (get_message_node, start_message, end_message) {
// select messages from start_message to end_message
var selectedRange = document.createRange();
selectedRange.setStart(get_message_node(start_message));
selectedRange.setEnd(get_message_node(end_message));
window.getSelection().removeAllRanges();
window.getSelection().addRange(selectedRange);
// Remove existing copy/paste divs, which may linger from the previous
// example. (The code clears these out with a zero-second timeout, which
// is probably sufficient for human users, but which causes problems here.)
$("#copytempdiv").remove();
// emulate copy event
$("body").trigger($.Event("keydown", {which: 67, ctrlKey: true}));
// find temp div with copied text
var temp_div = $("#copytempdiv");
return temp_div
.children("p")
.get()
.map(function (p) {
return p.textContent;
});
},
{
get_message_node: get_message_node,
start_message: start_message,
end_message: end_message,
}
);
}
// test copying first message from topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test C", "copy paste test C");
var expected_copied_lines = [];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by browser"
);
});
// test copying last message from topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test E", "copy paste test E");
var expected_copied_lines = [];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by browser"
);
});
// test copying two first messages from topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test C", "copy paste test D");
var expected_copied_lines = ["Desdemona: copy paste test C", "Desdemona: copy paste test D"];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by custom handler"
);
});
// test copying all messages from topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test C", "copy paste test E");
var expected_copied_lines = [
"Desdemona: copy paste test C",
"Desdemona: copy paste test D",
"Desdemona: copy paste test E",
];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by custom handler"
);
});
// test copying last message from previous topic and first message from next topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test B", "copy paste test C");
var expected_copied_lines = [
"Verona > copy-paste-subject #1 Today",
"Desdemona: copy paste test B",
"Verona > copy-paste-subject #2 Today",
"Desdemona: copy paste test C",
];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by custom handler"
);
});
// test copying last message from previous topic and all messages from next topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test B", "copy paste test E");
var expected_copied_lines = [
"Verona > copy-paste-subject #1 Today",
"Desdemona: copy paste test B",
"Verona > copy-paste-subject #2 Today",
"Desdemona: copy paste test C",
"Desdemona: copy paste test D",
"Desdemona: copy paste test E",
];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by custom handler"
);
});
// test copying all messages from previous topic and first message from next topic
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test A", "copy paste test C");
var expected_copied_lines = [
"Verona > copy-paste-subject #1 Today",
"Desdemona: copy paste test A",
"Desdemona: copy paste test B",
"Verona > copy-paste-subject #2 Today",
"Desdemona: copy paste test C",
];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by custom handler"
);
});
// test copying message from several topics
casper.then(function () {
var actual_copied_lines = copy_messages("copy paste test B", "copy paste test F");
var expected_copied_lines = [
"Verona > copy-paste-subject #1 Today",
"Desdemona: copy paste test B",
"Verona > copy-paste-subject #2 Today",
"Desdemona: copy paste test C",
"Desdemona: copy paste test D",
"Desdemona: copy paste test E",
"Verona > copy-paste-subject #3 Today",
"Desdemona: copy paste test F",
];
casper.test.assertEquals(
actual_copied_lines,
expected_copied_lines,
"Copying was handled by custom handler"
);
});
// Run the above queued actions.
casper.run(function () {
casper.test.done();
});

View File

@ -0,0 +1,159 @@
"use strict";
const assert = require("assert").strict;
const common = require("../puppeteer_lib/common");
async function copy_messages(page, start_message, end_message) {
return await page.evaluate(
(start_message, end_message) => {
function get_message_node(message) {
return $('.message_row .message_content:contains("' + message + '")').get(0);
}
// select messages from start_message to end_message
const selectedRange = document.createRange();
selectedRange.setStartAfter(get_message_node(start_message));
selectedRange.setEndBefore(get_message_node(end_message));
window.getSelection().removeAllRanges();
window.getSelection().addRange(selectedRange);
// Remove existing copy/paste divs, which may linger from the previous
// example. (The code clears these out with a zero-second timeout, which
// is probably sufficient for human users, but which causes problems here.)
$("#copytempdiv").remove();
// emulate copy event
$("body").trigger($.Event("keydown", {which: 67, ctrlKey: true}));
// find temp div with copied text
const temp_div = $("#copytempdiv");
return temp_div
.children("p")
.get()
.map((p) => p.textContent);
},
start_message,
end_message,
);
}
async function test_copying_first_message_from_topic(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test C");
const expected_copied_lines = [];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_last_message_from_topic(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test E", "copy paste test E");
const expected_copied_lines = [];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_first_two_messages_from_topic(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test D");
const expected_copied_lines = ["Desdemona: copy paste test C", "Desdemona: copy paste test D"];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_all_messages_from_topic(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test E");
const expected_copied_lines = [
"Desdemona: copy paste test C",
"Desdemona: copy paste test D",
"Desdemona: copy paste test E",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_last_from_prev_first_from_next(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test C");
const expected_copied_lines = [
"Verona > copy-paste-topic #1 Today",
"Desdemona: copy paste test B",
"Verona > copy-paste-topic #2 Today",
"Desdemona: copy paste test C",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_last_from_prev_all_from_next(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test E");
const expected_copied_lines = [
"Verona > copy-paste-topic #1 Today",
"Desdemona: copy paste test B",
"Verona > copy-paste-topic #2 Today",
"Desdemona: copy paste test C",
"Desdemona: copy paste test D",
"Desdemona: copy paste test E",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_all_from_prev_first_from_next(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test A", "copy paste test C");
const expected_copied_lines = [
"Verona > copy-paste-topic #1 Today",
"Desdemona: copy paste test A",
"Desdemona: copy paste test B",
"Verona > copy-paste-topic #2 Today",
"Desdemona: copy paste test C",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_messages_from_several_topics(page) {
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test F");
const expected_copied_lines = [
"Verona > copy-paste-topic #1 Today",
"Desdemona: copy paste test B",
"Verona > copy-paste-topic #2 Today",
"Desdemona: copy paste test C",
"Desdemona: copy paste test D",
"Desdemona: copy paste test E",
"Verona > copy-paste-topic #3 Today",
"Desdemona: copy paste test F",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function copy_paste_test(page) {
await common.log_in(page);
await common.send_multiple_messages(page, [
{stream: "Verona", topic: "copy-paste-topic #1", content: "copy paste test A"},
{stream: "Verona", topic: "copy-paste-topic #1", content: "copy paste test B"},
{stream: "Verona", topic: "copy-paste-topic #2", content: "copy paste test C"},
{stream: "Verona", topic: "copy-paste-topic #2", content: "copy paste test D"},
{stream: "Verona", topic: "copy-paste-topic #2", content: "copy paste test E"},
{stream: "Verona", topic: "copy-paste-topic #3", content: "copy paste test F"},
{stream: "Verona", topic: "copy-paste-topic #3", content: "copy paste test G"},
]);
await common.check_messages_sent(page, "zhome", [
["Verona > copy-paste-topic #1", ["copy paste test A", "copy paste test B"]],
[
"Verona > copy-paste-topic #2",
["copy paste test C", "copy paste test D", "copy paste test E"],
],
["Verona > copy-paste-topic #3", ["copy paste test F", "copy paste test G"]],
]);
console.log("Messages were sent successfully");
await test_copying_first_message_from_topic(page);
await test_copying_last_message_from_topic(page);
await test_copying_first_two_messages_from_topic(page);
await test_copying_all_messages_from_topic(page);
await test_copying_last_from_prev_first_from_next(page);
await test_copying_last_from_prev_all_from_next(page);
await test_copying_all_from_prev_first_from_next(page);
await test_copying_messages_from_several_topics(page);
}
common.run_test(copy_paste_test);