最近用 java写了一个用selenium测试WEB页面的程序,启动测试的浏览器选择为IE,
在javascript代码中如果有window.open()函数,则会出现问题,如下: java程序启动selenium如下: protected DefaultSelenium sele;
protected void setUp() throws Exception
{
try
{
sele = new DefaultSelenium("localhost",4444, "*iexplore", "http://localhost");
sele.start();
}
catch(Exception e)
{
logfile.writeLog(ERROR, e.getMessage());
}
} 在输入密码后,点击登陆按钮的代码如下:
sele.type("password", strPwd[i]);
sele.click("loginout_OkButton");这时,在被测试WEB页面的js代码中关于点击按钮后的处理如下:
var w =screen.width - 10;
var h = screen.height - 60;
win = window.open("./framework.html?sessionId="+sessionid+"&userName="+useName.value , "cgpWin" + Math.round(Math.random()*10000), "height="+ h +", width="+ w +", top=0, left=0, toolbar=no, menubar=no, scrollbars=no,resizable=yes,location=no, status=no") ;
if (!win)
{
window.alert("Your browser has prevented popping-up the new window, please check its setting.");
}
else
{
win.moveTo(0,0);
win.resizeTo(screen.availWidth, screen.availHeight);
setTimeout('closeWin()',1500);
}selenium在运行sele.click("loginout_OkButton")后,测试的页面总是会弹出“Your browser has prevented popping-up the new window, please check its setting” 的提示信息,window.open()打开新页面失败。但是如果直接打开IE,点击登陆则
可以打开新页面,同时,如果selenium启动firefox进行测试也可以打开新页面。这是怎么回事啊 ??跪求大家帮忙!!
在javascript代码中如果有window.open()函数,则会出现问题,如下: java程序启动selenium如下: protected DefaultSelenium sele;
protected void setUp() throws Exception
{
try
{
sele = new DefaultSelenium("localhost",4444, "*iexplore", "http://localhost");
sele.start();
}
catch(Exception e)
{
logfile.writeLog(ERROR, e.getMessage());
}
} 在输入密码后,点击登陆按钮的代码如下:
sele.type("password", strPwd[i]);
sele.click("loginout_OkButton");这时,在被测试WEB页面的js代码中关于点击按钮后的处理如下:
var w =screen.width - 10;
var h = screen.height - 60;
win = window.open("./framework.html?sessionId="+sessionid+"&userName="+useName.value , "cgpWin" + Math.round(Math.random()*10000), "height="+ h +", width="+ w +", top=0, left=0, toolbar=no, menubar=no, scrollbars=no,resizable=yes,location=no, status=no") ;
if (!win)
{
window.alert("Your browser has prevented popping-up the new window, please check its setting.");
}
else
{
win.moveTo(0,0);
win.resizeTo(screen.availWidth, screen.availHeight);
setTimeout('closeWin()',1500);
}selenium在运行sele.click("loginout_OkButton")后,测试的页面总是会弹出“Your browser has prevented popping-up the new window, please check its setting” 的提示信息,window.open()打开新页面失败。但是如果直接打开IE,点击登陆则
可以打开新页面,同时,如果selenium启动firefox进行测试也可以打开新页面。这是怎么回事啊 ??跪求大家帮忙!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script language="javascript" type="text/javascript">
function openWindow()
{
// var win = window.open("./test.html?session=243243&userName=admin","test1");
var win = window.open("./test2.html?session=243243&userName=admin","test2","width=350, height=350, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes,resizable=yes,location=yes, status=yes");
if(!win)
{
alert("open failed");
}
}
</script><body>
<input type="button" id="btn" onclick="openWindow()" value="按钮" />
<input type="checkbox" id="ck1" name="check" disabled=""/>
<input type="checkbox" id="ck2" name="check" disabled="disabled" />
</body>
</html>selenium的代码如下:
import junit.framework.TestCase;
import com.thoughtworks.selenium.*;public class ss extends TestCase
{
protected DefaultSelenium sele;
protected void setUp() throws Exception
{
sele = new DefaultSelenium("localhost",4444, "*iexplore","http://localhost");
//sele = new DefaultSelenium("localhost",4444, "*firefox D:\\WEB-TOOLS\\firefox\\firefox.exe","http://localhost");
sele.start();
}
protected void tearDown() throws Exception
{
//sele.stop();
}
public void testBB()
{
sele.open("http://127.0.0.1/test-sele/index.html");
sele.click("btn");
String ck1 = sele.getValue("ck1");
String ck2 = sele.getValue("ck2");
String atr2 = sele.getAttribute("ck2@disabled");
String atr1 = sele.getAttribute("ck1@disabled");
int i = 0;
i++;
}}错误提示:
file://C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\customProfileDir3365fef9f87f4c4b8bcc4812a0014abb\core\RemoteRunner.hta 的578行的13字符没有权限访问。
不知道原因,希望懂selenium的大侠赐教
而且经过测试,如果不是通过selenium启动的IE, 同样的JS代码window.open()是可以弹出窗口的.
在selenium-browerbot.js文件中,selenium对window.open()进行了重载,我把重载函数重写了一下,问题就OK了,
如下:
var newOpen = function(url, windowName, windowFeatures, replaceFlag) {
// var myOriginalOpen = originalOpen;
//var myOriginalOpen = window.open;
if (isHTA) {
// myOriginalOpen = this[originalOpenReference];
}
if( !windowFeatures )
{
windowFeatures = null;
}
if( !replaceFlag )
{
replaceFlag = null;
} var openedWindow = null;
if( !windowFeatures && !replaceFlag )
{
openedWindow = this.window.open(url, windowName);
}
else
{
openedWindow = this.window.open(url, windowName, windowFeatures, replaceFlag);
}
LOG.debug("window.open call intercepted; window ID (which you can use with selectWindow()) is \"" + windowName + "\"");
if (windowName!=null) {
openedWindow["seleniumWindowName"] = windowName;
} if(openedWindow != null)
{
selenium.browserbot.openedWindows[windowName] = openedWindow;
return openedWindow;
}
return null;
};