Can't find a popup window opened via target="_blank" - selenium

I am working with a website that has a form that submits a POST request and opens the target page in a new window using target="_blank". Firefox opens it in a new window.
When I do a loop like
String titles[];
do {
titles = browser.getAllWindowTitles();
System.out.println(titles.length);
} while (titles.length < 2);
It always prints out 1, even when the new window pops up and loads. How can I select this new window?
I am using DefaultSelenium. I have tried approaching it in other, indirect ways such as direct URL loading, but the site sends POST requests and I'm unable to simulate those.

Try the below alternative code and check if it is working as expected.
Set titles = browser.getAllWindowTitles();
Iterator i = titles.iterator();
while(i.hasNext()){
System.out.println(titles.size);
}

Related

Opening a new browser tab

I've tried several functions but none seems to be working? For example:
element, _ := webdriver.FindElement(selenium.ByCSSSelector, "body")
element.SendKeys(selenium.ControlKey + "t")
Selenium is capable of executing javascript within the browser.
To open a new tab get selenium to run the following:
window.open()
I've not used Selenium & Go before - so I can't comment on the syntax. However it's normally along the lines of driver.ExecuteScript("window.open()"). See if your IDE will help you plug the gap.
After you get a new tab, you typically need to use the .switchTo in order to move selenium to another tab.
updated:
Docs suggest....
// ExecuteScript executes a script.
ExecuteScript(script string, args []interface{}) (interface{}, error)
see here

Threading and Selenium

I'm trying to make multiple tabs in Selenium and open a page on each tab simultaneously. Here is the code.
CHROME_DRIVER_PATH = "C:/chromedriver.exe"
from selenium import webdriver
import threading
driver = webdriver.Chrome(CHROME_DRIVER_PATH)
links = ["https://www.google.com/",
"https://stackoverflow.com/",
"https://www.reddit.com/",
"https://edition.cnn.com/"]
def open_page(url, tab_index):
driver.switch_to_window(handles[tab_index])
driver.get(url)
return
# open a blank tab for every link in the list
for link in range(len(links)-1 ): # 1 less because first tab is already opened
driver.execute_script("window.open();")
handles = driver.window_handles # get handles
all_threads = []
for i in range(0, len(links)):
current_thread = threading.Thread(target=open_page, args=(links[i], i,))
all_threads.append(current_thread)
current_thread.start()
for thr in all_threads:
thr.join()
Execution goes without errors, and from what I understand this should logically work correctly. But, the effect of the program is not as I imagined. It only opens one page at a time, sometimes it doesn't even switch the tab... Is there a problem that I'm not aware of in my code or threading doesn't work with Selenium?
There is no need in switching to new window to get URL, you can try below to open each URL in new tab one by one:
links = ["https://www.google.com/",
"https://stackoverflow.com/",
"https://www.reddit.com/",
"https://edition.cnn.com/"]
# Open all URLs in new tabs
for link in links:
driver.execute_script("window.open('{}');".format(link))
# Closing main (empty) tab
driver.close()
Now you can handle (if you want) all the windows from driver.window_handles as usual

Geb: Open new tab for each test

I am trying to open new tab for each iteration of the test for each set of data in the where block.
I am trying like:
setup:
Keys.chord(Keys.CONTROL, "t")
but it does not work.
How to do it?
I solved this problem by this [WRITING AT THE BEGGINING OF THE TEST]:
def cachedDriver = CachingDriverFactory.clearCacheAndQuitDriver()
Now a new window is opened and previous window is closed for every set of data in the where block and it is very helpful for executing thousands of tests.
To open a new window using WebDriver and therefore Geb as well you need to call the window.open() javascript method in the browser you're driving. Using Geb it can be done in the following way:
js.exec "window.open('about:blank', '', '')"

Change ItemFileWriteStore URL, get data and refresh Grid

I have an EnhancedGrid with ItemFileWriteStore. After calling startup() on the grid, I hide the same by using following code:
dojo.style(grid.domNode, 'display', 'none');
And then on the click of a button, I change the URL of store for this grid and try to refresh the store and show the grid by using following code:
store.save();
store.close();
store.url='AjaxPopulate.json?os_type='+dijit.byId('osType').get('value');
store.save();
store.fetch({query:{id: '*'}});
dojo.style(grid.domNode, 'display', '');
grid.store.close();
grid.setStore(store);
The above code works fine with Firefox and Chrome but not on IE8 and IE9. I simply get "Object Error" message in IE Developer tools console.
Pls. help me identify any issues with the above code.
Howto reload store
If using the 'data' property to populate store initially (via constructor), you should set clearOnClose: true as well.
Use of .save() is only for a ItemFileWriteStore that has some settings changed (isDirty) and needs to propagate these to server. That said, you dont need .save on a closed store (allthough url has changed, no fetch has been run and definately no items has changed).
Try the following code, you'd only need the grid component to do it as calling .render() on the grid will get it to reload it's data.
// save if dirty, otherwise we cannot close a store unless its reset
grid.store.save();
// close store, this should clear data
grid.store.close();
// set new URL
grid.store.url = '??';
// rerun fetch XHR
// reload grid data with new items (no need to setStore as its same object reused)
grid.store.fetch({query:{id: '*'}, onComplete: function() {grid.render}});
Problem was that I had invalid JSON coming from server with one extra comma.
IE is very specific on these things.
Thanks everyone who viewed and tried to reply.

Cucumber + Capybara tests to ensure a new window is opened

I have the following lines in my feature file:
Given I have website "www.google.co.uk"
When I click the website "www.google.co.uk"
Then "www.google.co.uk" page is opened in a new window
I am struggling to find a way to test that the webpage is definately opened in a new window.
Currently I have been using this in my step def:
Then /url "([^"]*)" is opened in new window/ do |url|
browser = page.driver.browser
current_id = browser.window_handle
tab_id = page.driver.find_window(url)
browser.switch_to.window tab_id
page.driver.browser.close
browser.switch_to.window current_id
end
but this still passes the test if the webpage is loaded on the same page, I want it to fail if the webpage is loaded on the same window/tab.
Any suggestions?
Many thanks
I see no assertions in your test.
My approach would be to test size of window_handles array after performing clicking action on the link, since before clicking the size should equal 1 and after the clicking window_handles should equal 2.
assert page.driver.browser.window_handles.size == 2
Imho, good enough, since if the webpage is loaded in the same tab, the size will be 1 and the test will fail.