robotframework / SeleniumLibrary

Web testing library for Robot Framework
Apache License 2.0
1.38k stars 761 forks source link

Add functionality provided by new Selenium 4 create and/or switch to new window or tab #1823

Open emanlove opened 1 year ago

emanlove commented 1 year ago

As per the Selenium documentation [1], "Selenium 4 provides a new api NewWindow which creates a new tab (or) new window and automatically switches to it.". This is compared to the previous behavior where "[c]licking a link which opens in a new window [would] focus the new window or tab on screen, but WebDriver will not know which window the Operating System considers active. To work with the new window you will need to switch to it. If you have only two tabs or windows open, and you know which window you start with, by the process of elimination you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original."

References:

  1. Working with windows and tabs
  2. Selenium WebDriver 4: New Window, New Tab & Screenshots - TestProject Blog post
  3. Selenium 4 - New Windows and Tab Utilities - SauceLabs Blog post
emanlove commented 1 year ago

Some of my initial notes on Create and Switch to a new (blank) tab or window

[Ed M.: I have very little usage of the create windows or switching windows in my past history of using the library. So this is an area that I don't have much ideas for or deep understanding on usage, needs and best practices]

Antimatterr commented 1 year ago

@emanlove This functionality is currently used in my automation and in many places so I was currently doing the same thing that is suggested in your comment that we have to switch it using window handles but that keyword creates flaky testcases and I would like to work on this open issue

lisacrispin commented 1 year ago

@Antimatterr are you saying the window handles keyword GetWindowHandle causes test flakiness, am I reading that correctly? I'd like to know more about that. Are the tests flaky because of how they're using the GetWindowHandle, or how they are switching, or... I can't help you with the coding but would be happy to discuss the options for behavior. It seems to me more ideal to open the new window, switch automatically, and then close it when done, but I am not surprised that some apps require more complex window switching than that.

Antimatterr commented 1 year ago

@lisacrispin Regarding the GetWindowHandle function, I've observed that it exhibits varying behavior across different browsers, particularly in Safari. When multiple windows are open, each assigned with a distinct identifier such as '222' and '333', the function returns an array of IDs in reverse order. Specifically, the older window ID occupies the 0th index, while the newer one is placed at the 1st index, resulting in the sequence [333, 222]. However, in Chrome, the order is completely opposite, with the older ID at the 0th index and the newer ID at the 1st index, forming the pattern [222, 333]. This inconsistency deviates from the standard behavior and has necessitated debugging efforts. While alternative approaches, such as utilizing multiple keywords to switch between tabs, have been suggested, this method of elimination does not align with the expected functionality. In scenarios where two tabs are opened, and one takes longer to load than the other, it becomes challenging to determine which window handle corresponds to each tab. Consequently, this lack of clarity renders our tests unreliable. Hence, it is imperative to incorporate an automatic switching or tab creation functionality to mitigate these issues.

Snooz82 commented 1 year ago

@emanlove

As noted in the SeleniumLibrary docs it is already now possible to open a new Window and switching to it. i implemented that some years ago and added an example to the docs.

https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Browser%20and%20Window

Execute Javascript window.open()     # In BrowserA third window is opened.
${handle} Switch Window locator=NEW   # Switched to third window in BrowserA

i agree that this can be improved. I would however say that "just" a new keyword would be the only thing that is needed. A keyword that opens a Window, switches to it and Go To that new url.

proposal Open Window url=http://example.com

Also the docs of Switch Window describe that the locator NEW does automatically switches to the newest open Tab/Window. Tabs or Windows by the way does not make any difference.

I would also say that it should be possible to very reliable switch to the newest Tab by first getting all Handles and then opening the new Tab and then Switching to the newest one by using the list of handles as exclusion list.

Snooz82 commented 1 year ago

After some thoughts i would not go with New Window because the New prefix was not used at SL yet. So i would still go with Open Window

some other thoughts to this topic:

it is already possible to switch to windows by their title (document.title), url (window.location.href) or name (window.name)

the window.name could optionally set be by that keyword.

Even though i do not think that Tab vs Window is important, that could also be an argument to Open Window keyword. Same as name and url of course. Also Set Window Name could be a keyword, that could come handy in that context.