Selenium(C#)jQuery UI网站中的FindsBy PageObjectPattern问题
我正在使用带有PageObject模式的PageFactory 在其中一个网站(重jQuery)中,有些页面加载速度慢,尤其是第一次调用页面时。我得到“元素未显示”或“NoTouchElementException”等下拉列表 所以,因为当我执行myproperty.Click()时,我的FindsBy出现了一个错误,从窗口消失了 我开始调用我的“WaitElementVisible”扩展方法,取回一个元素,然后单击 这使得我的属性毫无意义。我是否遗漏了显而易见的东西Selenium(C#)jQuery UI网站中的FindsBy PageObjectPattern问题,c#,selenium-webdriver,C#,Selenium Webdriver,我正在使用带有PageObject模式的PageFactory 在其中一个网站(重jQuery)中,有些页面加载速度慢,尤其是第一次调用页面时。我得到“元素未显示”或“NoTouchElementException”等下拉列表 所以,因为当我执行myproperty.Click()时,我的FindsBy出现了一个错误,从窗口消失了 我开始调用我的“WaitElementVisible”扩展方法,取回一个元素,然后单击 这使得我的属性毫无意义。我是否遗漏了显而易见的东西 [FindsBy(Ho
[FindsBy(How = How.Id, Using = "user_password")]
public IWebElement txtPassword { get; set; }
public void DoSomething()
{
txtPassword.Click // Error Element is not displayed" or "NoSuchElementException"
//So FindsBy pointless And I need todo
var myElement=myDriver.WaitElementVisible(by.Id( "user_password"),30));
myElement.Click()
}
问题:
//Ideally I would like to implement a FindsByWithWait that does below
public static IWebElement WaitElementVisible(this IWebDriver driver, By by, int timeout = 10)
{
return new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until((drv) => {
try
{
var ele = drv.FindElement(by);
return ele.Displayed ? ele : null;
}
catch (StaleElementReferenceException){return null;}
catch (NotFoundException){return null;}
});
}
public abstract class BasePage
{
protected BasePage
{
WaitForPageToLoad();
}
protected void WaitForPageToLoad()
{
var wait = new WebDriverWait(myDriver, TimeSpan.FromSeconds(30));
try
{
wait.Until(p => p.Title == PageTitle);
}
catch (Exception e)
{
//log etc...
}
}
}
public class LoginPage : BasePage
{
[FindsBy(How = How.Id, Using = "user_login")]
public IWebElement txtUserName { get; set; }
[FindsBy(How = How.Id, Using = "user_password")]
public IWebElement txtPassword { get; set; }
[FindsBy(How = How.Name, Using = "commit")]
public IWebElement btnLogin { get; set; }
public void Logon(string userName, string password)
{
txtUserName.Clear();
txtUserName.SendKeys(userName);// sometimes will fail (specially site loaded for first time)
txtPassword.Clear();
txtPassword.SendKeys(password); //(fails specially site loaded for first time)
btnLogin.Click();//THIS MIGHT FAIL
}
}
I hope you can clarify and answer some of the questions above,that will help me greatly with my Selenium learning curve.
Thanks a lot
public string login = "user_login";
By user_login = new By.Id(login);
[FindsBy(How = How.Id, Using = login)]
public IWebElement txtUserName { get; set; }
public string login = "user_login";
By user_login = new By.Id(login);
[FindsBy(How = How.Id, Using = login)]
public IWebElement txtUserName { get; set; }