aboutsummaryrefslogtreecommitdiff
path: root/e2e/tab.test.ts
blob: 49d1c030d034b2c7af6fa149e061bee5092edaf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as path from "path";
import * as assert from "assert";

import TestServer from "./lib/TestServer";
import eventually from "./eventually";
import { Builder, Lanthan } from "lanthan";
import { WebDriver, Key } from "selenium-webdriver";
import Page from "./lib/Page";

describe("tab test", () => {
  const server = new TestServer().receiveContent("/*", "ok");
  let lanthan: Lanthan;
  let webdriver: WebDriver;
  let browser: any;
  let win: any;
  let tabs: any[];

  before(async () => {
    lanthan = await Builder.forBrowser("firefox")
      .spyAddon(path.join(__dirname, ".."))
      .build();
    webdriver = lanthan.getWebDriver();
    browser = lanthan.getWebExtBrowser();
    await server.start();
  });

  after(async () => {
    await server.stop();
    if (lanthan) {
      await lanthan.quit();
    }
  });

  beforeEach(async () => {
    win = await browser.windows.create({ url: server.url("/#0") });
    for (let i = 1; i < 5; ++i) {
      await browser.tabs.create({
        url: server.url("/#" + i),
        windowId: win.id,
      });
      await webdriver.navigate().to(server.url("/#" + i));
    }
    tabs = await browser.tabs.query({ windowId: win.id });
    tabs.sort((t1: any, t2: any) => t1.index - t2.index);
  });

  afterEach(async () => {
    await browser.windows.remove(win.id);
  });

  it("deletes tab and selects right by d", async () => {
    await browser.tabs.update(tabs[3].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("d");

    await eventually(async () => {
      const current = await browser.tabs.query({ windowId: win.id });
      assert.strictEqual(current.length, tabs.length - 1);
      assert.strictEqual(current[3].active, true);
      assert.strictEqual(current[3].id, tabs[4].id);
    });
  });

  it("deletes tab and selects left by D", async () => {
    await browser.tabs.update(tabs[3].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys(Key.SHIFT, "D");

    await eventually(async () => {
      const current = await browser.tabs.query({ windowId: win.id });
      assert.strictEqual(current.length, tabs.length - 1);
      assert.strictEqual(current[2].active, true);
      assert.strictEqual(current[2].id, tabs[2].id);
    });
  });

  it("deletes all tabs to the right by x$", async () => {
    await browser.tabs.update(tabs[1].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("x", "$");

    await eventually(async () => {
      const current = await browser.tabs.query({ windowId: win.id });
      assert.strictEqual(current.length, 2);
    });
  });

  it("duplicates tab by zd", async () => {
    await browser.tabs.update(tabs[0].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("z", "d");

    await eventually(async () => {
      const current = await browser.tabs.query({ windowId: win.id });
      current.sort((t1: any, t2: any) => t1.index - t2.index);
      assert.strictEqual(current.length, tabs.length + 1);
      assert.strictEqual(current[0].url, current[1].url);
    });
  });

  it("makes pinned by zp", async () => {
    await browser.tabs.update(tabs[0].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("z", "p");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[0].pinned, true);
  });

  it("selects previous tab by K", async () => {
    await browser.tabs.update(tabs[2].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys(Key.SHIFT, "K");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[1].active, true);
  });

  it("selects previous tab by K rotatory", async () => {
    await browser.tabs.update(tabs[0].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys(Key.SHIFT, "K");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[current.length - 1].active, true);
  });

  it("selects next tab by J", async () => {
    await browser.tabs.update(tabs[2].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys(Key.SHIFT, "J");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[3].active, true);
  });

  it("selects previous tab by J rotatory", async () => {
    await browser.tabs.update(tabs[tabs.length - 1].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys(Key.SHIFT, "J");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[0].active, true);
  });

  it("selects first tab by g0", async () => {
    await browser.tabs.update(tabs[2].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("g", "0");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[0].active, true);
  });

  it("selects last tab by g$", async () => {
    await browser.tabs.update(tabs[2].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("g", "$");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[current.length - 1].active, true);
  });

  it("selects last selected tab by <C-6>", async () => {
    await browser.tabs.update(tabs[1].id, { active: true });
    await browser.tabs.update(tabs[4].id, { active: true });

    const page = await Page.currentContext(webdriver);
    await page.sendKeys(Key.CONTROL, "6");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current[1].active, true);
  });

  // browser.sessions.getRecentlyClosed() sometime throws "An unexpected error occurred"
  // This might be a bug in Firefox.
  it.skip("reopen tab by u", async () => {
    await browser.tabs.remove(tabs[1].id);
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("u");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current.length, tabs.length);
  });

  it("does not delete pinned tab by d", async () => {
    await browser.tabs.update(tabs[0].id, { active: true, pinned: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("d");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current.length, tabs.length);
  });

  it("deletes pinned tab by !d", async () => {
    await browser.tabs.update(tabs[0].id, { active: true, pinned: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("!", "d");

    const current = await browser.tabs.query({ windowId: win.id });
    assert.strictEqual(current.length, tabs.length - 1);
  });

  it("opens view-source by gf", async () => {
    await browser.tabs.update(tabs[0].id, { active: true });
    const page = await Page.currentContext(webdriver);
    await page.sendKeys("g", "f");

    await eventually(async () => {
      const current = await browser.tabs.query({ windowId: win.id });
      assert.strictEqual(current.length, tabs.length + 1);
      assert.strictEqual(
        current[current.length - 1].url,
        `view-source:${server.url("/#0")}`
      );
    });
  });
});