2

我有一个像这样的简单测试类:

public class MyTest
{
    const string URL = "https://example.com/content/mypage.aspx";

    IWebDriver driver;
    NgWebDriver ngDriver;

    [SetUp]
    public void Setup()
    {
        driver = new ChromeDriver();
        driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
        ngDriver = new NgWebDriver(driver);
    }

    [TearDown]
    public void Teardown()
    {
        ngDriver.Quit();
    }

    [Test]
    public void Basic()
    {
        ngDriver.Url = URL;

        Assert.IsTrue(ngDriver.FindElement(By.CssSelector("#my")).Displayed);
    }
}

这是HTML片段:

<kendo-button id="my" ng-click="myCtrl.doSomething()">Do Something</kendo-button>

Assert.IsTrue在线收到以下错误:

javascript error: [ng:test] http://errors.angularjs.org/1.3.15/ng/test
JavaScript stack:
Error: [ng:test] http://errors.angularjs.org/1.3.15/ng/test
    at Error (native)
    at https://example.com/AngularJS/1.3.15/angular.min.js:6:417
    at Object.Ld [as getTestability] (https://example.com/AngularJS/1.3.15/angular.min.js:18:468)
    at eval (eval at executeAsyncScript (unknown source), <anonymous>:10:13)
    at eval (eval at executeAsyncScript (unknown source), <anonymous>:18:5)
    at executeAsyncScript (<anonymous>:329:26)
    at <anonymous>:345:29
    at callFunction (<anonymous>:237:33)
    at <anonymous>:247:23
    at <anonymous>:248:3
  (Session info: chrome=49.0.2623.87)
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64) (UnexpectedJavaScriptError)

堆栈跟踪是:

   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteAsyncScript(String script, Object[] args)
   at Protractor.NgWebDriver.WaitForAngular() in c:\Users\Bruno\Projets\GitHub\bbaia\protractor-net\src\Protractor\NgWebDriver.cs:line 315
   at Protractor.NgWebDriver.FindElement(By by) in c:\Users\Bruno\Projets\GitHub\bbaia\protractor-net\src\Protractor\NgWebDriver.cs:line 262

我只有 Protractor 和 Selenium WebDriver Nuget 包。我还需要安装其他东西还是这实际上是代码问题?

4

2 回答 2

0

基于我在下面分享的内容,我发现我的问题不是指定文档的根目录。在我的标签中,我有

data-ng-app='myApp'

创建我的量角器驱动程序的代码是:

ngDriver = new NgWebDriver(driver, "[ng-data='myApp']"

Protractor-net 不会修改这个直接的 CSS 搜索条件,所以有效的是:

ngDriver = new NgWebDriver(driver, "[data-ng-app='myApp']"

不是答案,但我没有能力发表评论。

我看到了同样的问题。进一步调查显示,实例化 NgWebDriver 时会引发异常。检查对象显示创建的对象的 Location、PageSource、Title 和 Url 成员都“抛出了类型为 'System.InvalidOperationException' 字符串 {System.InvalidOperationException} 的异常”。仅在尝试查找元素时才会抛出异常以进行测试。

我的代码如下所示:

        public DefaultPOM(IWebDriver webDriver, string baseURL)
    {
        driver = webDriver;
        this.baseURL = baseURL;
        driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
        driver.Navigate().GoToUrl(baseURL);
        ngDriver = new NgWebDriver(driver, "[ng-app='myApp']");
        ngDriver.Manage().Window.Maximize();
        ngDriver.Navigate().GoToUrl(baseURL);

    }

在创建 ngDriver 对象后立即检查它会显示异常。

如果我在导航之前关闭同步,那么记录异常的驱动程序对象的唯一成员就是 Location。

        ngDriver = new NgWebDriver(driver, "[ng-app='NCTWebPortal']");
        ngDriver.IgnoreSynchronization=true;
        ngDriver.Manage().Window.Maximize();
        ngDriver.Navigate().GoToUrl(baseURL);
        ngDriver.IgnoreSynchronization = false;
于 2016-11-10T14:27:14.730 回答
0

您实际上并没有导航到断言之前的 URL?

尝试 -

[Test]
public void Basic()
{
   NgDriver.Navigate().GoToUrl(URL);
   Assert.IsTrue(ngDriver.FindElement(By.CssSelector("#my")).Displayed);
}
于 2016-04-18T10:27:08.840 回答