UNPKG

10 kBPlain TextView Raw
1import { expectAssignable, expectError, expectNotAssignable, expectNotType, expectType } from 'tsd';
2import { ExpectElement, NightwatchAPI, NightwatchExpectResult } from '..';
3
4// Chai expect() library
5it('test expect()', async () => {
6 expectType<Chai.Assertion>(expect('Nightwatch.js').to.contain('Nightwatch'));
7 expectType<Chai.Assertion>(expect(2).to.equal(2));
8 expectType<Chai.Assertion>(expect(function () {}).to.not.throw());
9 expectType<Chai.Assertion>(expect({a: 2, b: 1}).to.have.property('b'));
10 expectType<Chai.Assertion>(expect([1, 2]).to.be.an('array').that.does.not.include(3));
11 expectType<Chai.Assertion>(expect([1, 2, 3]).to.be.an('array').that.does.include(3));
12
13 // if element is passed to expect
14 const expectAssert = expect(element('selector')).text.equal('something')
15 expectNotType<Chai.Assertion>(expectAssert);;
16 expectNotAssignable<Chai.Assertion>(expectAssert);
17 expectAssignable<ExpectElement>(expectAssert);
18 expectAssignable<ExpectElement>(expect(by.xpath('//tagname')).text.equal('something'));
19 expectAssignable<ExpectElement>(expect(await element('selector').getWebElement()).text.equal('something'))
20 expectAssignable<ExpectElement>(expect(await browser.findElement('selector')).text.equal('something'));;
21});
22
23// Expect test for language chains
24it('expect.equal(value)/.contain(value)/.match(regex)', () => {
25 browser.expect.element('#main').text.to.equal('The Night Watch');
26 browser.expect.element('#main').text.to.contain('The Night Watch');
27 browser.expect.element('#main').to.have.css('display').which.equals('block');
28
29 // with section
30 browser.expect.section('@main').text.to.equal('The Night Watch');
31 browser.expect.section('@main').text.to.contain('The Night Watch');
32 browser.expect.section('@main').to.have.css('display').which.equals('block');
33});
34
35it('expect.startWith(value)/.endWith(value)', () => {
36 browser.expect.element('#main').text.to.endWith('Watch');
37 browser.expect.element('#main').text.to.startWith('The');
38});
39
40it('expect.not', () => {
41 browser.expect.element('#main').text.to.not.equal('The Night Watch');
42 browser.expect.element('#main').text.to.not.contain('The Night Watch');
43 browser.expect.element('#main').to.have.css('display').which.does.not.equal('block');
44});
45
46it('expect.before(ms)/.after(ms)', () => {
47 browser.expect.element('#main').text.to.contain('The Night Watch').before(1000);
48 browser.expect.element('#main').text.to.not.contain('The Night Watch').after(500);
49});
50
51it('expect.cookie()', () => {
52 browser.expect.cookie('cookie-name').to.contain('cookie-value');
53 browser.expect.cookie('cookie-name').to.match(/regex/);
54 browser.expect.cookie('loginCookie', 'example.org').to.contain('cookie-value');
55});
56
57describe('expect.element()', () => {
58 it('have .a(type)', () => {
59 browser.expect.element('#q').to.be.an('input');
60 browser.expect.element('#q').to.be.an('input', 'Testing if #q is an input');
61 browser.expect.element('#w').to.be.a('span');
62 });
63
64 it('have .active()', () => {
65 browser.expect.element('#main').to.be.active;
66 browser.expect.element('#main').to.not.be.active;
67 browser.expect.element('#main').to.be.active.before(100);
68 // with two properties together
69 browser.expect.element('#main').to.be.active.and.visible;
70 });
71
72 it('have .attribute()', () => {
73 browser.expect.element('body').to.have.attribute('data-attr');
74 browser.expect.element('body').to.not.have.attribute('data-attr');
75 browser.expect.element('body').to.not.have.attribute('data-attr', 'Testing if body does not have data-attr');
76 browser.expect.element('body').to.have.attribute('data-attr').before(100);
77 browser.expect.element('body').to.have.attribute('data-attr').equals('some attribute');
78 browser.expect.element('body').to.have.attribute('data-attr').not.equals('other attribute');
79 browser.expect.element('body').to.have.attribute('data-attr').which.contains('something');
80 browser.expect
81 .element('body')
82 .to.have.attribute('data-attr')
83 .which.matches(/^something\ else/);
84 // with assertion before the attribute property
85 browser.expect.element('body').to.contain('Search').with.attribute('placeholder');
86 // with multiple properties
87 browser.expect.element('input[name=q]').to.have.attribute('class').and.attribute('id').contain('searchbox');
88 });
89
90 it('have .css(property)', () => {
91 browser.expect.element('#main').to.have.css('display');
92 browser.expect.element('#main').to.have.css('display', 'Testing for display');
93 browser.expect.element('#main').to.not.have.css('display');
94 browser.expect.element('#main').to.have.css('display').before(100);
95 browser.expect.element('#main').to.have.css('display').which.eq('block');
96 browser.expect.element('#main').to.have.css('display').which.contains('some value');
97 browser.expect
98 .element('#main')
99 .to.have.css('display')
100 .which.matches(/some\ value/);
101 });
102
103 it('have .enabled', () => {
104 browser.expect.element('#weblogin').to.be.enabled;
105 browser.expect.element('#main').to.not.be.enabled;
106 browser.expect.element('#main').to.be.enabled.before(100);
107 });
108
109 it('have .present', () => {
110 browser.expect.element('#main').to.be.present;
111 browser.expect.element('#main').to.not.be.present;
112 browser.expect.element('#main').to.be.present.before(100);
113 });
114
115 it('have .property', () => {
116 browser.expect.element('body').to.have.property('className').equals('test-class');
117 browser.expect
118 .element('body')
119 .to.have.property('className')
120 .matches(/^something\ else/);
121 browser.expect.element('body').to.not.have.property('classList').equals('test-class');
122 browser.expect.element('body').to.have.property('classList').deep.equal(['class-one', 'class-two']);
123 browser.expect.element('body').to.have.property('classList').contain('class-two');
124 browser.expect.element('body').to.have.domProperty('classList').contain('class-two');
125 });
126
127 it('have .selected', () => {
128 browser.expect.element('#main').to.be.selected;
129 browser.expect.element('#main').to.not.be.selected;
130 browser.expect.element('#main').to.be.selected.before(100);
131 });
132
133 it('have .text', () => {
134 browser.expect.element('#main').text.to.equal('The Night Watch');
135 browser.expect.element('#main').text.to.not.equal('The Night Watch');
136 browser.expect.element('#main').text.to.equal('The Night Watch').before(100);
137 browser.expect.element('#main').text.to.contain('The Night Watch');
138 browser.expect.element('#main').text.to.match(/The\ Night\ Watch/);
139 });
140
141 it('have .value', () => {
142 browser.expect.element('#q').to.have.value.that.equals('search');
143 browser.expect.element('#q').to.have.value.not.equals('search');
144 browser.expect.element('#q').to.have.value.which.contains('search');
145 browser.expect.element('#q').to.have.value.which.matches(/search/);
146 });
147
148 it('have .visible', () => {
149 browser.expect.element('#main').to.be.visible;
150 browser.expect.element('#main').to.not.be.visible;
151 browser.expect.element('#main').to.be.visible.before(100);
152 });
153
154 it('have .domProperty(property)', () => {
155 browser.expect.element('#main').to.have.domProperty('classList');
156 browser.expect.element('#main').to.have.domProperty('classList', 'Testing DOM property classList exists');
157 browser.expect.element('#main').to.not.have.domProperty('classList');
158 browser.expect.element('#main').to.have.domProperty('classList').before(100);
159 browser.expect.element('#main').to.have.domProperty('classList').which.equals('js-search-input');
160 browser.expect.element('#main').to.have.domProperty('classList').which.includes('search');
161 browser.expect
162 .element('#main')
163 .to.have.domProperty('classList')
164 .which.matches(/search/);
165 });
166});
167
168describe('expect.section()', () => {
169 // Just check if it contains some properties of `expect.element()`.
170 it('have .present', () => {
171 browser.expect.section('@main').to.be.present;
172 browser.expect.section('@main').to.not.be.present;
173 browser.expect.section('@main').to.be.present.before(100);
174 });
175
176 it('have .visible', () => {
177 browser.expect.section('@main').to.be.visible;
178 browser.expect.section('@main').to.not.be.visible;
179 browser.expect.section('@main').to.be.visible.before(100);
180 });
181});
182
183describe('expect.elements()', () => {
184 it('description', () => {
185 browser.expect.elements('div').count.to.equal(10);
186 browser.expect.elements('p').count.to.not.equal(1);
187
188 expectError(browser.expect.elements('div').value.contain('nightwatch'))
189 });
190
191 it('selector element properties - all & required', () => {
192 browser.expect
193 .elements({
194 selector: 'div',
195 locateStrategy: 'css selector',
196 index: 0,
197 timeout: 3000,
198 retryInterval: 1000,
199 suppressNotFoundErrors: false,
200 abortOnFailure: true,
201 })
202 .count.to.equal(10);
203 browser.expect
204 .elements({
205 selector: 'div',
206 })
207 .count.to.not.equal(1);
208 });
209});
210
211it('expect.title()', () => {
212 browser.expect.title().to.contain('value');
213 browser.expect.title().to.match(/value/);
214});
215
216it('expect.url()', () => {
217 browser.expect.url().to.equal('https://nightwatch.org');
218 browser.expect.url().to.contain('https://');
219 browser.expect.url().to.endWith('.org');
220});
221
222it('chains expect assertions', () => {
223 browser
224 .navigateTo('https://duckduckgo.com')
225 .expect.element('input[name=q]')
226 .with.attribute('placeholder')
227 .toContain('Search')
228 .expect.elements('input')
229 .count.toEqual(4)
230 .assert.titleContains('Privacy')
231 .url('https://google.com');
232
233 const result = browser.expect.elements('div').count.toEqual(4);
234 expectType<NightwatchAPI>(result);
235
236 expectNotAssignable<PromiseLike<any>>(result);
237});
238
239it('works with async/await', async () => {
240 const result = await browser.expect.element('#main').text.to.equal('The Night Watch');
241 expectType<NightwatchExpectResult>(result);
242 expectType<null>(result.value);
243});