Why I can't set global variable to child window in IE11? - internet-explorer-11

I am trying to do new functionality.
I was able to achieve what I want in modern browsers but not in the IE11.
if (!window.IS_PRESENTING) {
childWindow = window.open(window.location.pathname);
childWindow.IS_PRESENTING = true;
setupMainWindow();
} else {
setupChildWindow();
}
In modern browsers IS_PRESENTING is being set on the child windows. But in the IE11 it doesnt work and the browser keeps opening new windows...
How can I fix this? Any suggestions?

I can reproduce the issue in IE 11. As a workaround, in IE 11, you can get the value of window.IS_PRESENTING in the opening window and set the opening window's IS_PRESENTING value according to it :
In the opening window:
targetWindow = window.opener;
var IS_PRESENTING = !targetWindow.IS_PRESENTING; //targetWindow.IS_PRESENTING can get the value of window.IS_PRESENTING in child window
alert("The value of IS_PRESENTING is " + IS_PRESENTING);
Reference link: Window.opener

Related

How to close newly opened window in Jmeter webdriver Sampler.?

I have automated some actions in JMeter webdriver sampler, in a scenario I am moving to new window, but I am not able to close the newly opened window in that.
WDS.browser.close() does not work there. How can I close only the new window and work with parent?
Thanks in advance.
You could do something like:
var handles = WDS.browser.getWindowHandles()
var iterator = handles.iterator()
var currentHandle = WDS.browser.getWindowHandle()
while(iterator.hasNext()) {
var handle = iterator.next()
if (handle != currentHandle) {
WDS.browser.switchTo().window(handle)
WDS.browser.close()
}
}
WDS.browser.switchTo().defaultContent()
References:
How To Work with Multiple Windows
The WebDriver Sampler: Your Top 10 Questions Answered
What I do (in python though) is switch handles, close the current window, then switch back. Perhaps it can serve as inspiration for you problem :
def close_additional_tabs(self):
self.cfg.logger.info("[TEARDOWN] Close open tabs and switch to original frame.")
while len(self.webdriver.window_handles) > 1:
self.webdriver.switch_to_window(self.webdriver.window_handles[-1])
self.webdriver.close()
time.sleep(1)
self.webdriver.switch_to_window(self.webdriver.window_handles[-1])
This is for tabs but it works with windows as well.

Can HTAs be used to automate web browsing?

I am new to HTAs. I just read https://msdn.microsoft.com/en-us/library/ms536496%28v=vs.85%29.aspx and am a bit confused.
Can I use HTAs to automate browsing? Say I want to download a web page and fill in a form automatically, i.e. from a script. How would an HTA help me do this, if at all? It's important that the JavaScript code in the downloaded page is run as usual. I should be able to enter somehow and fill in the form after it has finished initializing, just as if I were a human agent.
First, you need to open an IE window, as follows:
var IE = new ActiveXObject("InternetExplorer.Application");
Then navigate the IE window to the webpage you want:
IE.Navigate("www.example.com");
Wether your IE window is visible or invisible, it's up to you. Use Visible property to make it visible:
IE.Visible = true;
Then, you should wait until the webpage is completely loaded and then run a function that takes your desired actions. To do so, first, get the HTML document object from the webpage using Document property of IE object, then repeatedly check the readyState property of document object. In the code below, it is assumed that you have a function named myFunc, which takes your desired actions on the webpage. (For example, modifying the contents of the webpage.)
var doc = IE.Document;
interval = setInterval(function() {
try
{
if (doc.readyState == "complete")
{
myFunc();
clearInterval(interval);
}
}
catch (e) {}
}, 1000);
In the function myFunc, you can do anything you want with the webpage since you have HTML document object stored in doc variable. You can also use parentWindow property to get the HTML window object.

NoSuchWindowException when trying to get a report inside a new window

I have the following in my code:
withWindow({ title == 'Google' }) {
report "08"
}
And report is leading me to the exception NoSuchWindowException. I've checked if that was a problem of the Window selector and it isn't,After some research I guessed that the problem was that the driver got messed in the way so I stored and switched my driver:
String mainHandle= driver.getWindowHandle();
driver.switchTo().window('Google');
But I kept getting the same error. So I tried:
driver.get("http://www.google.com");
And it is working but I need to do this dinamycally and automatically because the windows and popups that we are working with are hundreds and with different titles.
How can I achieve that in every windows that opens? We are generating the code with a external tool so I don't need to do "magic", only a driver.get.windowUrl or something like this will work for me, I will add the concurrence later.
If you want to get the last window handle that is opened, you can do something like this:
public String getLastWindowHandle() {
Set<String> windows = webDriver.getWindowHandles();
Iterator<String> itera = windows.iterator();
String window = null;
while (itera.hasNext()) {
window = itera.next();
}
return window;
}
with this handle, you can switch to the new window:
webDriver.switchTo().window(getLastWindowHandle());

Switching to a window with no name

Using the Codeception testing framework and Selenium 2 module to test a website, I end up following a hyperlink that opens a new window with no name. As a result the switchToWindow() function will not work because it is trying to switch to the parent window (which I'm currently on). Without being able to switch to the new window I cannot perform any testing on it.
<a class="external" target="_blank" href="http://mylocalurl/the/page/im/opening">
View Live
</a>
Using both Chrome and Firefox debugging tools I can confirm the new window doesn't have a name, and I cannot give it one because I cannot edit the HTML page I am working on. Ideally I would have changed the HTML to use javascript onclick="window.open('http://mylocalurl/the/page/im/opening', 'myPopupWindow') however this is not possible in my case.
I've looked around on the Selenium forums without any clear method to tackle this problem, and Codeception doesn't appear to have much functionality around this.
After searching around on the Selenium forum and some helpful prods from #Mark Rowlands, I got it to work using raw Selenium.
// before codeception v2.1.1, just typehint on \Webdriver
$I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) {
$handles=$webdriver->window_handles();
$last_window = end($handles);
$webdriver->focusWindow($last_window);
});
Returning back to the parent window was easy because I could just use Codeception's switchToWindow method:
$I->switchToWindow();
Building on the accepted answer, in Codeception 2.2.9 I was able to add this code to the Acceptance Helper and it seems to work.
/**
* #throws \Codeception\Exception\ModuleException
*/
public function switchToNewWindow()
{
$webdriver = $this->getModule('WebDriver')->webDriver;
$handles = $webdriver->getWindowHandles();
$lastWindow = end($handles);
$webdriver->switchTo()->window($lastWindow);
}
Then in the test class I can do this:
$I->click('#somelink');
$I->switchToNewWindow();
// Some assertions...
$I->switchToWindow(); // this switches back to the previous window
I had a heck of a time trying to figure out how to do this by just searching google, so I hope it helps someone else.
Try this,
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
}
make sure to return on parent window using,
browser.close(); // close the popup.
browser.switchTo().window(parentWindowHandle); // Switch back to parent window.
I hope will help you.
Using Codeception 2.2+ it looks like this:
$I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) {
$handles = $webdriver->getWindowHandles();
$lastWindow = end($handles);
$webdriver->switchTo()->window($lastWindow);
});

Safari 5.1 extension development error -> event.userInfo returns null when it try to get selected text

I am trying to create a simple safari extension. My current version of Safari is 5.1.7 running in Snow Leopard.
I have 2 documents :
global.html
<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", performCommand, false);
function performCommand(event) {
if (event.command === "traducir") {
var query = event.userInfo;
alert(query);
query = query.replace(/\s+/g,"+");
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = "http://translate.google.es/#en/es/" + query ;
}
}
</script>
and the injected script : injected.js
document.addEventListener("contextmenu", handleMessage, false);
function handleMessage(msgEvent) {
var sel = '';
sel = window.parent.getSelection()+'';
sel = sel.replace(/^\s+|\s+$/g,"");
safari.self.tab.setContextMenuEventUserInfo(msgEvent, sel);
}
The extension is very simple :
1- When the user selects one text or word, click right-button and select the item of the contextual menu that raise the function.
2- The injected file gets the value of the selected text and it shared with the global.html through the userInfo.
3- The global.html script open a new tab with the url of google translate.
The problem is that event.userInfo is always NULL. I was searching in Google and all the examples are like this and I donĀ“t know where the problem is and why it returns always NULL.
This could possibly be due to a too strict setting for "Access Level" in the Extension Builder. Try setting the dropdown menu to "All", and tick the "Include Secure Pages" checkbox.