Selenium(C#)jQuery UI网站中的FindsBy PageObjectPattern问题

Selenium(C#)jQuery UI网站中的FindsBy PageObjectPattern问题,c#,selenium-webdriver,C#,Selenium Webdriver,我正在使用带有PageObject模式的PageFactory 在其中一个网站(重jQuery)中,有些页面加载速度慢,尤其是第一次调用页面时。我得到“元素未显示”或“NoTouchElementException”等下拉列表 所以,因为当我执行myproperty.Click()时,我的FindsBy出现了一个错误,从窗口消失了 我开始调用我的“WaitElementVisible”扩展方法,取回一个元素,然后单击 这使得我的属性毫无意义。我是否遗漏了显而易见的东西 [FindsBy(Ho

我正在使用带有PageObject模式的PageFactory

在其中一个网站(重jQuery)中,有些页面加载速度慢,尤其是第一次调用页面时。我得到“元素未显示”或“NoTouchElementException”等下拉列表

所以,因为当我执行myproperty.Click()时,我的FindsBy出现了一个错误,从窗口消失了 我开始调用我的“WaitElementVisible”扩展方法,取回一个元素,然后单击 这使得我的属性毫无意义。我是否遗漏了显而易见的东西

  [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()

   }
问题:

  • 构造函数BasePage是否应该实现“WaitForPageToLoad”类型 查找要显示的元素的方法? 这能解决问题吗

  • PageInitElement是否只是初始化属性,如果找不到,则将其设置为null。这是否正确

  • 我最好忘记“[FindsBy(等)],用一种方法做每件事

  • 是否可以编写自己的“FindByWithWait”属性,以及如何做到这一点,并避免编写如下扩展方法:

  • 示例代码:

        //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
    
  • Selenium已经在等待加载初始网页,但是您可以自己从那里开始
  • 我相信init只是初始化属性
  • PageFactory可能会给C带来麻烦,现在我发现,最好是使用vars和PageFactory元素创建。例如:

    public string login = "user_login";
    
     By user_login = new By.Id(login);
    
     [FindsBy(How = How.Id, Using = login)]
     public IWebElement txtUserName { get; set; }
    
  • 您可以编写自己的等待方法。示例:

  • Selenium已经在等待加载初始网页,但是您可以自己从那里开始
  • 我相信init只是初始化属性
  • PageFactory可能会给C带来麻烦,现在我发现,最好是使用vars和PageFactory元素创建。例如:

    public string login = "user_login";
    
     By user_login = new By.Id(login);
    
     [FindsBy(How = How.Id, Using = login)]
     public IWebElement txtUserName { get; set; }
    
  • 您可以编写自己的等待方法。示例: