Selenium IDE window focus in Internet Explorer - selenium

I'm using selenium IDE and webdriver to test a web page in Internet Explorer. I discovered a while back that IE will not fully accept commands from Selenium if it's not the window in focus. For example, if the Selenium IDE window is in focus, and the command is to click a button in IE, the button will push down, but it won't let go.
With that in mind, my test involves popping up a window, doing a few things in it, leaving it open and returning to the null window to do a few things, then returning to the popup for a few more commands.
Is there a way I can make the null window come forward (over the popup) when I need to execute the commands for the null window? And then vice versa, can I make the popup then come forward when I need to return to it? I tried using windowFocus, but that did not work.

Use the SwitchTo() method and the TargetLocator Interface in Selenium.
A really simple example would look like this:
// Switch to new window
public String SwitchToNewWindow()
{
// Get the original window handle
String winHandleBefore = driver.getWindowHandle();
foreach(String winHandle in driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
return Constants.KEYWORD_PASS;
}
// Switch back to original window
public String switchwindowback()
{
String winHandleBefore = driver.getWindowHandle();
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
return Constants.KEYWORD_PASS;
}

I remembered that webdriver sometimes acts differently than running non-webdriver tests. It turns out that using windowSelect followed by windowFocus switches between windows when running webdriver tests.

Related

How to close popup windows in Selenium?

I'm doing a test with java and popup windows appear (ads) and I do not know how to close them to continue with my test.
I have to click on a button but I change the focus to the window.
The url is www.orbitz.com
Please help!
orbitz
Java solution:
//switch to opened tab
ArrayList<String> tabs_windows = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs_windows.get(1));
//close current tab and switch driver back to original
((JavascriptExecutor)driver).executeScript("close();");
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
I haven't tested this for a while, but in different browsers getWindowHandles() used to return different orders for the tabs. Not sure if you can assume 0 is the first tab for all browsers. Change the index accordingly, or store the current handle before popup, and close all that don't match that.

Close download bar

I am using Java and Selenium to write a test. Somewhere in my test, I download a file but then need to click on a button under the download bar which appears at the bottom of the chrome browser page. I search a lot but the only solution was here which is not my case as I don't have a scroll.
I also use:
action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);
ArrayList<String> tabs2 = new ArrayList<String> (driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs2.get(1));
Thread.sleep(500);
driverChrome.close();
driverChrome.switchTo().window(tabs2.get(0));
Thread.sleep(500);
but it doesn't open the download page.
Anyway that I can close the download bar?
This method did not work for me either, but I developed a workaround. I do any download test in a new window, then close the download window, the original window does not have the download bar. It must be a new window, if you do a new tab it will transfer over, to get this I use JavaScript. Switch to the new window, run download test and then switch to the original window when done.
string javascript = $"$(window.open('', '_blank', 'location=yes'))";
((IJavaScriptExecutor)Driver).ExecuteScript(javascript); //create new window
Driver.SwitchTo().Window(Driver.WindowHandles.Last())); //switch to new window
//do download test here
Driver.Close(); //close created window
Driver.SwitchTo().Window(Driver.WindowHandles.First()); //back to original window with no download bar

How to handle windows download popup for IE11 using Selenium Webdriver without using AutoIT

While testing on IE11, When I click on the link to download a doc, a new blank window opens up with save and Cancel option popup. I want to hit cancel, Switch back to my current window and continue validation.
Can anyone help me how to handle this without the use of AutoIT.
If you are using selenium-webdriver with Java then you can use Sikuli-api.
Just a small introduction: Sikuli is a tool which helps to achive automation task for any software(web/standalone). Base of Sikuli is a Screenshot of the control you would like to interact with.
In your case, it is just matter of 2 buttons. Hence I would not suggest you to use seperate autoIT generated *.exe file.
Just take screenshot of OK & Cancel button.
Showing you the sample code here:
import org.sikuli.script.*;
public class Test {
Screen m_screen;
SikuliScript m_sikscr;
#Test
public void Test1() throws FindFailed
{
m_screen=new Screen();
m_screen.wait((double) 10.0);
//Click on Cancel button
m_screen.click(new Pattern("./img/CancelButton.png"));
}
}
This much should do your task.
Please Note, if at all you decide to use this method, make sure that you handle all Sikuli related dependencies in java project.
Try manually at first whether by pressing escape key it dismissing the popup.
if so follow the below code,
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
r.keyPress(KeyEvent.VK_ESCAPE);

Selenium - how to change driver instance from parent to child window

I have a situation in Selenium , where we are launching new browser instance using Internet explorer driver .
normally the load URL is given in the same instance of IE that is launched this becomes the parent window , Now my issue is in parent window on click of a button a child window will open I need to give my second URL in child window which will continue to be my main page
The language we are using is Java
Used the below code to switch window from parent to child window
public static boolean handleMultipleWindows(WebDriver Driver) throws InterruptedException
{
Thread.sleep(4000);
String currentWindow= Driver.getWindowHandle(); // Storing parent window reference into a String Variable
System.out.println(" Check title " + Driver.getTitle() + Driver.getWindowHandles().size());
for (String popUpHandle : Driver.getWindowHandles()) {
if(popUpHandle.equalsIgnoreCase(currentWindow))
continue;
Driver.switchTo().window(popUpHandle);
String sTitle = Driver.getTitle();
but the problem in our application is the parent window gets close and when i try to play back the recording getting the below error
org.openqa.selenium.WebDriverException: Unable to get browser (WARNING: The server did not provide any stacktrace information).
Use method switchTo().window() method to switch between IE windows.
You didn't specify programming language that you use so my example is in Java:
//get window handlers as list
List<String> browserWindows = new ArrayList<String> (driver.getWindowHandles());
//switch to new window
driver.switchTo().window(browserTabs.get(1));
//do something
//then close window and get back
driver.close();
driver.switchTo().window(browserTabs.get(0));

How to click on facebook longin dialong window using Selenium tool

Hi All,
I'm new buddy to the selenium web automation tool. I'm stuck with an scenario were in I need to send log in credentials in the Facebook login window page to complete my test case. So please help out or provide some suggestion to pass the credentials and login to the Facebook account.
I tried out using the Selenium IDE and record my test script but while running the recorded script I'm unable to get access to that particular dialog window.
Below I had attached my scenario image
Thanks in Advance.
After lot of trails, I was able to achieved the above scenario by using the following below code.
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
try with this one please
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
//Perform the actions on new window
//close the new window, if that window no more required
driver.close();
//Swithc back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)