gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:53:09 25 4
gpt4 key购买 nike

所以我在与下面的 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>

这是我尝试失败的方法:

//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 中使用 Tab 键来找到我想要的元素。我对这样一个脆弱的解决方案真的很不满意,希望你能提供一些建议。

提前致谢!

最佳答案

快速修复是创建一个 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"));

上面代码片段的完整源代码可以在 here 中找到。 .此外,您可以像这样从 Protractor API 为 Angular 元素创建自己的自定义装饰器:

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 中找到。 .

关于c# - 如何使用 Selenium Webdriver 自动测试 Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38593435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com