lundi 14 septembre 2020

getWindowHandles and Iterator equivalent C# - Navigate between tabs

I am used to writing in Java but recently I have been working with C#

Basically I want to click on links on a page and open them in a new tabs and then iterate through the tabs and verify page title of the tabs to ensure the links are working

In Java my code is:

public List<String> linksAreWorkingDashBoardLeftPanal() throws Exception {

        List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
        for (int i = 0; i < li_All.size(); i++) {

            String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
            links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
            Thread.sleep(2000);

        } // Opens all the tabs
        Set<String> getTitleinWindow = driver.getWindowHandles();
        Iterator<String> it = getTitleinWindow.iterator();

        List<String> actualTitleList = new ArrayList<>();
        while (it.hasNext()) {
            driver.switchTo().window(it.next());
            actualTitleList.add(driver.getTitle());
        }

        return actualTitleList;

So far I have the following:

IWebElement table = WebDriver.Driver.FindElement(By.XPath("//table[@id='files_list']//tbody"));
            IList<IWebElement> rows = table.FindElements(By.TagName("tr"));
            foreach (IWebElement row in rows)
            {
                IList<IWebElement> filelink = row.FindElements(By.TagName("a"));
                for (int i = 0; i < filelink.Count(); i++)
                {
                    String clickOnLinkTab = (Keys.Control + "t");
                    row.FindElements(By.TagName("a")).ElementAt(i).SendKeys(clickOnLinkTab);
                    Thread.Sleep(2000);

However I am stuck on this bit:

} // Opens all the tabs
        Set<String> getTitleinWindow = driver.getWindowHandles();
        Iterator<String> it = getTitleinWindow.iterator();

        List<String> actualTitleList = new ArrayList<>();
        while (it.hasNext()) {

            driver.switchTo().window(it.next());
            actualTitleList.add(driver.getTitle());

What's the best way to handle this in C#? I cant really find a similar example online when I search? help would be appreciated greatly

Aucun commentaire:

Enregistrer un commentaire