mirror of
https://github.com/ONLYOFFICE/build_tools.git
synced 2026-02-10 12:35:23 +08:00
Add tests for editors
This commit is contained in:
4
tests/puppeteer/config_chrome.json
Normal file
4
tests/puppeteer/config_chrome.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"browser" : "chrome",
|
||||
"browserUrl" : "C:/Program Files/Google/Chrome/Application/chrome.exe"
|
||||
}
|
||||
4
tests/puppeteer/config_firefox.json
Normal file
4
tests/puppeteer/config_firefox.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"browser" : "firefox",
|
||||
"browserUrl" : "C:/Program Files/Mozilla Firefox/firefox.exe"
|
||||
}
|
||||
9
tests/puppeteer/install.py
Normal file
9
tests/puppeteer/install.py
Normal file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
sys.path.append('../../scripts')
|
||||
import base
|
||||
import os
|
||||
|
||||
os.environ["PUPPETEER_SKIP_CHROMIUM_DOWNLOAD"] = "true"
|
||||
base.cmd("npm", ["i", "puppeteer"])
|
||||
64
tests/puppeteer/run.py
Normal file
64
tests/puppeteer/run.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
sys.path.append('../../scripts')
|
||||
import base
|
||||
import os
|
||||
import glob
|
||||
import json
|
||||
|
||||
def get_tests_in_dir(directory):
|
||||
files = []
|
||||
for file in glob.glob(directory + "/*.js"):
|
||||
if base.is_file(file):
|
||||
files.append(file)
|
||||
elif is_dir(file):
|
||||
files += get_tests_in_dir(file)
|
||||
return files
|
||||
|
||||
params = sys.argv[1:]
|
||||
if (0 == len(params)):
|
||||
print("use: run.py path_to_config [path_to_test]")
|
||||
exit(0)
|
||||
|
||||
config_path = params[0]
|
||||
test_file = "./tests"
|
||||
|
||||
if (1 < len(params)):
|
||||
test_file = params[1]
|
||||
|
||||
tests_array = [test_file]
|
||||
if base.is_dir(test_file):
|
||||
tests_array = get_tests_in_dir(test_file)
|
||||
|
||||
config_content = "{}"
|
||||
with open(config_path, "r") as config_path_loader:
|
||||
config_content = config_path_loader.read()
|
||||
|
||||
print(config_content)
|
||||
|
||||
config = json.loads(config_content)
|
||||
os.environ["PUPPETEER_SKIP_CHROMIUM_DOWNLOAD"] = "true"
|
||||
if "browser" in config:
|
||||
print("browser: " + config["browser"])
|
||||
os.environ["PUPPETEER_PRODUCT"] = config["browser"]
|
||||
|
||||
if "browserUrl" in config:
|
||||
print("browserUrl: " + config["browserUrl"])
|
||||
os.environ["PUPPETEER_EXECUTABLE_PATH"] = config["browserUrl"]
|
||||
|
||||
if not base.is_dir("./work_directory"):
|
||||
base.create_dir("./work_directory")
|
||||
base.create_dir("./work_directory/cache")
|
||||
base.create_dir("./work_directory/downloads")
|
||||
|
||||
for test in tests_array:
|
||||
print("run test: " + test)
|
||||
run_file = test + ".runned.js"
|
||||
base.copy_file("./tester.js", run_file)
|
||||
test_content = base.readFile(test)
|
||||
test_content = test_content.replace("await Tester.", "Tester.")
|
||||
test_content = test_content.replace("Tester.", "await Tester.")
|
||||
base.replaceInFile(run_file, "\"%%CODE%%\"", test_content)
|
||||
base.cmd("node", [run_file])
|
||||
base.delete_file(run_file)
|
||||
171
tests/puppeteer/tester.js
Normal file
171
tests/puppeteer/tester.js
Normal file
@ -0,0 +1,171 @@
|
||||
const puppeteer = require('puppeteer')
|
||||
const pathfs = require('path')
|
||||
const fs = require('fs');
|
||||
|
||||
function TesterImpl()
|
||||
{
|
||||
this.browser = null;
|
||||
this.page = null;
|
||||
this.width = 1500;
|
||||
this.height = 800;
|
||||
this.pixelRatio = 1;
|
||||
|
||||
this.cacheDir = pathfs.resolve("./work_directory/cache");
|
||||
this.downloadsDir = pathfs.resolve("./work_directory/downloads");
|
||||
this.downloadCounter = 0;
|
||||
|
||||
this.load = async function(url)
|
||||
{
|
||||
const head = { x: 100, y: 200 };
|
||||
this.browser = await puppeteer.launch({
|
||||
headless: false,
|
||||
product: process.env["PUPPETEER_PRODUCT"],
|
||||
args: [
|
||||
"--disable-infobars",
|
||||
`--window-size=${this.width+head.x},${this.height+head.y}`,
|
||||
"--disk-cache-dir=" + this.cacheDir
|
||||
],
|
||||
defaultViewport : {width: this.width, height: this.height, deviceScaleFactor : this.pixelRatio }
|
||||
});
|
||||
|
||||
this.page = await this.browser.newPage();
|
||||
await this.page.setViewport({ width: this.width, height: this.height });
|
||||
let waitObject = (process.env["PUPPETEER_PRODUCT"] === "firefox") ? { waitUntil: "networkidle0", timeout: 15000 } : {};
|
||||
await this.page.goto(url + "&autotest=enabled", waitObject);
|
||||
console.log("[tester] pageLoaded");
|
||||
return this.page;
|
||||
};
|
||||
|
||||
this.close = async function(nosleep)
|
||||
{
|
||||
if (true !== nosleep)
|
||||
await this.waitAutosave();
|
||||
await this.browser.close();
|
||||
};
|
||||
|
||||
this.sleep = async function(ms)
|
||||
{
|
||||
return await new Promise(resolve => setTimeout(resolve, ms));
|
||||
};
|
||||
|
||||
this.waitEditor = async function()
|
||||
{
|
||||
// TODO: wait first onEndRecalculate
|
||||
await this.sleep(5000);
|
||||
console.log("[tester] editorReady");
|
||||
};
|
||||
|
||||
this.waitAutosave = async function()
|
||||
{
|
||||
await this.sleep(5000);
|
||||
};
|
||||
|
||||
this.evaluateInMainFrame = async function(code)
|
||||
{
|
||||
return await this.page.evaluate(code);
|
||||
};
|
||||
this.evaluateInEditorFrame = async function(code)
|
||||
{
|
||||
const frame = await this.page.frames().find(frame => frame.name() === 'frameEditor');
|
||||
if (!frame)
|
||||
return;
|
||||
return await frame.evaluate(code);
|
||||
};
|
||||
|
||||
this.click = async function(id)
|
||||
{
|
||||
let res = await this.evaluateInEditorFrame("document.getElementById(\"" + id + "\").click(); \"[tester] clicked: " + id + "\"");
|
||||
//console.log(res);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.mouseClick = async function(x, y, options)
|
||||
{
|
||||
let res = await this.page.mouse.click(x, y, options);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.eval = async function(code)
|
||||
{
|
||||
let res = await this.evaluateInEditorFrame(code);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.keyDown = async function(key)
|
||||
{
|
||||
// https://pptr.dev/api/puppeteer.keyinput
|
||||
let res = await this.page.keyboard.down(key);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.keyUp = async function(key)
|
||||
{
|
||||
// https://pptr.dev/api/puppeteer.keyinput
|
||||
let res = await this.page.keyboard.up(key);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.keyClick = async function(key)
|
||||
{
|
||||
// https://pptr.dev/api/puppeteer.keyinput
|
||||
let res = await this.page.keyboard.down(key);
|
||||
res = await this.page.keyboard.up(key);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.keyPress = async function(key)
|
||||
{
|
||||
// https://pptr.dev/api/puppeteer.keyinput
|
||||
let res = await this.page.keyboard.press(key);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.input = async function(text)
|
||||
{
|
||||
let res = await this.page.keyboard.type(text);
|
||||
await this.sleep(200);
|
||||
return res;
|
||||
};
|
||||
|
||||
this.downloadFile = async function(format, path)
|
||||
{
|
||||
const tmpDir = pathfs.resolve(this.downloadsDir, "./tmp" + this.downloadCounter++);
|
||||
fs.mkdirSync(tmpDir);
|
||||
|
||||
// emulate download
|
||||
const client = await this.page.target().createCDPSession();
|
||||
await client.send("Page.setDownloadBehavior", {
|
||||
behavior: "allow",
|
||||
downloadPath: tmpDir
|
||||
});
|
||||
|
||||
await this.evaluateInEditorFrame("document.querySelectorAll('[data-layout-name=\"toolbar-file\"]')[0].click();");
|
||||
await this.sleep(200);
|
||||
await this.evaluateInEditorFrame("document.getElementsByClassName(\"svg-format-" + format + "\")[0].click();");
|
||||
await this.sleep(200);
|
||||
await this.evaluateInEditorFrame("document.getElementById(\"fm-btn-return\").click();");
|
||||
|
||||
await this.sleep(2000);
|
||||
|
||||
const files = fs.readdirSync(tmpDir);
|
||||
fs.copyFileSync(pathfs.resolve(tmpDir, "./" + files[0]), pathfs.resolve(path));
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
};
|
||||
}
|
||||
|
||||
const Tester = new TesterImpl;
|
||||
|
||||
try {
|
||||
(async () => {
|
||||
"%%CODE%%"
|
||||
})();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
27
tests/puppeteer/tests/bold.js
Normal file
27
tests/puppeteer/tests/bold.js
Normal file
@ -0,0 +1,27 @@
|
||||
Tester.load("path_to_file");
|
||||
Tester.waitEditor();
|
||||
|
||||
// down Enter
|
||||
Tester.keyClick("Enter");
|
||||
|
||||
// type text
|
||||
Tester.input("Hello World!");
|
||||
|
||||
Tester.keyPress("ArrowLeft");
|
||||
Tester.keyDown("Shift");
|
||||
for (let i = 0; i < 5; i++)
|
||||
Tester.keyPress("ArrowLeft");
|
||||
Tester.keyUp("Shift");
|
||||
|
||||
// bold
|
||||
Tester.click("id-toolbar-btn-bold");
|
||||
// italic
|
||||
Tester.mouseClick(115, 105);
|
||||
|
||||
// if needed
|
||||
Tester.waitAutosave();
|
||||
|
||||
Tester.downloadFile("docx", "./work_directory/new.docx")
|
||||
Tester.downloadFile("odt", "./work_directory/new.odt")
|
||||
|
||||
Tester.close(true);
|
||||
Reference in New Issue
Block a user