selenium的BUG??selenium中启动IE进行测试,在js代码中如果有window.open()函数出现的问题,急救啊!

qinxiaofei 2009-03-16 02:46:31
最近用 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进行测试也可以打开新页面。这是怎么回事啊 ??跪求大家帮忙!!
...全文
2477 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
JLycxygdrz 2009-05-15
  • 打赏
  • 举报
回复
哥们,selenium-browerbot.js文件在哪个目录下面了?
JLycxygdrz 2009-05-15
  • 打赏
  • 举报
回复
重载就弹两次了!!!
qinxiaofei 2009-03-23
  • 打赏
  • 举报
回复
这个问题今天无意间解决了。
在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;

};
qinxiaofei 2009-03-19
  • 打赏
  • 举报
回复
没人关注啊,自己先顶
qinxiaofei 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wh0116 的回复:]
IE阻止了弹出窗口,需要设置下IE选项
[/Quote]


应该不是IE阻止了,因为我已经把IE的阻止弹出窗口给取消了,
而且经过测试,如果不是通过selenium启动的IE, 同样的JS代码window.open()是可以弹出窗口的.
Sou2012 2009-03-17
  • 打赏
  • 举报
回复
没用过。。
wh0116 2009-03-17
  • 打赏
  • 举报
回复
IE阻止了弹出窗口,需要设置下IE选项
qinxiaofei 2009-03-16
  • 打赏
  • 举报
回复
没人关注吗??只好自己先顶了 ,
问题很紧急,在线等待
qinxiaofei 2009-03-16
  • 打赏
  • 举报
回复
同时经过测试,在函数window.open()中含有window name这个参数的时候,selenium利用IE测试就会出错,测试如下:

html中的代码如下:
<!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的大侠赐教
(1).测试案例(Testcase)通过Client Lib的接口向Selenium Server发送Http请求,要求和Selenium Server建立连接。 为什么要通过发送Http请求控制Selenium Server而不采用其他方式呢?从上文可以看出,Selenium Server是一个独立的间服务器(确切地说是代理服务器),它可以架设在其他机器上!所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。 (2).Selenium Server的Launcher启动浏览器,把Selenium Core加载入浏览器页面当,并把浏览器的代理设置为Selenium Server的Http Proxy。 (3).测试案例通过Client Lib的接口向Selenium Server发送Http请求,Selenium Server对请求进行解析,然后通过Http Proxy发送JS命令通知Selenium Core执行操作浏览器的动作。 (4).Selenium Core接收到指令后,执行操作。 (5).浏览器收到新的页面请求信息(因为在(4)Selenium Core的操作可能引发新的页面请求),于是发送Http请求,请求新的Web页面。 由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。 (6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。 (7).Selenium Server的Http Proxy把接收的Web页面返回给浏览器。 因为浏览器存在同源策略,所以Selenium RCSelenium Server需要以这种代理模式运行。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧