Robot Framework verify a new browser tab was opened

20,040

Solution 1

Selenium does not support tabs (as of June 2013, Selenium 2.33.0) and always opens new windows instead. If your test opens a new tab, good luck to you.

That said, if it correctly opens a new window, use Select Window.

Select Window | url=https://twitter.com/expectedPage

My working WebDriver (2.33.0) code in Java, hopefully it will help a little. The problems you are describing are where my Robot knowledge begins to fall off.

@Test
public void targetBlankLinkTest() {
    // load the website and make sure only one window is opened
    driver.get(file("TargetBlankLinkTest.html"));
    assertEquals(1, driver.getWindowHandles().size());

    // click the link and assert that a new window has been opened      
    driver.findElement(By.linkText("Follow us on Twitter!")).click();
    Set<String> windowHandles = driver.getWindowHandles();
    assertEquals(2, windowHandles.size());

    // switch to the new window and do whatever you like
    // (Java doesn't have Switch Window functionality that can select by URL.
    // I could write it, but have been using this trick instead)
    for (String handle : windowHandles) {
        if (handle != driver.getWindowHandle()) {
            driver.switchTo().window(handle);
        }
    }
    assertThat(driver.getCurrentUrl(), containsString("twitter.com"));
}

Solution 2

Using Robot Selenium2Library keywords :

@{windows} =  List Windows
${numWindows} =  Get Length  ${windows}
${indexLast} =  Evaluate  ${numWindows}-1
Should Be True  ${numWindows} > 1
Select Window  @{windows}[${indexLast}]
Location Should Contain  /my/url/whatever
Title Should Be   myTitle
Page Should Contain   ...

You get the idea.

Solution 3

if you want to verify if a new tab has opened or not then you can just compare the window handles before and after clicking a link/element which opens a new tab. Just follow this code might help you.

Here ${LINKDIN} is the element which opens in new tab. so before clicking on it save the window handles in variable and after clicking the element again save the window handle in a variable. now if a new is tab is opened then both the variable value will be different and if no new tab is opened then both variable value is same.in this way you can verify the ne wtab opening.

 Go Back
 ${Current_window}        List Windows
 Click Element            ${LINKDIN}
 ${New_Windows_list}      List Windows
 Should Not Be Equal      ${Current_window}    ${New_Windows_list}
Share:
20,040
Dave
Author by

Dave

Updated on July 09, 2022

Comments

  • Dave
    Dave almost 2 years

    For some of the web links on our page, there are external links which direct the user to say Facebook and Twitter. The links use the HMTL tag target="_blank" so that a new browser tab is opened for our Twitter page.

    I would like to verify 1. the new browser tab is open, 2. set focus to it and 3. validate page elements in the new browser tab. The #3 part would be easy enough, once I get focus on it. Tasks #1 and #2 though, I can't figure out, nor can I find anyone talking about this.

    Using Selenium2Library (WebDriver)