Robot Framework verify a new browser tab was opened
Asked Answered
C

6

4

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)

Colon answered 21/6, 2013 at 0:19 Comment(0)
M
3

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"));
}
Matchlock answered 21/6, 2013 at 10:40 Comment(6)
Thanks, Slanec. But, is there any method for at least verifying a new browser tab was opened?Colon
@Colon The closest you can get is Get Window Identifiers which will return one more value after the window has been opened. I use it's Java counterpart all the time for this purpose.Hamfurd
... or simply do the Select Window method and validate something in it (Title Should Be?) and if it fails, it has not been opened.Hamfurd
Tried using Select Window , it did not work. Going to look at Get Window Identifiers and see if I can get that to pull it off.Colon
@Colon Interesting. I have written pure WebDriver code that works for me, so I'll edit it into the answer so you can mimic it using your equivalent methods. I know you're using Robot Framework, but frankly, it doesn't seem likely anyone will show up and help, so at least something is better than nothing :(...Hamfurd
And to your Select Window not working - if your clicked link href was e.g. http://twitter.com/myCompany, try both that and https://twitter.com/myCompany/ (URL that is in the browser). Or try the same window switching mechanism I did - it gets windown identifiers and tries them all (both) one by one and when it reaches an identifier that is not the current window's identifier, it switches to it (works only for two windows, obviously).Hamfurd
A
1

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}
Allahabad answered 21/5, 2017 at 13:23 Comment(0)
C
1

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.

Catechin answered 8/2, 2018 at 17:3 Comment(0)
A
0

I recommend:

1. First, acquire new window handle by using the switchTo() method.
2. Then bring that window into focus using a JavaScriptExecutor:
   ((JavascriptExecutor) myTestDriver).executeScript("window.focus();");
   Using switchTo() without a javascript executor to get focus , is not 
   very reliable in my opinion.
3.  Next, do what you need to do in that window.
4. Use driver.close() to close that window.
5.  Verify you are back at the parent window with focus.
Arty answered 24/6, 2013 at 20:45 Comment(0)
A
0

U can use below code to set focus on new window:

Click element ${a_link_which_opensin_newtab} Select window New

now u can perform your actions on new tab. If u want to switch back the main tab then use

Select window Main

Allahabad answered 19/7, 2018 at 2:14 Comment(0)
A
0

https://www.selenium.dev/documentation/webdriver/interactions/windows/

// Opens a new tab and switches to new tab
driver.switchTo().newWindow(WindowType.TAB);

// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
Anticlimax answered 23/6, 2023 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.