Skip to content

feat: waitForNumberOfTabs #4124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/helpers/Playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,21 @@ See [Playwright's reference][41]

- `options` **any**

### waitForNumberOfTabs

Waits for number of tabs.

```js
I.waitForNumberOfTabs(2);
```

#### Parameters

- `expectedTabs` **[number][20]** expecting the number of tabs.
- `sec` **[number][20]** number of secs to wait.

Returns **void** automatically synchronized promise through #recorder

### waitForRequest

Waits for a network request.
Expand Down
15 changes: 15 additions & 0 deletions docs/helpers/Puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,21 @@ See [Puppeteer's reference][23]

- `opts` **any**

### waitForNumberOfTabs

Waits for number of tabs.

```js
I.waitForNumberOfTabs(2);
```

#### Parameters

- `expectedTabs` **[number][10]** expecting the number of tabs.
- `sec` **[number][10]** number of secs to wait.

Returns **void** automatically synchronized promise through #recorder

### waitForRequest

Waits for a network request.
Expand Down
15 changes: 15 additions & 0 deletions docs/helpers/WebDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,21 @@ I.waitForInvisible('#popup');

Returns **void** automatically synchronized promise through #recorder

### waitForNumberOfTabs

Waits for number of tabs.

```js
I.waitForNumberOfTabs(2);
```

#### Parameters

- `expectedTabs` **[number][22]** expecting the number of tabs.
- `sec` **[number][22]** number of secs to wait.

Returns **void** automatically synchronized promise through #recorder

### waitForText

Waits for a text to appear (by default waits for 1sec).
Expand Down
9 changes: 9 additions & 0 deletions docs/webapi/waitForNumberOfTabs.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Waits for number of tabs.

```js
I.waitForNumberOfTabs(2);
```

@param {number} expectedTabs expecting the number of tabs.
@param {number} sec number of secs to wait.
@returns {void} automatically synchronized promise through #recorder
18 changes: 18 additions & 0 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -2583,6 +2583,24 @@ class Playwright extends Helper {
});
}

/**
* {{> waitForNumberOfTabs }}
*/
async waitForNumberOfTabs(expectedTabs, sec) {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
let currentTabs;
let count = 0;

do {
currentTabs = await this.grabNumberOfOpenTabs();
await this.wait(1);
count += 1000;
if (currentTabs >= expectedTabs) return;
} while (count <= waitTimeout);

throw new Error(`Expected ${expectedTabs} tabs are not met after ${waitTimeout / 1000} sec.`);
}

async _getContext() {
if (this.context && this.context.constructor.name === 'FrameLocator') {
return this.context;
Expand Down
18 changes: 18 additions & 0 deletions lib/helper/Puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,24 @@ class Puppeteer extends Helper {
});
}

/**
* {{> waitForNumberOfTabs }}
*/
async waitForNumberOfTabs(expectedTabs, sec) {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
let currentTabs;
let count = 0;

do {
currentTabs = await this.grabNumberOfOpenTabs();
await this.wait(1);
count += 1000;
if (currentTabs >= expectedTabs) return;
} while (count <= waitTimeout);

throw new Error(`Expected ${expectedTabs} tabs are not met after ${waitTimeout / 1000} sec.`);
}

async _getContext() {
if (this.context && this.context.constructor.name === 'Frame') {
return this.context;
Expand Down
18 changes: 18 additions & 0 deletions lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,24 @@ class WebDriver extends Helper {
return this.browser.waitUntil(async () => this.browser.execute(fn, ...args), { timeout: aSec * 1000, timeoutMsg: '' });
}

/**
* {{> waitForNumberOfTabs }}
*/
async waitForNumberOfTabs(expectedTabs, sec) {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeoutInSeconds;
let currentTabs;
let count = 0;

do {
currentTabs = await this.grabNumberOfOpenTabs();
await this.wait(1);
count += 1000;
if (currentTabs >= expectedTabs) return;
} while (count <= waitTimeout);

throw new Error(`Expected ${expectedTabs} tabs are not met after ${waitTimeout / 1000} sec.`);
}

/**
* {{> switchTo }}
*/
Expand Down
6 changes: 3 additions & 3 deletions test/helper/Playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('Playwright', function () {
.then(() => I.dontSee('Hovered', '#show')));
});

describe('#switchToNextTab, #switchToPreviousTab, #openNewTab, #closeCurrentTab, #closeOtherTabs, #grabNumberOfOpenTabs', () => {
describe('#switchToNextTab, #switchToPreviousTab, #openNewTab, #closeCurrentTab, #closeOtherTabs, #grabNumberOfOpenTabs, #waitForNumberOfTabs', () => {
it('should only have 1 tab open when the browser starts and navigates to the first page', () => I.amOnPage('/')
.then(() => I.wait(1))
.then(() => I.grabNumberOfOpenTabs())
Expand Down Expand Up @@ -298,13 +298,13 @@ describe('Playwright', function () {

it('should close other tabs', () => I.amOnPage('/')
.then(() => I.openNewTab())
.then(() => I.wait(1))
.then(() => I.waitForNumberOfTabs(2))
.then(() => I.seeInCurrentUrl('about:blank'))
.then(() => I.amOnPage('/info'))
.then(() => I.openNewTab())
.then(() => I.amOnPage('/login'))
.then(() => I.closeOtherTabs())
.then(() => I.wait(1))
.then(() => I.waitForNumberOfTabs(1))
.then(() => I.seeInCurrentUrl('/login'))
.then(() => I.grabNumberOfOpenTabs())
.then(numPages => assert.equal(numPages, 1)));
Expand Down
4 changes: 2 additions & 2 deletions test/helper/Puppeteer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('Puppeteer', function () {
.then(() => I.dontSee('Hovered', '#show')));
});

describe('#switchToNextTab, #switchToPreviousTab, #openNewTab, #closeCurrentTab, #closeOtherTabs, #grabNumberOfOpenTabs', () => {
describe('#switchToNextTab, #switchToPreviousTab, #openNewTab, #closeCurrentTab, #closeOtherTabs, #grabNumberOfOpenTabs, #waitForNumberOfTabs', () => {
it('should only have 1 tab open when the browser starts and navigates to the first page', () => I.amOnPage('/')
.then(() => I.wait(1))
.then(() => I.grabNumberOfOpenTabs())
Expand All @@ -238,7 +238,7 @@ describe('Puppeteer', function () {
.then(numPages => assert.equal(numPages, 1))
.then(() => I.click('New tab'))
.then(() => I.switchToNextTab())
.then(() => I.wait(2))
.then(() => I.waitForNumberOfTabs(2))
.then(() => I.seeCurrentUrlEquals('/login'))
.then(() => I.grabNumberOfOpenTabs())
.then(numPages => assert.equal(numPages, 2)));
Expand Down
3 changes: 2 additions & 1 deletion test/helper/WebDriver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ describe('WebDriver', function () {
});
});

describe('#switchToNextTab, #switchToPreviousTab, #openNewTab, #closeCurrentTab, #closeOtherTabs, #grabNumberOfOpenTabs', () => {
describe('#switchToNextTab, #switchToPreviousTab, #openNewTab, #closeCurrentTab, #closeOtherTabs, #grabNumberOfOpenTabs, #waitForNumberOfTabs', () => {
it('should only have 1 tab open when the browser starts and navigates to the first page', async () => {
await wd.amOnPage('/');
const numPages = await wd.grabNumberOfOpenTabs();
Expand All @@ -614,6 +614,7 @@ describe('WebDriver', function () {
assert.equal(numPages, 1);

await wd.click('New tab');
await wd.waitForNumberOfTabs(2);
await wd.switchToNextTab();
await wd.waitInUrl('/login');
const numPagesAfter = await wd.grabNumberOfOpenTabs();
Expand Down