Skip to content

Commit c4b6192

Browse files
author
Christopher Robinson
committed
Use localhost hostname for local browsers in proxy mode
When running tests in local browsers with native automation disabled, TestCafe previously attempted to resolve a network-accessible hostname. This process can be slow and may fail in environments with restricted or no network access. This change optimizes this behavior by defaulting the hostname to 'localhost' when all browsers are local and a specific hostname has not been configured. This improves test startup time and enhances reliability for local-only test runs.
1 parent cd8b2c6 commit c4b6192

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/configuration/testcafe-configuration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,12 @@ export default class TestCafeConfiguration extends Configuration {
330330
this.mergeOptions({ hostname });
331331
}
332332

333-
public async calculateHostname ({ nativeAutomation } = { nativeAutomation: false }): Promise<void> {
333+
public async calculateHostname ({ nativeAutomation, allBrowsersLocal } = { nativeAutomation: false, allBrowsersLocal: false }): Promise<void> {
334334
await this.ensureHostname(async hostname => {
335335
if (nativeAutomation)
336336
hostname = LOCALHOST_NAMES.LOCALHOST;
337+
else if (!hostname && allBrowsersLocal)
338+
hostname = LOCALHOST_NAMES.LOCALHOST;
337339
else
338340
hostname = await getValidHostname(hostname);
339341

src/runner/bootstrapper.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,16 @@ export default class Bootstrapper {
171171
};
172172
}
173173

174-
private async _setupProxy (): Promise<void> {
174+
private async _setupProxy (browserInfo: BrowserInfoSource[]): Promise<void> {
175175
if (this.browserConnectionGateway.status === BrowserConnectionGatewayStatus.initialized)
176176
return;
177177

178-
await this.configuration.calculateHostname({ nativeAutomation: !this.configuration.getOption(OPTION_NAMES.disableNativeAutomation) });
178+
const allBrowsersLocal = await this._isAllBrowsersLocal(browserInfo);
179+
180+
await this.configuration.calculateHostname({
181+
nativeAutomation: !this.configuration.getOption(OPTION_NAMES.disableNativeAutomation),
182+
allBrowsersLocal,
183+
});
179184

180185
this.browserConnectionGateway.initialize(this.configuration.startOptions);
181186
}
@@ -209,7 +214,7 @@ export default class Bootstrapper {
209214

210215
this._validateUserProfileOptionInNativeAutomation(automated);
211216

212-
await this._setupProxy();
217+
await this._setupProxy(browserInfo);
213218

214219
let browserConnections = this._createAutomatedConnections(automated);
215220

@@ -349,13 +354,17 @@ export default class Bootstrapper {
349354
return testedApp;
350355
}
351356

352-
private async _canUseParallelBootstrapping (browserInfo: BrowserInfoSource[]): Promise<boolean> {
357+
private async _isAllBrowsersLocal (browserInfo: BrowserInfoSource[]): Promise<boolean> {
353358
const isLocalPromises = browserInfo.map(browser => browser.provider.isLocalBrowser(void 0, Bootstrapper._getBrowserName(browser)));
354359
const isLocalBrowsers = await Promise.all(isLocalPromises);
355360

356361
return isLocalBrowsers.every(result => result);
357362
}
358363

364+
private async _canUseParallelBootstrapping (browserInfo: BrowserInfoSource[]): Promise<boolean> {
365+
return this._isAllBrowsersLocal(browserInfo);
366+
}
367+
359368
private async _bootstrapSequence (browserInfo: BrowserInfoSource[], id: string): Promise<BasicRuntimeResources> {
360369
const tests = await this._getTests(id);
361370
const testedApp = await this._startTestedApp();

test/server/bootstrapper-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,39 @@ describe('Bootstrapper', () => {
189189
'the "userProfile" suffix from the following browser aliases: "chrome, edge".');
190190
}
191191
});
192+
193+
it('Should use localhost hostname strategy for local browsers in proxy mode', async () => {
194+
let calculateHostnameOptions = null;
195+
196+
const originalGetOption = bootstrapper.configuration.getOption;
197+
const originalCalculateHostname = bootstrapper.configuration.calculateHostname;
198+
199+
try {
200+
bootstrapper.configuration.getOption = optionName => {
201+
if (optionName === 'disableNativeAutomation')
202+
return true;
203+
204+
return originalGetOption(optionName);
205+
};
206+
207+
bootstrapper.configuration.calculateHostname = options => {
208+
calculateHostnameOptions = options;
209+
};
210+
211+
await bootstrapper._setupProxy([{
212+
browserName: 'firefox',
213+
provider: createBrowserProviderMock({ local: true }),
214+
}]);
215+
216+
expect(calculateHostnameOptions).eql({
217+
nativeAutomation: false,
218+
allBrowsersLocal: true,
219+
});
220+
}
221+
finally {
222+
bootstrapper.configuration.getOption = originalGetOption;
223+
bootstrapper.configuration.calculateHostname = originalCalculateHostname;
224+
}
225+
});
192226
});
193227
});

test/server/configuration-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,20 @@ describe('TestCafeConfiguration', function () {
505505

506506
expect(configuration.getOption('hostname')).eql('123.456.789');
507507
});
508+
509+
it('Native automation is disabled/all browsers are local/hostname is unset', async () => {
510+
await configuration.init();
511+
await configuration.calculateHostname({ nativeAutomation: false, allBrowsersLocal: true });
512+
513+
expect(configuration.getOption('hostname')).eql('localhost');
514+
});
515+
516+
it('Native automation is disabled/all browsers are local/hostname is set', async () => {
517+
await configuration.init({ hostname: '123.456.789' });
518+
await configuration.calculateHostname({ nativeAutomation: false, allBrowsersLocal: true });
519+
520+
expect(configuration.getOption('hostname')).eql('123.456.789');
521+
});
508522
});
509523
});
510524

0 commit comments

Comments
 (0)