Search code examples
c#selenium-webdriverwindow-handles

The first loop for printing driver.WindowHandles giving me 2 window handles when only one window is opened


for getting the number of window handles after opening one window and after opening one more window. the first for loop is giving me 2 window handles instead of 1,when only 1 window is opened. whereas the second loop is giving me 2 window handles which is after opening the second window which is correct.

public class BaseTest
{
    public IWebDriver driver;
                                   
    [OneTimeSetUp]
    public void Setup()
    {
        
        driver = new ChromeDriver();
        driver.Manage().Window.Maximize();
    } 

    [OneTimeTearDown] 
    public void TearDown() 
    { 
        driver.Quit();
        extent.Flush();
    }
}


[TestClass]
public class Tests :BaseTest
{
    BaseTest base1=new BaseTest();
     
    [Test]
    public void Test6()
    {
        HTMLtestPage hTMLtestPage= new HTMLtestPage(driver);
        //hTMLtestPage.searchSome();
        hTMLtestPage.htmlres();
    }
}

public class HTMLtestPage
{
    public void htmlres()
    {   
        driver.Url = "https://www.google.com/";
        Console.WriteLine("initial no of windhandls");
        List<String> list = new List<String>(driver.WindowHandles);
        foreach (var x in list)
        {
            Console.WriteLine(x);
        }
        Thread.Sleep(1000);
        driver.SwitchTo().NewWindow(WindowType.Window);
        driver.Url = "https://www.w3schools.com/Xml/xpath_syntax.asp";
        Console.WriteLine(driver.CurrentWindowHandle);
        Thread.Sleep(1000);
        List<String> listn = new List<String>(driver.WindowHandles);
        Console.WriteLine("printing in for loop");
        foreach (var y in listn)
        {
            Console.WriteLine(y);
        }
    }
}

Please Click this link for Results image in standard output


Solution

  • You aren't counting the number of window handles, you are printing them and doing it in such a way that you are confusing yourself of the actual count.

    If you want the count of window handles, use driver.WindowHandles.Count.

    I've updated your script to be more clear.

    driver.Url = "https://www.google.com/";
    Console.WriteLine($"Initial # of WindowHandles: {driver.WindowHandles.Count}");
    foreach (var x in driver.WindowHandles)
    {
        Console.WriteLine(x);
    }
    
    Console.WriteLine("Open new window");
    driver.SwitchTo().NewWindow(WindowType.Window);
    driver.Url = "https://www.w3schools.com/Xml/xpath_syntax.asp";
    Console.WriteLine($"New # of WindowHandles: {driver.WindowHandles.Count}");
    foreach (var x in driver.WindowHandles)
    {
        Console.WriteLine(x);
    }
    

    Output

    Initial # of WindowHandles: 1
    76C157F0651A8249B598F5C8E24DBEAD
    Open new window
    New # of WindowHandles: 2
    76C157F0651A8249B598F5C8E24DBEAD
    66425B47296291D12BB0861E73525D84
    

    So you can see the count of window handles is correct in both cases.