### Manage Popup Window with JavaScript Source: https://github.com/dev4s/watin/blob/master/source/UnitTests/html/main.html Provides functions to get and manage a popup window reference. Ensure the popup window is initialized before calling GetPopupWindow. ```javascript var popupWindow; function GetPopupWindow() { return popupWindow; } ``` -------------------------------- ### Instantiate and Navigate Browsers Source: https://context7.com/dev4s/watin/llms.txt Demonstrates creating browser instances, navigating URLs, and handling authentication dialogs. ```csharp using WatiN.Core; // Create a new Internet Explorer instance and navigate to a URL using (IE browser = new IE("http://www.example.com")) { // Browser is ready with page loaded Console.WriteLine("Page title: " + browser.Title); Console.WriteLine("Current URL: " + browser.Url); // Navigate to another page browser.GoTo("http://www.google.com"); // Navigate back and forward browser.Back(); browser.Forward(); // Refresh the page browser.Refresh(); // Browser automatically closes when disposed } // Create browser in new process (isolated session) using (IE ie = new IE("http://www.example.com", createInNewProcess: true)) { // Operations in isolated process } // Create browser with authentication var logonHandler = new LogonDialogHandler("domain\\username", "password"); using (IE ie = new IE("https://secure.example.com", logonHandler)) { // Authenticated session } ``` -------------------------------- ### Configure WatiN Settings Source: https://context7.com/dev4s/watin/llms.txt Adjust global timeouts, highlighting, dialog handling, and browser behavior. Use Settings.Clone() or temporary instance swapping for scoped configuration changes. ```csharp using WatiN.Core; // Configure timeout settings Settings.AttachToBrowserTimeOut = 60; // Attach timeout in seconds Settings.WaitUntilExistsTimeOut = 30; // Element wait timeout Settings.WaitForCompleteTimeOut = 30; // Page load timeout // Configure element highlighting Settings.HighLightElement = true; Settings.HighLightColor = "yellow"; // Configure dialog handling Settings.AutoCloseDialogs = true; Settings.AutoStartDialogWatcher = true; // Configure browser behavior Settings.MakeNewIeInstanceVisible = true; Settings.MakeNewIe8InstanceNoMerge = true; Settings.AutoMoveMousePointerToTopLeft = false; // Configure Firefox settings Settings.CloseExistingFireFoxInstances = false; // Reset to defaults Settings.Reset(); // Clone current settings ISettings backup = Settings.Clone(); // Use custom settings temporarily ISettings originalSettings = Settings.Instance; try { Settings.WaitUntilExistsTimeOut = 5; // Quick timeout // Perform operations with short timeout } finally { Settings.Instance = originalSettings; } ``` -------------------------------- ### Interact with Buttons and Links Source: https://context7.com/dev4s/watin/llms.txt Demonstrates clicking elements, handling navigation, and checking element states. Use ClickNoWait for scenarios where the action does not trigger a full page reload. ```csharp using WatiN.Core; using (IE browser = new IE("http://www.example.com")) { // Find and click a button Button submitButton = browser.Button(Find.ById("submit")); submitButton.Click(); // Click button by value text Button loginButton = browser.Button(Find.ByValue("Login")); loginButton.Click(); // Click without waiting for page load (useful for popups/dialogs) Button popupButton = browser.Button(Find.ById("showPopup")); popupButton.ClickNoWait(); // Find and click a link Link homeLink = browser.Link(Find.ByText("Home")); homeLink.Click(); // Get link URL before clicking Link aboutLink = browser.Link(Find.ByText("About")); string url = aboutLink.Url; Console.WriteLine("Link URL: " + url); aboutLink.Click(); // Double-click an element Div item = browser.Div(Find.ById("selectable-item")); item.DoubleClick(); // Check if element is enabled before clicking Button saveButton = browser.Button(Find.ById("save")); if (saveButton.Enabled) { saveButton.Click(); } } ``` -------------------------------- ### Manage Browser Instances and State Source: https://context7.com/dev4s/watin/llms.txt Attach to existing browser windows, control window properties, and manage cookies or cache. ```csharp using WatiN.Core; // Attach to existing IE window by title IE browser = Browser.AttachTo(Find.ByTitle("My Application")); // Attach by URL IE browserByUrl = Browser.AttachTo(Find.ByUrl("http://www.example.com")); // Attach with timeout IE browserWithTimeout = Browser.AttachTo( Find.ByTitle("Slow Loading Page"), timeout: 60); // Check if browser exists bool exists = Browser.Exists(Find.ByTitle("My App")); // Get all open IE instances IECollection allBrowsers = IE.InternetExplorers(); foreach (IE ie in allBrowsers) { Console.WriteLine(ie.Title + " - " + ie.Url); } // Browser window management using (IE ie = new IE("http://www.example.com")) { // Bring to front ie.BringToFront(); // Resize window ie.SizeWindow(1024, 768); // Maximize/minimize ie.ShowWindow(NativeMethods.WindowShowStyle.Maximize); ie.ShowWindow(NativeMethods.WindowShowStyle.Minimize); ie.ShowWindow(NativeMethods.WindowShowStyle.Restore); // Control visibility ie.Visible = true; // Control auto-close behavior ie.AutoClose = false; // Keep browser open after disposal // Cookie management ie.ClearCookies(); // Clear all cookies ie.ClearCookies("http://www.example.com"); // Site-specific // Get/set cookies string cookie = ie.GetCookie("http://www.example.com", "sessionId"); ie.SetCookie("http://www.example.com", "myCookie=value; path=/"); // Clear cache ie.ClearCache(); // Force close all IE instances ie.ForceClose(); } ``` -------------------------------- ### Manage Element Waiting and Existence Source: https://context7.com/dev4s/watin/llms.txt Use these methods to handle asynchronous page loading and verify element states. Ensure the browser instance is initialized before performing these operations. ```csharp using WatiN.Core; using System.Text.RegularExpressions; using (IE browser = new IE("http://www.example.com/dynamic")) { // Check if element exists (does not wait) Button submitBtn = browser.Button(Find.ById("submit")); if (submitBtn.Exists) { submitBtn.Click(); } // Wait until element exists (default 30 seconds) Div results = browser.Div(Find.ById("results")); results.WaitUntilExists(); // Wait with custom timeout (in seconds) Div slowContent = browser.Div(Find.ById("slowLoading")); slowContent.WaitUntilExists(60); // Wait until element is removed from DOM Div loadingIndicator = browser.Div(Find.ById("loading")); loadingIndicator.WaitUntilRemoved(); loadingIndicator.WaitUntilRemoved(timeout: 10); // Wait until attribute has specific value Div status = browser.Div(Find.ById("status")); status.WaitUntil("innertext", "Complete"); status.WaitUntil("className", "success", timeout: 15); // Wait until attribute matches regex status.WaitUntil("innertext", new Regex("(Complete|Done)")); // Wait using constraint Button enabledBtn = browser.Button(Find.ById("action")); enabledBtn.WaitUntil(Find.By("disabled", "false")); // Wait using predicate enabledBtn.WaitUntil