ExecuteScript不执行html中描述的func,并且在加载页面后运行

时间:2013-03-13 18:35:04

标签: javascript testing selenium webdriver

有Silenium服务器,NUnit,WebDriver。

我有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运行测试,当出现“hello”时,背景颜色不会改变。 如何运行html中正在执行某些操作的函数?

1 个答案:

答案 0 :(得分:0)

你错过了括号。

成功:

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

顺便说一句,你实际上 需要使用此函数的返回值。