1

有 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 中的函数并执行某些操作?

4

1 回答 1

0

您缺少括号。

做了:

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

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

于 2013-03-14T09:24:47.843 回答