2013-03-13 69 views
1

有Silenium服务器,NUnit,WebDriver。ExecuteScript不执行HTML中描述的func,并在页面加载后运行

我有C#DLL与下面的代码:

private IWebDriver driver; 

// ... 

driver = new InternetExplorerDriver(); 

// ... 

[Test] 
public void test() 
{ 
    string testURL = "file:/C:/Tests/test.html"; 
    driver.Navigate().GoToUrl(testURL); 
    IJavaScriptExecutor js = driver as IJavaScriptExecutor; 
    object resFunc = js.ExecuteScript("open_form"); 
    object res = js.ExecuteAsyncScript("alert('hello')"); 
} 

文件test.html样子:

<html> 
<head> 
    <title></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <script type="text/javascript" src="./test.js"></script> 
</head> 
<body unselectable="on"> 
</body> 
</html> 

而且test.js

function open_form() { 
    document.body.style.backgroundColor = "green"; 
} 

我运行控制台NUnit的,当测试“你好“出现,背景颜色不变。 如何运行html内的函数以及哪些正在执行的操作?

回答

0

您错过了括号。

让它:

object resFunc = js.ExecuteScript("open_form()"); 

顺便说一下,你做哪个,不实际上需要从这个函数中使用的返回值。

+0

非常感谢!这有助于运行脚本!但它仍然是要解决与争论的问题。 所以当我将参数传递给函数并输出警报时,参数传递为undefined int dec = 10; string value =(string)js.ExecuteScript(“return open_form()”,new object [] {dec}); function open_form(val){ \t alert(val); \t return val; } – Alexey 2013-03-14 09:52:30