使用量角器的C#单元测试中的操作异常无效

时间:2016-03-31 20:18:43

标签: c# selenium-webdriver protractor protractor-net

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

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包。我还需要安装其他东西吗?这实际上是代码问题吗?

2 个答案:

答案 0 :(得分:0)

您实际上并未在Assert之前导航到该网址吗?

尝试 -

[Test]
public void Basic()
{
   NgDriver.Navigate().GoToUrl(URL);
   Assert.IsTrue(ngDriver.FindElement(By.CssSelector("#my")).Displayed);
}

答案 1 :(得分: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成员&#34;抛出了类型&System; Inc.InvalidOperationException&#39; string {System.InvalidOperationException}&#34;。只有在尝试查找元素时才会抛出异常。

我的代码如下:

        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对象后立即检查它将显示异常。

如果我在导航前关闭同步,则记录异常的驱动程序对象的唯一成员是位置。

        ngDriver = new NgWebDriver(driver, "[ng-app='NCTWebPortal']");
        ngDriver.IgnoreSynchronization=true;
        ngDriver.Manage().Window.Maximize();
        ngDriver.Navigate().GoToUrl(baseURL);
        ngDriver.IgnoreSynchronization = false;