Skip to content
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
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Enforce LF line endings for all text files across all platforms
* text=auto eol=lf

# Explicit declarations for common file types
*.js text eol=lf
*.ts text eol=lf
*.json text eol=lf
*.md text eol=lf
*.yml text eol=lf
*.yaml text eol=lf
*.sh text eol=lf

# Binary files
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.woff binary
*.woff2 binary
*.ttf binary
*.eot binary
69 changes: 65 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,77 @@ const validate = () => {

const getClient = () => _client;

const targetExists = async (targetId) => {
try {
const pages = await targetHandler.getFirstAvailablePageTarget();
return pages.some((p) => p.targetId === targetId);
} catch (error) {
logEvent(`Error checking target existence: ${error.message}`);
return false;
}
};

eventHandler.addListener("targetCreated", async (newTarget) => {
const response = await isReachable(
`${defaultConfig.host}:${defaultConfig.port}`,
);
if (response) {
const pages = await targetHandler.getFirstAvailablePageTarget();
await connect_to_cri(pages[0].targetId).then(() => {
logEvent(`Target Navigated: Target id: ${newTarget.targetInfo.targetId}`);
const targetId = newTarget.targetInfo.targetId;
const openerId = newTarget.targetInfo.openerId;

// Wait for authentication redirects to complete
await new Promise((resolve) => setTimeout(resolve, 500));

// Check if target still exists after redirects
const exists = await targetExists(targetId);

if (exists) {
try {
// Attempt 1: Connect directly to the target ID from the event
await connect_to_cri(targetId);
await targetHandler.switchBrowserContext(targetId);
logEvent(`Target Navigated: Target id: ${targetId}`);
eventHandler.emit("targetNavigated");
return;
} catch (error) {
// Log but continue to fallback
if (error.code === "ECONNRESET") {
logEvent(
`Target connection reset (expected for redirects): ${targetId}`,
);
} else {
logEvent(
`Direct target connection failed: ${error.message}. Searching for active target with openerId: ${openerId}`,
);
}
}
} else {
logEvent(
`Target ${targetId} no longer exists after redirects, using fallback`,
);
}

// Fallback: search by openerId
try {
const pages = await targetHandler.getFirstAvailablePageTarget();

// Find the target that was opened from the same opener
// This handles the case where redirects changed the targetId
const newPage =
pages.find((p) => p.openerId === openerId && p.targetId !== openerId) ||
pages.find((p) => p.targetId === targetId) ||
pages[pages.length - 1];

await connect_to_cri(newPage.targetId);
await targetHandler.switchBrowserContext(newPage.targetId);
logEvent(`Target Navigated (fallback): Target id: ${newPage.targetId}`);
eventHandler.emit("targetNavigated");
});
} catch (fallbackError) {
logEvent(
`Fallback target connection also failed: ${fallbackError.message}`,
);
throw fallbackError;
}
}
});

Expand Down
134 changes: 128 additions & 6 deletions 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.4.6",
"version": "1.4.7",
"description": "Taiko is a Node.js library for automating Chromium based browsers",
"main": "bin/taiko.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/write.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ describe("write with hideText option", () => {
});

after(async () => {
await taiko.closeBrowser();
removeFile(filePath);
taiko.closeBrowser();
taiko.__set__("descEvent", actualEmmiter);
});

Expand Down