Add integration tests for scroll

jh-changes
Shin'ya Ueoka 6 years ago
parent af9662f86c
commit 6e6cd4c01e
  1. 150
      e2e-lanthan/scroll.test.js
  2. 58
      e2e-lanthan/server/MockServer.js
  3. 17
      e2e-lanthan/server/handlers.js

@ -0,0 +1,150 @@
const express = require('express');
const lanthan = require('lanthan');
const path = require('path');
const assert = require('power-assert');
const Key = lanthan.Key;
const newApp = () => {
let app = express();
app.get('/', (req, res) => {
res.send(`<!DOCTYPEhtml>
<html lang="en">
<body style="width:10000px; height:10000px"></body>
</html">`);
});
return app;
};
describe("scroll test", () => {
const port = 12321;
let http;
let firefox;
let session;
let body;
before(async() => {
http = newApp().listen(port);
firefox = await lanthan.firefox();
await firefox.session.installAddon(path.join(__dirname, '..'));
session = firefox.session;
});
after(async() => {
if (firefox) {
await firefox.close();
}
http.close();
});
beforeEach(async() => {
await session.navigateTo(`http://127.0.0.1:${port}`);
body = await session.findElementByCSS('body');
});
it('scrolls up by k', async () => {
await body.sendKeys('j');
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert.equal(pageYOffset, 64);
});
it('scrolls down by j', async () => {
await session.executeScript(() => window.scrollTo(0, 200));
await body.sendKeys('k');
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert.equal(pageYOffset, 136);
});
it('scrolls left by h', async () => {
await session.executeScript(() => window.scrollTo(100, 100));
await body.sendKeys('h');
let pageXOffset = await session.executeScript(() => window.pageXOffset);
assert.equal(pageXOffset, 36);
});
it('scrolls left by l', async () => {
await session.executeScript(() => window.scrollTo(100, 100));
await body.sendKeys('l');
let pageXOffset = await session.executeScript(() => window.pageXOffset);
assert.equal(pageXOffset, 164);
});
it('scrolls top by gg', async () => {
await session.executeScript(() => window.scrollTo(0, 100));
await body.sendKeys('g', 'g');
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert.equal(pageYOffset, 0);
});
it('scrolls bottom by G', async () => {
await session.executeScript(() => window.scrollTo(0, 100));
await body.sendKeys(Key.Shift, 'g');
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert(pageYOffset > 5000);
});
it('scrolls bottom by 0', async () => {
await session.executeScript(() => window.scrollTo(0, 100));
await body.sendKeys(Key.Shift, '0');
let pageXOffset = await session.executeScript(() => window.pageXOffset);
assert(pageXOffset === 0);
});
it('scrolls bottom by $', async () => {
await session.executeScript(() => window.scrollTo(0, 100));
await body.sendKeys(Key.Shift, '$');
let pageXOffset = await session.executeScript(() => window.pageXOffset);
assert(pageXOffset > 5000);
});
it('scrolls bottom by <C-U>', async () => {
await session.executeScript(() => window.scrollTo(0, 1000));
await body.sendKeys(Key.Control, 'u');
let pageHeight =
await session.executeScript(() => window.document.documentElement.clientHeight);
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert(Math.abs(pageYOffset - (1000 - Math.floor(pageHeight / 2))) < 5);
});
it('scrolls bottom by <C-D>', async () => {
await session.executeScript(() => window.scrollTo(0, 1000));
await body.sendKeys(Key.Control, 'd');
let pageHeight =
await session.executeScript(() => window.document.documentElement.clientHeight);
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert(Math.abs(pageYOffset - (1000 + Math.floor(pageHeight / 2))) < 5);
});
it('scrolls bottom by <C-B>', async () => {
await session.executeScript(() => window.scrollTo(0, 1000));
await body.sendKeys(Key.Control, 'b');
let pageHeight =
await session.executeScript(() => window.document.documentElement.clientHeight);
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert(Math.abs(pageYOffset - (1000 - pageHeight)) < 5);
});
it('scrolls bottom by <C-F>', async () => {
await session.executeScript(() => window.scrollTo(0, 1000));
await body.sendKeys(Key.Control, 'f');
let pageHeight =
await session.executeScript(() => window.document.documentElement.clientHeight);
let pageYOffset = await session.executeScript(() => window.pageYOffset);
assert(Math.abs(pageYOffset - (1000 + pageHeight)) < 5);
});
});

@ -0,0 +1,58 @@
var http = require('http');
var url = require('url');
var handlers = require('./handlers');
class MockServer {
constructor() {
this.handlers = [];
this.server = undefined;
}
start() {
if (this.server) {
throw new Error('Server is already started');
}
let listener = (req, res) => {
if (req.method !== 'GET') {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found')
return
}
let u = url.parse(req.url);
let handler = this.handlers.find(h => u.pathname == h.pathname);
if (!handler) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found')
return
}
handler.handler(req, res);
}
this.server = http.createServer(listener);
this.server.listen();
}
stop() {
if (!this.server) {
throw new Error('Server is not started');
}
this.server.close();
this.server = undefined;
}
port() {
if (!this.server) {
throw new Error('Server is not started');
}
return this.server.address().port
}
on(pathname, handler) {
this.handlers.push({ pathname, handler });
}
}
module.exports = MockServer

@ -0,0 +1,17 @@
const handleText = (body) => {
return (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plane'});
res.end(body);
}
}
const handleHtml = (body) => {
return (req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(body);
}
}
module.exports = {
handleText, handleHtml
}