Skip to content

Commit e29abae

Browse files
committed
Add an E2E test for smartphone client to access blog (WIP) fc2blog#137
It is a runnable, but will be conflicts with blog_crawl.test.ts when run in parallel now(WIP). Have to decoupling a entry(comment) id.
1 parent 4c53e18 commit e29abae

File tree

1 file changed

+393
-0
lines changed

1 file changed

+393
-0
lines changed

e2e_test/tests/blog_crawl_sp.test.ts

+393
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,393 @@
1+
import { afterAll, beforeAll, describe, expect, it } from "@jest/globals";
2+
import { Helper } from "./helper";
3+
4+
describe("crawl some blog sp", () => {
5+
let c: Helper;
6+
let captcha_key: string;
7+
8+
beforeAll(async () => {
9+
c = new Helper();
10+
await c.init();
11+
await c.setSpUserAgent();
12+
captcha_key = "1234";
13+
});
14+
15+
const start_url = "http://localhost:8080/testblog2/";
16+
17+
it("open blog top", async () => {
18+
const [response] = await Promise.all([
19+
c.waitLoad(),
20+
c.page.goto(start_url),
21+
]);
22+
23+
await c.getSS("blog_top_sp.png");
24+
25+
expect(response.status()).toEqual(200);
26+
expect(response.url()).toEqual(start_url);
27+
expect(await c.page.title()).toEqual("testblog2");
28+
});
29+
30+
it("check cookie", async () => {
31+
const cookies = await c.page.cookies();
32+
33+
// ここがコケるということは、テスト無しにクッキーが追加されているかも
34+
expect(cookies.length).toEqual(1);
35+
36+
const [session_cookie] = cookies.filter((elm) => elm.name === "dojima");
37+
38+
// console.log(session_cookie);
39+
expect(session_cookie.domain).toEqual("localhost");
40+
expect(session_cookie.path).toEqual("/");
41+
expect(session_cookie.expires).toEqual(-1);
42+
expect(session_cookie.httpOnly).toEqual(true);
43+
expect(session_cookie.secure).toEqual(false);
44+
expect(session_cookie.session).toEqual(true);
45+
expect(session_cookie.sameSite).toEqual("Lax");
46+
});
47+
48+
it("check fuzzy display contents", async () => {
49+
const title_text = await c.page.$eval("h1 a", (elm) => elm.innerHTML);
50+
expect(title_text).toEqual("testblog2");
51+
52+
const entry_bodies = await c.page.$$eval("ul#entry_list strong", (elm_list) => {
53+
let bodies = [];
54+
elm_list.forEach((elm) => bodies.push(elm.textContent));
55+
return bodies;
56+
});
57+
// console.log(entry_bodies);
58+
59+
expect(entry_bodies[0].match(/3rd/)).toBeTruthy();
60+
expect(entry_bodies[1].match(/2nd/)).toBeTruthy();
61+
expect(entry_bodies[2].match(//)).toBeTruthy();
62+
});
63+
64+
it("click first entry", async () => {
65+
const link = await c.page.$("ul#entry_list a");
66+
67+
const [response] = await Promise.all([c.waitLoad(), link.click()]);
68+
69+
await c.getSS("entry_sp.png");
70+
71+
expect(response.status()).toEqual(200);
72+
expect(response.url()).toEqual(start_url + "?no=3");
73+
expect(await c.page.title()).toEqual("3rd - testblog2");
74+
75+
const entry_h2_title = await c.page.$eval(
76+
"div.entry_title h2",
77+
(elm) => elm.textContent
78+
);
79+
const entry_body = await c.page.$eval(
80+
"div.entry_body",
81+
(elm) => elm.textContent
82+
);
83+
84+
expect(entry_body.match(/3rd/)).toBeTruthy();
85+
expect(entry_h2_title.match(/3rd/)).toBeTruthy();
86+
});
87+
88+
it("next page", async () => {
89+
const [response] = await Promise.all([
90+
c.waitLoad(),
91+
c.page.click("a.nextpage"),
92+
]);
93+
94+
expect(response.status()).toEqual(200);
95+
expect(response.url()).toEqual(start_url + "?no=2");
96+
expect(await c.page.title()).toEqual("2nd - testblog2");
97+
98+
const entry_h2_title = await c.page.$eval(
99+
"div.entry_title h2",
100+
(elm) => elm.textContent
101+
);
102+
const entry_body = await c.page.$eval(
103+
"div.entry_body",
104+
(elm) => elm.textContent
105+
);
106+
107+
expect(entry_body.match(/2nd/)).toBeTruthy();
108+
expect(entry_h2_title.match(/2nd/)).toBeTruthy();
109+
});
110+
111+
it("prev page", async () => {
112+
const [response] = await Promise.all([
113+
c.waitLoad(),
114+
c.page.click("a.prevpage"),
115+
]);
116+
117+
expect(response.status()).toEqual(200);
118+
expect(response.url()).toEqual(start_url + "?no=3");
119+
expect(await c.page.title()).toEqual("3rd - testblog2");
120+
121+
const entry_h2_title = await c.page.$eval(
122+
"div.entry_title h2",
123+
(elm) => elm.textContent
124+
);
125+
const entry_body = await c.page.$eval(
126+
"div.entry_body",
127+
(elm) => elm.textContent
128+
);
129+
130+
expect(entry_body.match(/3rd/)).toBeTruthy();
131+
expect(entry_h2_title.match(/3rd/)).toBeTruthy();
132+
});
133+
134+
let posted_comment_num;
135+
136+
it("open comment form", async () => {
137+
const link = await c.page.$("#entry > ul > li:nth-child(1) > a");
138+
const [response] = await Promise.all([c.waitLoad(), link.click()]);
139+
expect(response.status()).toEqual(200);
140+
expect(response.url()).toEqual(start_url + "?no=3&m2=form");
141+
expect(await c.page.title()).toEqual("3rd - testblog2");
142+
});
143+
144+
it("fill comment", async () => {
145+
await c.page.type(
146+
"form[name=form1] input[name='comment[name]']",
147+
"テスト太郎"
148+
);
149+
await c.page.type(
150+
"form[name=form1] input[name='comment[title]']",
151+
"テストタイトル"
152+
);
153+
await c.page.type(
154+
"form[name=form1] input[name='comment[mail]']",
155+
156+
);
157+
await c.page.type(
158+
"form[name=form1] input[name='comment[url]']",
159+
"http://example.jp"
160+
);
161+
await c.page.type(
162+
"form[name=form1] textarea[name='comment[body]']",
163+
"これはテスト投稿です\nこれはテスト投稿です!"
164+
);
165+
await c.page.type(
166+
"form[name=form1] input[name='comment[pass]']",
167+
"pass_is_pass"
168+
);
169+
170+
await c.getSS("comment_filled_sp.png");
171+
172+
const [response] = await Promise.all([
173+
c.waitLoad(),
174+
await c.page.click("#comment_post > form > div > a"),
175+
]);
176+
177+
await c.getSS("comment_confirm_sp.png");
178+
179+
expect(response.status()).toEqual(200);
180+
expect(response.url()).toEqual(start_url);
181+
});
182+
183+
it("failed with wrong captcha", async () => {
184+
// input wrong captcha
185+
await c.page.type("input[name=token]", "0000"); // wrong key
186+
187+
const [response] = await Promise.all([
188+
c.waitLoad(),
189+
await c.page.click("#sys-comment-form > fieldset > div > input"),
190+
]);
191+
192+
await c.getSS("comment_wrong_captcha_sp.png");
193+
194+
expect(response.status()).toEqual(200);
195+
expect(response.url()).toEqual(start_url);
196+
197+
// 特定しやすいセレクタがない
198+
const is_captcha_failed = await c.page.$$eval("p", (elms) => {
199+
let isOk = false;
200+
elms.forEach((elm) => {
201+
if (elm.textContent.match(//)) {
202+
isOk = true;
203+
}
204+
});
205+
return isOk;
206+
});
207+
208+
expect(is_captcha_failed).toBeTruthy();
209+
});
210+
211+
212+
it("comment success", async () => {
213+
await c.page.type("input[name=token]", captcha_key);
214+
await c.getSS("comment_correct_token_sp.png");
215+
216+
const [response] = await Promise.all([
217+
c.waitLoad(),
218+
await c.page.click("#sys-comment-form input[type=submit]"),
219+
]);
220+
221+
expect(response.status()).toEqual(200);
222+
const exp = new RegExp(
223+
start_url + 'index.php\\?mode=entries&process=view&id=[0-9]{1,100}'
224+
);
225+
226+
expect(response.url().match(exp)).not.toBeNull();
227+
228+
const comment_a_text = await c.page.$eval("#entry > ul > li:nth-child(2) > a", elm=>elm.textContent);
229+
230+
await c.getSS("comment_success_sp.png");
231+
posted_comment_num = parseInt(comment_a_text.replace(/^\(/,'').replace(/\)$/,''));
232+
});
233+
234+
it("open comment list", async () => {
235+
const link = await c.page.$("#entry > ul > li:nth-child(2) > a");
236+
const [response] = await Promise.all([
237+
c.waitLoad(),
238+
link.click()
239+
]);
240+
expect(response.status()).toEqual(200);
241+
expect(response.url()).toEqual(start_url + "?no=3&m2=res");
242+
expect(await c.page.title()).toEqual("3rd - testblog2");
243+
});
244+
245+
it("open comment form", async () => {
246+
const link = await c.page.$("#comment > dl > dd:nth-child(2) > p > a:nth-child(4)");
247+
const [response] = await Promise.all([
248+
c.waitLoad(),
249+
link.click()
250+
]);
251+
expect(response.status()).toEqual(200);
252+
expect(response.url()).toEqual(start_url + "index.php?mode=entries&process=comment_edit&id=1");
253+
expect(await c.page.title()).toEqual("- testblog2"); // TODO issue #223
254+
await c.getSS("comment_edit_before_sp");
255+
});
256+
257+
it("comment edit", async () => {
258+
await c.page.$eval(
259+
"#comment_post input[name='edit[name]']",
260+
(elm: HTMLInputElement) => (elm.value = "")
261+
);
262+
await c.page.type("#comment_post input[name='edit[name]']", "テスト太郎2");
263+
await c.page.$eval(
264+
"#comment_post input[name='edit[title]']",
265+
(elm: HTMLInputElement) => (elm.value = "")
266+
);
267+
await c.page.type(
268+
"#comment_post input[name='edit[title]']",
269+
"テストタイトル2"
270+
);
271+
await c.page.$eval(
272+
"#comment_post input[name='edit[mail]']",
273+
(elm: HTMLInputElement) => (elm.value = "")
274+
);
275+
await c.page.type(
276+
"#comment_post input[name='edit[mail]']",
277+
278+
);
279+
await c.page.$eval(
280+
"#comment_post input[name='edit[url]']",
281+
(elm: HTMLInputElement) => (elm.value = "")
282+
);
283+
await c.page.type(
284+
"#comment_post input[name='edit[url]']",
285+
"http://example.jp/2"
286+
);
287+
await c.page.$eval(
288+
"#comment_post textarea[name='edit[body]']",
289+
(elm: HTMLInputElement) => (elm.value = "")
290+
);
291+
await c.page.type(
292+
"#comment_post textarea[name='edit[body]']",
293+
"これは編集済み"
294+
);
295+
await c.page.$eval(
296+
"#comment_post input[name='edit[pass]']",
297+
(elm: HTMLInputElement) => (elm.value = "")
298+
);
299+
await c.page.type("#comment_post input[name='edit[pass]']", "pass_is_pass");
300+
301+
await c.getSS("comment_edit_filled_sp");
302+
303+
// 保存する
304+
let [response] = await Promise.all([
305+
c.waitLoad(),
306+
await c.page.click("#comment_post > form > div > input[type=submit]:nth-child(1)"),
307+
]);
308+
309+
await c.getSS("comment_edit_confirm_sp");
310+
expect(response.status()).toEqual(200);
311+
312+
await c.page.type("input[name=token]", captcha_key);
313+
314+
[response] = await Promise.all([
315+
c.waitLoad(),
316+
await c.page.click("#sys-comment-form > fieldset > div > input"),
317+
]);
318+
319+
await c.getSS("comment_edit_success_sp");
320+
expect(response.status()).toEqual(200);
321+
});
322+
323+
it("open comment list to delete", async () => {
324+
const link = await c.page.$("#entry > ul > li:nth-child(2) > a");
325+
const [response] = await Promise.all([
326+
c.waitLoad(),
327+
link.click()
328+
]);
329+
expect(response.status()).toEqual(200);
330+
expect(response.url()).toEqual(start_url + "?no=3&m2=res");
331+
expect(await c.page.title()).toEqual("3rd - testblog2");
332+
await c.getSS("comment_list_delete_before_sp");
333+
});
334+
335+
it("open comment form to delete", async () => {
336+
const link = await c.page.$("#comment > dl > dd:nth-child(2) > p > a:nth-child(4)");
337+
const [response] = await Promise.all([
338+
c.waitLoad(),
339+
link.click()
340+
]);
341+
expect(response.status()).toEqual(200);
342+
expect(response.url()).toEqual(start_url + "index.php?mode=entries&process=comment_edit&id=1");
343+
expect(await c.page.title()).toEqual("- testblog2"); // TODO issue #223
344+
await c.getSS("comment_form_delete_before_sp");
345+
});
346+
347+
it("comment delete fail by wrong password", async () => {
348+
const delete_button = await c.page.$(
349+
"#comment_post > form > div > input[type=submit]:nth-child(2)"
350+
);
351+
352+
const [response] = await Promise.all([c.waitLoad(), await delete_button.click()]);
353+
354+
expect(response.status()).toEqual(200);
355+
expect(response.url()).toEqual(start_url);
356+
357+
const wrong_password_error_text = await c.page.$eval("#comment_post > form > dl > dd:nth-child(12) > p", elm => elm.textContent);
358+
expect(//.exec(wrong_password_error_text)).toBeTruthy();
359+
});
360+
361+
it("comment delete success", async () => {
362+
await c.page.type("#comment_post > form > dl > dd:nth-child(12) > input[type=password]", "pass_is_pass");
363+
364+
const [response] = await Promise.all([
365+
c.waitLoad(),
366+
await c.page.click("#comment_post > form > div > input[type=submit]:nth-child(2)")
367+
]);
368+
369+
expect(response.status()).toEqual(200);
370+
expect(response.url()).toEqual(start_url+"index.php?mode=entries&process=index&sp");
371+
});
372+
373+
it("open entry check delete complete", async () => {
374+
const link = await c.page.$("#entry_list > li:nth-child(1) > a");
375+
const [response] = await Promise.all([
376+
c.waitLoad(),
377+
link.click()
378+
]);
379+
expect(response.status()).toEqual(200);
380+
expect(response.url()).toEqual(start_url + "?no=3&sp");
381+
expect(await c.page.title()).toEqual("3rd - testblog2");
382+
});
383+
384+
it("check comment count", async () => {
385+
const comment_a_text = await c.page.$eval("#entry > ul > li:nth-child(2) > a", elm=>elm.textContent);
386+
const comment_num = parseInt(comment_a_text.replace(/^\(/,'').replace(/\)$/,''));
387+
expect(comment_num).toEqual(posted_comment_num);
388+
});
389+
390+
afterAll(async () => {
391+
await c.browser.close();
392+
});
393+
});

0 commit comments

Comments
 (0)