How to automate Angular JS testing using Selenium Webdriver

So, I ran into the problem of interacting with this HTML below. I cannot open a popup and close it using different Selenium Webdriver commands. Although I'm looking for a specific solution here, any general advice on working with Angular JS will also be appreciated. I believe that the main cause of the problem is that I do not know how to efficiently automate Angular using Selenium Webdriver.

I use C #, but I will take any useful information from any programming language, as I can always modify the solution.

I try to interact with this popup button, unsuccessfully: UIB-popover-template = "'/ application / student / evaluation / 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 codeHide result

Here is what I tried unsuccessfully:

//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);

I had to tabulate through the user interface in order to find the element I want. I am really unhappy with such a fragile decision and hoped you could offer some advice.

Thanks in advance!

+4
source share
1 answer

A quick fix is ​​to create a CssSelector to access your element as follows: Driver.FindElement(By.CssSelector("a[ng-click='buttonOnClick($event)']")); A good solution is to create a class for each page you are testing and reach the elements of your page as follows:

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; }
    }

Angular, ng, Protractor-Net, NgBy Angular DOM, :

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

. , Angular 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 .

+1

Source: https://habr.com/ru/post/1649177/


All Articles