【问题标题】:How to automate testing of Angular JS using Selenium Webdriver如何使用 Selenium Webdriver 自动测试 Angular JS
【发布时间】:2017-03-04 08:42:19
【问题描述】:

所以我在与下面的 HTML 交互时遇到了问题。我无法使用不同的 Selenium Webdriver 命令打开和关闭弹出窗口。虽然我在这里寻找一个特定的解决方案,但任何有关处理 Angular JS 的一般提示也将不胜感激。我认为问题的根本原因是我不知道使用 Selenium Webdriver 自动化 Angular 的好方法。

我正在使用 C#,但我会从任何编程语言中获取任何有用的信息,因为我可以随时改进解决方案。

我正在尝试与此弹出按钮交互,但未成功: uib-popover-template="'/app/student/assessment/directives/templates/shortTextPopupTemplate.html'"

<div class="line ttsBorder">“<span style="font-style:italic;">And be it further enacted</span><span style="display: inline;">, That in all that territory ceded</span><span short-text-popup="" accession-number="VH209006" class="ng-isolate-scope" ng-non-bindable=""><a uib-popover-template="'/app/student/assessment/directives/templates/shortTextPopupTemplate.html'" popover-is-open="ctrl.isVisible" popover-trigger="none" popover-placement="auto top" tabindex="0" title="Shows more information." ng-click="buttonOnClick($event)" ng-keydown="buttonOnKeydown($event)" ng-class="['stpu-button', {disabled: ctrl.isDisabled, active: ctrl.isVisible}]" class="ng-scope stpu-button" style="z-index: 3;"></a></span> by France to the United States . . . which lies north of thirty‑six degrees and thirty minutes north latitude . . . slavery . . . shall be, and is hereby, forever prohibited.”</div>

这是我尝试失败的方法:

//attempt 1
var elements = Driver.FindElements(By.XPath("//*[@class='line ttsBorder']"));

//attempt 2 - UserInteractions = new Actions(Driver);
UserInteractions.MoveToElement(PopUpButton).Click().Perform();


//attempt 3    
    Driver.FindElement(By.XPath("//[@title='Shows more information.']")).Click();

//attempt 4
//Driver.FindElement(By.XPath("//a[@uib-popover-template]"));
    PopUpButton.Click();

//attempt 5
//Working, but seems dirty - JavaExecutor.ExecuteScript("arguments[0].click();", PopUpButton);

我不得不在 UI 中切换以找到我想要的元素。我对这样一个脆弱的解决方案感到非常不满,希望您能提供一些建议。

提前致谢!

【问题讨论】:

    标签: c# angularjs selenium


    【解决方案1】:

    快速解决方法是创建一个 CssSelector 来访问您的元素,如下所示:Driver.FindElement(By.CssSelector("a[ng-click='buttonOnClick($event)']")); 一个好的解决方法是为您正在测试的每个页面创建一个类,并像这样访问页面的元素:

    class LoginPageObject
        {
            public LoginPageObject()
            {
                PageFactory.InitElements(TestBase.driver, this);
            }
    
            [FindsBy(How = How.Id, Using = "UserName")]
            public IWebElement TxtUsername { get; set; }
    
            [FindsBy(How = How.Id, Using = "Password")]
            public IWebElement TxtPassword { get; set; }
    
            [FindsBy(How = How.Id, Using = "submit")]
            public IWebElement BtnLogin { get; set; }
        }
    

    对于使用 ng 属性访问 Angular 元素,最好使用 Protractor-Net 公开 NgBy 类来探索 DOM 中的 Angular 元素,如下所示:

            var ngDriver = new NgWebDriver(driver);
            ngDriver.Navigate().GoToUrl("http://www.angularjs.org");
            var elements = ngDriver.FindElements(NgBy.Repeater("todo in todoList.todos"));
    

    上面代码sn-p的完整源代码可以在here找到。此外,您可以从量角器 API 为角度元素创建自己的自定义装饰器,如下所示:

    public class NgByRepeaterFinder : By
        {
            public NgByRepeaterFinder(string locator)
            {
                FindElementsMethod = context => context.FindElements(NgBy.Repeater(locator));
            }
        }
    
        internal class NgByModelFinder : By
        {
            public NgByModelFinder(string locator)
            {
                FindElementMethod = context => context.FindElement(NgBy.Model(locator));
            }
        }
    

    然后像这样在你的页面类中使用它们:

    class YourPageObject
    {
        public YourPageObject()
        {
            PageFactory.InitElements(TestBase.ngWebDriver, this);
        }
    
        [FindsBy(How = How.CssSelector, Using = "a[ng-click='addNewTrack()']")]
        public IWebElement BtnAddNewTrack { get; set; }
    
        [FindsBy(How = How.Custom, CustomFinderType = typeof(NgByModelFinder), Using = "trackSearch")]
        public IWebElement TxtSearchTrack { get; set; }
    
        [FindsBy(How = How.Custom, CustomFinderType = typeof(NgByRepeaterFinder), Using = "track in models.tracks | filter: trackSearch")]
        public IList<IWebElement> BtnListTracks { get; set; }
    } 
    

    关于如何为 angularjs 创建和自定义查找器注释器的完整指南可以在 here 中找到。

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2016-04-17
      • 2020-02-29
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多