Switch between different tabs using capybara or selenium - selenium

I wish to switch between browser tabs for a feature I am testing. However I have not been able to do this. Have tried using the following:
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
The idea been that when I am on the second tab the above code should bring it back to the first tab. However this is not working.
I also just tried to close the second tab using this:
page.execute_script "window.close();"
but this does do anything, the tab is not closed nor is the overall browser window so appears that it is not doing anything.
Had anybody else had problems like this and how did you figure out a solution? I am using FireFox.

Here's my method for closing new tabs.
def close_new_tabs
window = page.driver.browser.window_handles
if window.size > 1
page.driver.browser.switch_to.window(window.last)
page.driver.browser.close
page.driver.browser.switch_to.window(window.first)
end
end
I call this method anytime a new tab may need to be closed.

I was able to switch between tabs using the following
browser.switch_to.window browser.window_handles.last

Related

Is there any way to change focus of browser tabs in robot?

I am using robot framework+python+selenium to create an automation framework.
I am stuck at this point.
On this website. When the link 'a' is clicked, a new tab is opened. On this new new I want to test functionality. My problem is I'm not sure how to change the focus from one tab to another. I tried the switch browser keyword as well as the select window keyword. No luck.
You can make use of the following code change focus of the windows using selenium2Library.
#{windows_num} = List Windows
${nWindows} = Get Length ${windows_num}
${latest_window} = Evaluate ${nWindows}-1
Select Window #{windows_num}[${latest_window}]
Found a simple solution here, that worked for me:
https://www.youtube.com/watch?v=5tVgC-oQVKs

How to open new tabs, switch tabs and close tabs with selenium on chrome after update removed sending shortcuts

There are two parts of switching tabs on chrome (and possibly other drivers too).
You first have to switch the context that the driver is working on, and in my case I also have to change the view so that a certain tab is focused.
You use to be able to just send the short cut keys (ctrl + tab, ctrl + t, etc) to manage switching the views, however that was changed recently.
After causing me a headache for this, and not really finding any good answers to fix this, I came up with some work arounds.
Opening
You use to be able to create tabs on chrome by sending keys the shortcut to the page using the driver. However, it appears that doesn't work any more. Opening tabs in an alternate way is actually fairly easy, by using some Javascript:
((JavascriptExecutor)webdriver).executeScript("window.open();");
You then have to switch the context to the new window you just opened. You do this by using the webdriver.switchTo() method (plenty of documentation on this)
Switching
Another challenge that I faced was switching tabs after the chrome update made it impossible to just use shortcuts. I originally tried using Javascript to give the window a name like:
window.name = 'foo'
And then on a different tab you could do:
window.open('', 'foo')
and it would switch tabs correctly. However the issue with that it would work fine when you are sending it via console, but for whatever reason it would not work when sending it through the JavascriptExecutor.
I found a work around however. It is kind of hacky, but I do not see an alternative as of now. First you want to switch the context to whatever tab you want to be on using the window handles (again plenty of other posts explain how to do this, so I will not go into detail how to get the correct handle) doing this:
webdriver.switchTo().window(windowHandle);
This makes it so now the webdriver is on that tabs context. Then to actually switch the view so that it is synchronized with the context you have to do a bit of a hack. What you do is you open an alert on the context you want to switch views to, and then close the alert. This happens fairly quick so you will not even see the alert open.
((JavascriptExecutor) webdriver()).executeScript("window.alert('Horrible hack to switch tabs')");
webDriver().switchTo().alert().accept();
Closing
Closing is actually very easy. All you do is:
webdriver.close();

Robot Framework - Selenium2Library - Chrome & Edge open new tab instead of window

Sorry if this is a mundane question.
My issue is that the user can click on the "Help" button, which by default will open in a new tab, the help page.
When firing the test in Firefox, it creates a new window. Which is great as I can use the Select Window keyword and give it the URL. However, with Chrome and Edge, it just makes a new tab.
I read that Tabs aren't supported - But the weird part is, the test on Edge passes fine, it is just Chrome which is having the problem. Is there a reason behind that?
Is there a way to force it to be a Window and not a Tab? Or something?
I read that I can input a keyboard shortcut of CTRL + Tab. But that feels really dirty and would prefer a better way of doing it.
Error:
ValueError: Unable to locate window with URL 'http://...
EDIT
OK - So instead of using the URL param, I tried the title instead. Just on a whim. And it worked (?) I have no idea how it worked and why using the URL didnt and the title did: Here are the lines I used:
Run Keyword If '${Browser}' == 'Chrome'
... Select Window 1.7.1 User Guide
... ELSE
... Select Window 1.7.1 User Guide - Welcome to the User Guide
Still though, both the "Gets" did not produce a link to the newly created tab / window though :/ - Many thanks to shicky and Helio!
Are tabs not handled largely the same as windows? You should be able to identify what is on the screen as #Helio says by using one of the following:
Get Window Identifiers
Get Window Names
Here are some similar questions that should help you get started, post back here with some further detail if you need more help.
How to get the current URL in Robot framework?
How to make chrome display on the top opened in selenium [Mac OS X]

Unable to switch among the windows

I am automating using selenium 2.0, my application launches the login page by default in a new window, hence my application has by default two windows. These two windows will remain open always. In this case I could switch between the windows without any problem. The below code is executed without any errors.
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
The problem starts while clicking the menu options a pop up window launches to search the records. Here, I need to switch between these three windows. I tried the below piece of code. It returns only the first two window handles.
Set availableWindows = driver.getWindowHandles();
This popup window is coded in such a way that, "In a .jsp file it is parameterised as window.open()".
Please let me know, if some one could help me on this?
If you're only seeing 2 window in getWindowHandles(), then the popup is probably a iframe. In this event, use driver.switchTo().frame() to switch focus to that frame instead of looking for an entirely new window.
Here's the documentation on the switch method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#switchTo()
One probable solution is to use JavascriptExecutor.executeScript() method to run a javascript code and handle the pop up window without switching into pop up window.
For example, from parent window of pop up window, run a javascript code which is something like this.
JavascriptExecutor exec = (JavascriptExecutor)driver;exec.executeScript("var popup = <<popupopener function>>; //operate on popup object to manipulate the markup of pop up window");

How can we force selenium to focus on new tab?

One of my application after clicking continue button refreshes and opens in a new tab and closes the old tab that had continue button.
Is there anyway that we can select the new tab as the current window and move forward?
I used the following code which didn't work.
selenium.select("");
selenium.select(null);
Any help would be appreciated.
There is an additional option for selenium server. You can add -singlewindow parameter when you are starting selenium server. This parameter will force selenium to open all pages in the same frame. Please look here for more details: http://seleniumhq.org/docs/05_selenium_rc.html#server-options
Best Regards
Pawel