CodeceptJSでHandsontableのセルをクリックする方法

CodeceptJSでHandsontableのセルをクリックする方法

ReactTypeScript

おはこんばんは。最近、CodeceptJSでE2Eテストを書くことが増えたのですが、CodeceptJSでHandsontableの項目名セルをクリックするのに少し苦戦したので、対策についてご紹介しようと思います。

背景

CodeceptJSでは、I.click(locator)でlocatorに指定されたリンクやボタンのクリックを実行します。ただ、Handsontableの項目名セルを指定するとうまくクリックが実行されませんでした。

Handsontableの項目名セルが検知しているイベントとCodeceptJSがI.click(locator)で発火しているイベントが違うみたいです...恐らく...

対策

今回はCodeceptJSのI.click(locator)は利用せず、mouseEventでmousedownを発火して対応するようにしました。

以下のように該当のセルにmousedownを発火するメソッドを定義します。

e2e/configs/components/Handsontable.ts
1 export = {
2 /**
3 * I.clickではHandsontableの項目セルがクリックされないので独自で定義している
4 * @param row 該当セルの行
5 * @param line 該当セルの列
6 */
7 clickCell: async function (row: number, line: number): Promise<void> {
8 await I.executeScript(
9 (row, line) => {
10 const trElement = document.querySelectorAll(".ht_master table tr");
11 if (trElement?.length < row) return;
12 const tdElement = trElement[row-1].childNodes;
13 if (tdElement?.length < line) return;
14
15 const mousedownEvent = new MouseEvent("mousedown", {
16 bubbles: true,
17 cancelable: true,
18 });
19 tdElement[line-1].dispatchEvent(mousedownEvent);
20 },
21 row,
22 line
23 );
24 },
25}
26

そしてCodeceptJSで上記を呼び出せるように設定ファイルへ追記してあげます。

codecept.conf.js
1include: {
2 Handsontable: "./e2e/configs/components/Handsontable.ts",
3}

steps.d.ts
1type Handsontable = typeof import("./e2e/configs/components/Handsontable");
2
3declare namespace CodeceptJS {
4 interface SupportObject {
5 Handsontable: Handsontable;
6 }
7}
8

これでCodeceptJSで利用できるようになったので、以下のように呼び出してあげればOKです。お疲れ様でした。

1Scenario(
2 "test",
3 async ({ Handsontable }) => {
4 await Handsontable.clickCell(1, 1);
5 }
6);

さいごに

CodeceptJSでHandsontableのセルをクリックする方法をご紹介しました。CodeceptJSとHandsontableは闇があったりするので、ちょっと苦戦しますね。

今回の記事が誰かの参考になれば幸いです。