Wednesday, 20 August 2014

Java Webdriver control hangs, while downloading file in InternetExplorer-9

Webdriver control hangs(i.e. suspended) while performing the download file action in InternetExplorer-9.

Prerequisite:
1. "selenium-java-<<Version>>.jar" is set in java build path.
2. "IEDriverServer.exe" is set.

Sample Program:
package SampleTest.WD;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class AppTest {
public static void main(String[] args)  {
try{
File file = new File("Drivers\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver= new InternetExplorerDriver();
driver.get("http://docs.seleniumhq.org/download/");
driver.manage().window().maximize();
Thread.sleep(1000);
                 
                //Click on the link "32 bit Windows IE"
driver.findElement(By.xpath("//*[@id='mainContent']/p[8]/a[1]")).click();
Thread.sleep(1000);
                
                //Click on the link "CHANGELOG".
driver.findElement(By.xpath(".//*[@id='mainContent']/p[8]/a[3]")).click();
}catch (Exception e) {
System.out.println(e);
}
}
}

While executing the above program, webdriver clicks on the link "32 bit Windows IE".
Download options are displayed with "Open", "Save" and "Cancel" buttons, after that webdriver control hangs at IE9 browser and doesn't perform any actions on the browser's(i.e. "CHANGELOG" link is not clicked)


Waited for long time and closed the browser manually, after closing the browser below error message is displayed in the EclipseIDE console.

Error Message:
Started InternetExplorerDriver server (64-bit)
2.42.0.0
Listening on port 25142
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 18 milliseconds
Build info: version: '2.42.2', revision: '6a6995d31c7c56c340d6f45a76976d43506cd6cc', time: '2014-06-03 10:52:47'
System info: host: 'HW1179', ip: '172.16.6.196', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_23'
Session ID: bc5470cb-0a5d-41dd-9b4f-4593c56d347d
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=9, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:25142/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]


Workaround:

Below sample program show's workaround using "java.awt.Robot" API with Webdriver API.

Workaround with below sample program:

package SampleTest.WD;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class AppTest {
public static void main(String[] args)  {
try{
File file = new File("Drivers\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver= new InternetExplorerDriver();
Robot r = new Robot();
 
driver.get("http://docs.seleniumhq.org/download/");
driver.manage().window().maximize();
Thread.sleep(1000);
//Click on the link "32 bit Windows IE".
                //Click the "TAB" button until control reaches the link "32 bit Windows IE". 
for(int i=1;i<=31;i++){
r.keyPress(KeyEvent.VK_TAB);
Thread.sleep(100);
}
r.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
//Save the file, using ALT+S 
/*r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_S);
r.keyRelease(KeyEvent.VK_S);
r.keyRelease(KeyEvent.VK_ALT);*/
 
//If saving the file with ALT+S doesn't work, then go with below steps.
r.keyPress(KeyEvent.VK_ALT);
r.keyPress(KeyEvent.VK_N);
r.keyRelease(KeyEvent.VK_N);
r.keyRelease(KeyEvent.VK_ALT);
 
r.keyPress(KeyEvent.VK_TAB);
r.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
 
//Click on the link "CHANGELOG".
driver.findElement(By.xpath(".//*[@id='mainContent']/p[8]/a[3]")).click();
}catch (Exception e) {
System.out.println(e);
}
}
}

Note:
1. While executing the program(using java.awt.Robot API), user(i.e. You) should not perform any operations like moving the mouse, clicking the keyboard key, etc.
2. Another workaround is by using webdriver with sikuli API. 




No comments:

Post a Comment