Skip to content

Commit f65c1ca

Browse files
committed
Improve test reliability
1 parent 9498491 commit f65c1ca

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

test/support/utils.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Tab.prototype = {
7878
},
7979

8080
runInFrame: async function(frameUrl, fn, ...args) {
81-
let frame = await this.page.frames().find(f => f.url() === frameUrl);
81+
let frame = await this.page.waitForFrame(frameUrl, { timeout: 500 });
8282
if (!frame) {
8383
throw new Error(`Frame with URL ${frameUrl} not found`);
8484
}

test/tests/translationTest.mjs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,9 @@ describe("Translation", function() {
123123
assert.equal(items.length, 1);
124124
assert.equal(items[0].itemType, 'journalArticle');
125125
var frameURL = getExtensionURL('progressWindow/progressWindow.html');
126-
var message = await tab.runInFrame(frameURL, async function() {
127-
// TODO: A more robust way to wait for the text to show up.
128-
let timeout = Zotero.isFirefox ? 1000 : 100;
129-
await Zotero.Promise.delay(timeout);
130-
return document.querySelector('.ProgressWindow-progressBox').textContent;
131-
});
126+
var frame = await tab.page.waitForFrame(frameURL);
127+
var elem = await frame.waitForSelector('.ProgressWindow-progressBox');
128+
var message = await elem.evaluate(node => node.textContent);
132129
assert.include(message, items[0].title);
133130
});
134131

@@ -152,12 +149,9 @@ describe("Translation", function() {
152149
assert.equal(items.length, 1);
153150
assert.equal(items[0].itemType, 'journalArticle');
154151
var frameURL = getExtensionURL('progressWindow/progressWindow.html');
155-
var message = await tab.runInFrame(frameURL, async function() {
156-
// TODO: A more robust way to wait for the text to show up.
157-
let timeout = Zotero.isFirefox ? 1000 : 100;
158-
await Zotero.Promise.delay(timeout);
159-
return document.querySelector('.ProgressWindow-progressBox').textContent;
160-
});
152+
var frame = await tab.page.waitForFrame(frameURL);
153+
var elem = await frame.waitForSelector('.ProgressWindow-progressBox');
154+
var message = await elem.evaluate(node => node.textContent);
161155
assert.include(message, items[0].title);
162156
});
163157

@@ -170,12 +164,9 @@ describe("Translation", function() {
170164
}, tab.tabId);
171165
await delay(20);
172166
var frameURL = getExtensionURL('progressWindow/progressWindow.html');
173-
var message = await tab.runInFrame(frameURL, async function() {
174-
// TODO: A more robust way to wait for the text to show up.
175-
let timeout = Zotero.isFirefox ? 1000 : 100;
176-
await Zotero.Promise.delay(timeout);
177-
return document.querySelector('.ProgressWindow-progressBox').textContent;
178-
});
167+
var frame = await tab.page.waitForFrame(frameURL);
168+
var elem = await frame.waitForSelector('.ProgressWindow-progressBox');
169+
var message = await elem.evaluate(node => node.textContent);
179170
assert.include(message, "Scarcity or Abundance? Preserving the Past in a Digital Era");
180171
} finally {
181172
await background(() => Zotero.Connector.callMethodWithCookies.restore())
@@ -200,12 +191,9 @@ describe("Translation", function() {
200191
}
201192
}, tab.tabId);
202193
var frameURL = getExtensionURL('progressWindow/progressWindow.html');
203-
var message = await tab.runInFrame(frameURL, async function() {
204-
// TODO: A more robust way to wait for the text to show up.
205-
let timeout = Zotero.isFirefox ? 1000 : 100;
206-
await Zotero.Promise.delay(timeout);
207-
return document.querySelector('.ProgressWindow-error').textContent;
208-
});
194+
var frame = await tab.page.waitForFrame(frameURL);
195+
var elem = await frame.waitForSelector('.ProgressWindow-error');
196+
var message = await elem.evaluate(node => node.textContent);
209197
assert.include(message, "An error occurred while saving this item.");
210198
});
211199
});
@@ -235,14 +223,10 @@ describe("Translation", function() {
235223
Zotero.Connector_Browser.saveWithTranslator(tab, 0).then(deferred.resolve).catch(deferred.reject);
236224
}, tab.tabId);
237225
// Wait for the modal prompt to appear
238-
await delay(500);
239226
var frameURL = getExtensionURL('modalPrompt/modalPrompt.html');
240-
var message = await tab.runInFrame(frameURL, async function() {
241-
// TODO: A more robust way to wait for the text to show up.
242-
let timeout = Zotero.isFirefox ? 1000 : 100;
243-
await Zotero.Promise.delay(timeout);
244-
return document.getElementById('zotero-modal-prompt').textContent;
245-
});
227+
var frame = await tab.page.waitForFrame(frameURL);
228+
var elem = await frame.waitForSelector('#zotero-modal-prompt');
229+
var message = await elem.evaluate(node => node.textContent);
246230
assert.include(message, 'The Zotero Connector was unable to communicate with the Zotero desktop application.');
247231
} finally {
248232
await background(function() {
@@ -270,12 +254,9 @@ describe("Translation", function() {
270254

271255
try {
272256
var frameURL = getExtensionURL('progressWindow/progressWindow.html');
273-
var message = await tab.runInFrame(frameURL, async function() {
274-
// TODO: A more robust way to wait for the text to show up.
275-
let timeout = Zotero.isFirefox ? 1000 : 100;
276-
await Zotero.Promise.delay(timeout);
277-
return document.querySelector('.ProgressWindow-box').textContent;
278-
});
257+
var frame = await tab.page.waitForFrame(frameURL);
258+
var elem = await frame.waitForSelector('.ProgressWindow-box');
259+
var message = await elem.evaluate(node => node.textContent);
279260

280261
assert.include(message, 'zotero.org');
281262
assert.include(message, 'Scarcity or Abundance? Preserving the Past in a Digital Era');

0 commit comments

Comments
 (0)