gpt4 book ai didi

c# - 如果我已经有了带有 FindsBy 的元素,如何将 Wait.Until 与 Selenium 一起使用

转载 作者:行者123 更新时间:2023-11-30 20:37:31 26 4
gpt4 key购买 nike

我正在使用 Wait.Until 方法来检查我的页面是否已经加载或是否仍在加载。这是它的样子:

protected IWebElement FindElement(By by, int timeoutInSeconds)
{
StackTrace stackTrace = new StackTrace();
string callingMethod = stackTrace.GetFrame(1).GetMethod().Name;

string message = "Error finding element in method: " + callingMethod;

if (timeoutInSeconds > 0)
{
try
{
WebDriverWait wait = new WebDriverWait(chromeDriver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(ExpectedConditions.ElementIsVisible(by));
Thread.Sleep(800);
}
catch (Exception)
{
Assert(false, message);
throw new Exception(message);
}
}

return chromeDriver.FindElement(by);
}

但是现在我们想改变我们的自动化页面并开始使用 FindBy 来对抗每个元素,就像这样:

  [FindsBy(How = How.Id, Using = "username")]
public IWebElement _logInUserName;

但是 wait.until 需要“by”元素。

我看到了这个问题的抽象解决方案,但它对我的情况没有好处。谁能知道我可以使用的另一种解决方案?

最佳答案

有一个ByFactory class in Selenium .NET solution .我用这个实现来实现你想要的:

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace SeleniumPlayground
{
public static class SeleniumHelper
{
public static FindsByAttribute GetFindsByAttributeFromField(Type pageObject, string iwebElementFieldName)
{

FieldInfo fi = pageObject.GetField(iwebElementFieldName);

FindsByAttribute attr = (FindsByAttribute)fi.GetCustomAttributes(typeof(FindsByAttribute), false).FirstOrDefault();

return attr;
}

public static By GeyByFromFindsBy(FindsByAttribute attribute)
{
var how = attribute.How;
var usingValue = attribute.Using;
switch (how)
{
case How.Id:
return By.Id(usingValue);
case How.Name:
return By.Name(usingValue);
case How.TagName:
return By.TagName(usingValue);
case How.ClassName:
return By.ClassName(usingValue);
case How.CssSelector:
return By.CssSelector(usingValue);
case How.LinkText:
return By.LinkText(usingValue);
case How.PartialLinkText:
return By.PartialLinkText(usingValue);
case How.XPath:
return By.XPath(usingValue);
case How.Custom:
if (attribute.CustomFinderType == null)
{
throw new ArgumentException("Cannot use How.Custom without supplying a custom finder type");
}

if (!attribute.CustomFinderType.IsSubclassOf(typeof(By)))
{
throw new ArgumentException("Custom finder type must be a descendent of the By class");
}

ConstructorInfo ctor = attribute.CustomFinderType.GetConstructor(new Type[] { typeof(string) });
if (ctor == null)
{
throw new ArgumentException("Custom finder type must expose a public constructor with a string argument");
}

By finder = ctor.Invoke(new object[] { usingValue }) as By;
return finder;
}

throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Did not know how to construct How from how {0}, using {1}", how, usingValue));
}
}

这是一个示例用法:

public class Page
{
private IWebDriver driver;

[FindsBy(How = How.Id, Using = "content")]
public IWebElement ele;

public Page(IWebDriver _driver)
{
this.driver = _driver;
}
}

使用如下:

Page page = PageFactory.InitElements<Page>(driver);

FindsByAttribute findsBy = SeleniumHelper.GetFindsByAttributeFromField(typeof(Page), "ele");

By by = SeleniumHelper.GeyByFromFindsBy(findsBy);

关于c# - 如果我已经有了带有 FindsBy 的元素,如何将 Wait.Until 与 Selenium 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35969229/

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