Skip to content

Commit

Permalink
#1285 close and reconnect only if target matches active tab (#1414)
Browse files Browse the repository at this point in the history
* #1285 close and reconnect only if target matches active tab

Signed-off-by: NivedhaSenthil <[email protected]>

* #1285 fix closeTab

Signed-off-by: NivedhaSenthil <[email protected]>

* Bump version to 1.0.18

Signed-off-by: NivedhaSenthil <[email protected]>

Co-authored-by: Vinay Shankar Shukla <[email protected]>
  • Loading branch information
NivedhaSenthil and Vinay Shankar Shukla authored Aug 12, 2020
1 parent f19737b commit c232ff1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/handlers/targetHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const getCriTargets = async function (targetObject, host, port) {
};
if (targetObject) {
response = { matching: [], others: [] };
for (const target of targets) {
for (const target of pages) {
if (isMatchingUrl(target, targetObject) || isMatchingRegex(target, targetObject)) {
response.matching.push(target);
} else {
Expand Down
13 changes: 8 additions & 5 deletions lib/taiko.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ module.exports.closeTab = async (targetUrl) => {
if (!matching.length) {
throw new Error(`No tab(s) matching ${targetUrl} found`);
}
let currentUrl = await currentURL();
let activeTab = { url: await currentURL(), title: await title() };
let closedTabUrl;
for (let target of matching) {
closedTabUrl = target.url;
Expand All @@ -726,13 +726,15 @@ module.exports.closeTab = async (targetUrl) => {
port: currentPort,
id: target.id,
});
await _client.close();
}
if (
!targetHandler.isMatchingUrl(others[0], currentUrl) &&
!targetHandler.isMatchingRegex(others[0], currentUrl)
targetUrl === null ||
targetUrl === undefined ||
targetHandler.isMatchingUrl(activeTab, targetUrl) ||
targetHandler.isMatchingRegex(activeTab, targetUrl)
) {
_client.removeAllListeners();
await _client.close();
await connect_to_cri(others[0]);
}
let message = targetUrl
Expand Down Expand Up @@ -1055,13 +1057,14 @@ module.exports.currentURL = currentURL;
*
* @returns {Promise<string>} - The title of the current page.
*/
module.exports.title = async () => {
const title = async () => {
validate();
const result = await runtimeHandler.runtimeEvaluate(
'document.querySelector("title").textContent',
);
return result.result.value;
};
module.exports.title = title;

const checkIfElementAtPointOrChild = async (e) => {
function isElementAtPointOrChild() {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json.schemastore.org/package",
"name": "taiko",
"version": "1.0.17",
"version": "1.0.18",
"description": "Taiko is a Node.js library for automating Chromium based browsers",
"main": "bin/taiko.js",
"bin": {
Expand Down
43 changes: 37 additions & 6 deletions test/unit-tests/closeTab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { eventHandler } = require('../../lib/eventBus');
describe('closeTab', () => {
let _targets = { matching: [], others: [] };
let currentURL = '';
let title = '';
let _isMatchUrl = false;
let _isMatchRegex = false;
let currentTarget, taiko, targetHandler;
Expand Down Expand Up @@ -44,6 +45,7 @@ describe('closeTab', () => {

taiko.__set__('validate', () => {});
taiko.__set__('currentURL', () => currentURL);
taiko.__set__('title', () => title);
targetHandler.__set__('cri', mockCri);
taiko.__set__('targetHandler', mockHandler);
taiko.__set__('descEvent', descEmmitter);
Expand All @@ -66,6 +68,7 @@ describe('closeTab', () => {
taiko.__set__('_client', new EventEmitter());
taiko.__set__('_client.close', () => {});
_targets = { matching: [], others: [] };
currentTarget = undefined;
});

it('should close the browser if there are no tabs to reconnect', async () => {
Expand Down Expand Up @@ -120,7 +123,7 @@ describe('closeTab', () => {
url: 'https://flipkart.com',
});
currentURL = 'https://flipkart.com';
_isMatchUrl = false;
_isMatchUrl = true;
_isMatchRegex = false;

let validatePromise = validateEmitterEvent(
Expand All @@ -131,13 +134,13 @@ describe('closeTab', () => {
await validatePromise;
expect(currentTarget.url).to.be.eql('https://amazon.com');
});
it('should close all matching tabs if target is non active tab', async () => {
it('should close all matching tabs if target is non active tab and no reconnect should happen', async () => {
_targets.matching.push({
id: '1',
type: 'page',
url: 'https://flipkart.com',
});
_targets.others.push({
_targets.matching.push({
id: '2',
type: 'page',
url: 'https://flipkart.com',
Expand All @@ -148,7 +151,7 @@ describe('closeTab', () => {
url: 'https://amazon.com',
});
currentURL = 'https://amazon.com';
_isMatchUrl = true;
_isMatchUrl = false;
_isMatchRegex = false;

let validatePromise = validateEmitterEvent(
Expand All @@ -157,7 +160,35 @@ describe('closeTab', () => {
);
await taiko.closeTab('https://flipkart.com');
await validatePromise;
expect(currentTarget.url).to.be.eql('https://amazon.com');
expect(currentTarget).to.be.undefined;
});
it('should close all matching tabs by title and no reconnect should happen if active tab is not matched', async () => {
_targets.matching.push({
id: '1',
type: 'page',
title: 'Flipkart',
url: 'https://flipkart.com',
});
_targets.matching.push({
id: '2',
type: 'page',
title: 'Flipkart',
url: 'https://flipkart.com',
});
_targets.others.push({
id: '3',
type: 'page',
url: 'https://amazon.com',
});
currentURL = 'https://amazon.com';
title = 'Amazon';
_isMatchUrl = false;
_isMatchRegex = false;

let validatePromise = validateEmitterEvent('success', 'Closed tab(s) matching Flipkart');
await taiko.closeTab('Flipkart');
await validatePromise;
expect(currentTarget).to.be.undefined;
});
it('should close all matching tabs for given regex', async () => {
_targets.matching.push({
Expand Down Expand Up @@ -185,6 +216,6 @@ describe('closeTab', () => {
);
await taiko.closeTab(/http(s?):\/\/(www?).google.(com|co.in|co.uk)/);
await validatePromise;
expect(currentTarget.url).to.be.eql('https://amazon.com');
expect(currentTarget).to.be.undefined;
});
});

0 comments on commit c232ff1

Please sign in to comment.