login.e2e.js 1.39 KB
Newer Older
valleykid's avatar
valleykid committed
1
import puppeteer from 'puppeteer';
偏右's avatar
偏右 committed
2

3 4
const BASE_URL = `http://localhost:${process.env.PORT || 8000}`;

偏右's avatar
偏右 committed
5
describe('Login', () => {
valleykid's avatar
valleykid committed
6
  let browser;
偏右's avatar
偏右 committed
7
  let page;
valleykid's avatar
valleykid committed
8 9

  beforeAll(async () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10
    jest.setTimeout(1000000);
jim's avatar
fix ci  
jim committed
11
    browser = await puppeteer.launch({ args: ['--no-sandbox'] });
valleykid's avatar
valleykid committed
12 13 14 15
  });

  beforeEach(async () => {
    page = await browser.newPage();
16
    await page.goto(`${BASE_URL}/user/login`, { waitUntil: 'networkidle2' });
valleykid's avatar
valleykid committed
17
    await page.evaluate(() => window.localStorage.setItem('antd-pro-authority', 'guest'));
偏右's avatar
偏右 committed
18 19
  });

valleykid's avatar
valleykid committed
20 21
  afterEach(() => page.close());

偏右's avatar
偏右 committed
22
  it('should login with failure', async () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
23 24 25
    await page.waitForSelector('#userName', {
      timeout: 2000,
    });
valleykid's avatar
valleykid committed
26 27 28 29
    await page.type('#userName', 'mockuser');
    await page.type('#password', 'wrong_password');
    await page.click('button[type="submit"]');
    await page.waitForSelector('.ant-alert-error'); // should display error
偏右's avatar
偏右 committed
30 31
  });

afc163's avatar
afc163 committed
32
  it('should login successfully', async () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33 34 35
    await page.waitForSelector('#userName', {
      timeout: 2000,
    });
valleykid's avatar
valleykid committed
36
    await page.type('#userName', 'admin');
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
    await page.type('#password', 'ant.design');
valleykid's avatar
valleykid committed
38 39 40
    await page.click('button[type="submit"]');
    await page.waitForSelector('.ant-layout-sider h1'); // should display error
    const text = await page.evaluate(() => document.body.innerHTML);
偏右's avatar
偏右 committed
41 42
    expect(text).toContain('<h1>Ant Design Pro</h1>');
  });
valleykid's avatar
valleykid committed
43 44

  afterAll(() => browser.close());
偏右's avatar
偏右 committed
45
});