如何使用Selenium Webdriver自动测试Angular JS

Nik*_*kin 6 c# selenium angularjs

所以我遇到了与下面这个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&nbsp;.&nbsp;.&nbsp;.&nbsp;which lies north of thirty?six degrees and thirty minutes north latitude&nbsp;.&nbsp;.&nbsp;.&nbsp;slavery&nbsp;.&nbsp;.&nbsp;.&nbsp;shall be, and is hereby, forever prohibited.”</div>
Run Code Online (Sandbox Code Playgroud)

这是我尝试失败的原因:

//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);
Run Code Online (Sandbox Code Playgroud)

我不得不通过用户界面来查找我想要的元素.我真的对这种脆弱的解决方案感到不满,并希望你能提供一些建议.

提前致谢!

Ami*_*azi 3

快速解决方法是创建一个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; }
    }
Run Code Online (Sandbox Code Playgroud)

为了使用 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"));
Run Code Online (Sandbox Code Playgroud)

可以在此处找到上述代码片段的完整源代码。此外,您还可以通过量角器 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));
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后在页面类中使用它们,如下所示:

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; }
} 
Run Code Online (Sandbox Code Playgroud)

有关如何创建 AngularJS 的自定义查找器注释器的完整指南可以在此处找到。