Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
emanlove opened this issue May 17, 2023 · 6 comments
Milestone

Comments

@emanlove
Copy link
Member

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 emanlove added this to the V6.2.0 milestone May 17, 2023
@emanlove
Copy link
Member Author

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

  • What is the current state and how does this change from that?

[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
Copy link

@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
Copy link

@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
Copy link

@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
Copy link
Member

Snooz82 commented Jul 18, 2023

@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
Copy link
Member

Snooz82 commented Jul 18, 2023

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.

@emanlove emanlove modified the milestones: v6.2.0, v6.3.0 Nov 19, 2023
@emanlove emanlove modified the milestones: v6.3.0, April 2024 Mar 9, 2024
@emanlove emanlove modified the milestones: April 2024, May 2025 Apr 20, 2024
@emanlove emanlove modified the milestones: May 2024, June 2024 Jun 10, 2024
@emanlove emanlove modified the milestones: Sept 2024, Oct 2024 Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants