繁体   English   中英

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

[英]FindsBy-PageObjectPattern in Selenium (C#) jQuery UI websites issues

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

在其中一个网站(沉重的jQuery)中,一些页面加载缓慢,特别是第一次调用页面。 我得到“元素未显示”或“NoSuchElementException”等。下拉同样的事情。

因为当我执行myproperty.Click()时,我得到一个错误我的FindsBy离开了Windows我开始调用我的“WaitElementVisible”extensionMethod而是,返回一个元素,然后点击这使得我的属性属性毫无意义。我错过了明显?

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

   }

问题:

  1. 构造函数BasePage应该实现一个“WaitForPageToLoad”类方法寻找要显示的元素吗? 这会解决问题吗?

  2. PageInitElement是否只是初始化属性,如果没有找到,则将其设置为null。这是正确的吗?

  3. 我最好忘记“[FindsBy(等等)”并在方法中做所有事情?

  4. 是否可以编写自己的“FindsByWithWait”属性以及如何执行此操作并避免编写如下所示的extensionMethod:

示例代码:

    //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
  1. Selenium已经等待加载初始网页了,但是从那里你就可以自己了。
  2. 我相信init只是初始化属性。
  3. 现在我发现了PageFactory对C#来说很麻烦,我最好创建By 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; } 
  4. 您可以编写自己的等待方法。 示例: WaitForElementsOnPage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM