forked from ChainSafe/dappeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.spec.ts
208 lines (167 loc) · 5.93 KB
/
test.spec.ts
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
import { readdir } from 'fs/promises';
import path from 'path';
import { expect, use as chaiUse } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import puppeteer from 'puppeteer';
import { Dappeteer, RECOMMENDED_METAMASK_VERSION } from '../src';
import * as dappeteer from '../src/index';
import deploy from './deploy';
import { pause } from './utils';
import { addNetworkTests } from './utils/addNetwork';
import { importPKTests } from './utils/importPK';
chaiUse(chaiAsPromised);
function getCounterNumber(contract): Promise<number> {
return contract.methods
.count()
.call()
.then((res) => {
return Number(res);
});
}
async function clickElement(page, selector): Promise<void> {
await page.bringToFront();
await page.waitForSelector(selector);
const element = await page.$(selector);
await element.click();
}
export let testContract, browser, metamask: Dappeteer, testPage;
describe('dappeteer', () => {
before(async () => {
testContract = await deploy();
browser = await dappeteer.launch(puppeteer, {
metamaskVersion: process.env.METAMASK_VERSION || RECOMMENDED_METAMASK_VERSION,
});
metamask = await dappeteer.setupMetamask(browser, {
// optional, else it will use a default seed
seed: 'pioneer casual canoe gorilla embrace width fiction bounce spy exhibit another dog',
password: 'password1234',
});
testPage = await browser.newPage();
await testPage.goto('http://localhost:8080/');
// output version
const directory = path.resolve(__dirname, '..', 'metamask');
const files = await readdir(directory);
console.log(`::set-output name=version::${files.pop().replace(/_/g, '.')}`);
});
// validate dappateer setup
it('should be deployed, contract', async () => {
expect(testContract).to.be.ok;
expect(testContract.address).to.be.ok;
expect(testContract.options.address).to.be.ok;
});
it('should running, puppeteer', async () => {
expect(browser).to.be.ok;
});
it('should open, metamask', async () => {
expect(metamask).to.be.ok;
});
it('should open, test page', async () => {
expect(testPage).to.be.ok;
expect(await testPage.title()).to.be.equal('Local metamask test');
});
describe('test addNetwork method', addNetworkTests.bind(this));
describe('test importPK method', importPKTests.bind(this));
// TODO: add more cases
it('should switch network, localhost', async () => {
await metamask.switchNetwork('localhost');
const selectedNetwork = await metamask.page.evaluate(
() => (document.querySelector('.network-display > span:nth-child(2)') as HTMLSpanElement).innerHTML,
);
expect(selectedNetwork).to.be.equal('Localhost 8545');
});
describe('test switchAccount method', async () => {
before(async () => {
await metamask.importPK('4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b10');
});
after(async () => {
await metamask.helpers.deleteAccount(2);
await pause(0.5);
});
it('should switch accounts', async () => {
await metamask.switchAccount(1);
});
});
// TODO: cover more cases
it('should add token', async () => {
await metamask.switchNetwork('kovan');
await metamask.addToken({
tokenAddress: '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa',
symbol: 'KAKI',
});
await metamask.switchNetwork('localhost');
});
it('should lock and unlock', async () => {
await metamask.lock();
await metamask.unlock('password1234');
});
it('should connect to ethereum', async () => {
await clickElement(testPage, '.connect-button');
await metamask.approve();
// For some reason initial approve does not resolve nor fail promise
await clickElement(testPage, '.connect-button');
await testPage.waitForSelector('#connected');
});
it('should be able to sign', async () => {
await clickElement(testPage, '.sign-button');
await metamask.sign();
await testPage.waitForSelector('#signed');
});
it('should return token balance', async () => {
const tokenBalance: number = await metamask.helpers.getTokenBalance('ETH');
expect(tokenBalance).to.be.greaterThan(0);
});
it('should return 0 token balance when token not found', async () => {
const tokenBalance: number = await metamask.helpers.getTokenBalance('FARTBUCKS');
expect(tokenBalance).to.be.equal(0);
});
describe('test contract', async () => {
let counterBefore;
before(async () => {
await metamask.switchNetwork('local');
counterBefore = await getCounterNumber(testContract);
});
it('should confirm transaction', async () => {
// click increase button
await clickElement(testPage, '.increase-button');
// submit tx
await metamask.confirmTransaction();
await testPage.waitForSelector('#txSent');
});
it('should have increased count', async () => {
// wait half a seconds just in case
await pause(1);
const counterAfter = await getCounterNumber(testContract);
expect(counterAfter).to.be.equal(counterBefore + 1);
});
});
describe('test confirmTransaction method', async () => {
it('should change gas values', async () => {
// click increase button
await clickElement(testPage, '.increase-fees-button');
// submit tx
await metamask.confirmTransaction({
gas: 21000,
gasLimit: 400000,
});
await testPage.waitForSelector('#feesTxSent');
});
it('should not fail if gas priority is missing', async () => {
await metamask.switchNetwork('localhost');
// click increase button
await clickElement(testPage, '.transfer-button');
await pause(1);
// submit tx
await metamask.confirmTransaction({
gas: 21000,
priority: 2,
gasLimit: 202020,
});
await pause(5);
await testPage.waitForSelector('#transferred');
});
});
after(async () => {
// close browser
await browser.close();
});
});