login.e2e.js 1.35 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 () => {
jim's avatar
fix ci  
jim committed
10
    browser = await puppeteer.launch({ args: ['--no-sandbox'] });
valleykid's avatar
valleykid committed
11 12 13 14
  });

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

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

偏右's avatar
偏右 committed
21
  it('should login with failure', async () => {
陈帅's avatar
陈帅 committed
22 23 24
    await page.waitForSelector('#userName', {
      timeout: 2000,
    });
valleykid's avatar
valleykid committed
25 26 27 28
    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
29 30
  });

afc163's avatar
afc163 committed
31
  it('should login successfully', async () => {
陈帅's avatar
陈帅 committed
32 33 34
    await page.waitForSelector('#userName', {
      timeout: 2000,
    });
valleykid's avatar
valleykid committed
35 36 37 38 39
    await page.type('#userName', 'admin');
    await page.type('#password', '888888');
    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
40 41
    expect(text).toContain('<h1>Ant Design Pro</h1>');
  });
valleykid's avatar
valleykid committed
42 43

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