1

How to convert IWebElement in Locator Selenium WebDriver Page Objects?

I work like this:

using OpenQA.Selenium.Support.PageObjects;
        [FindsBy(How = How.Id, Using = "user")]
        public IWebElement txtUser { get; set; }

        public void fillUserField(string user)
        {
           wait.Until(ExpectedConditions.ElementIsVisible(By.Id("user")));
           txtUser.SendKeys(user);
        }

I do not want to repeat the ID "user" in wait.

I don't work like this:

    public void fillUserField(string user)
    {   // TO DO - Convert IWebElement in Locator (BY)
        //Argument1: Cannot convert from 'OpenQA.Selenium.IWebElement' to 'OpenQA.Selenium.By'  
        wait.Until(ExpectedConditions.**ElementIsVisible(txtUser)**);
        txtUser.SendKeys(user);
    }

Is possible? Thanks!

  • If you are going to stick with this model you can create a separate method that takes `IWebElement`, pass it your elements (e.g. `txtUser`), and then handle in that method with a try catch waiting until the element is found/ready/visible. I can't see how to make `wait` in your scenario work without duplicating the By locator type string. IMO, the ideal solution for wait on elements is this: http://stackoverflow.com/a/7312740/2246511 But I don't know how you could use that with PageObjects. – jibbs Dec 15 '16 at 22:37

1 Answers1

-1

You would declare a locator at the top of the class.

public By userLocator = By.Id("user");

and then use it like

wait.Until(ExpectedConditions.ElementIsVisible(userLocator));
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • 1
    You would still have the duplication that Ramon is trying to avoid with the FindsBy attribute of the txtUser property he already has and then again in the userLocator field you recommend. – jibbs Dec 15 '16 at 21:45
  • @jibbs Yes but he wouldn't need the `FindsBy` at that point so the duplication would be removed. – JeffC Dec 16 '16 at 00:01
  • 1
    @JeffC This solution does not solve a duplication, but I solved the problem like this: wait.Until (ExpectedConditions.ElementToBeClickable (+1 overload)); Thanks – Ramon Souza Dec 16 '16 at 17:58