Search code examples
c#seleniumselenium-webdriverpageobjectspage-factory

Pass parameters for my element using page object model and page factory


have a problem, my element needs to receive parameters to be able to locate an element, as you can do it using:

[FindsBy(How = How.XPath, Using = "//iframe[contains(@id,'" + VAR + "')]")]
public IWebElement iframe{ get; set; }

I need to pass a parameter, Example:

var alumnoPage = new CrearAlumnoPage();
PageFactory.InitElements(driver, alumnoPage);
alumnoPage .iframe(VAR);

I must have all the elements of the pages there, if it does not break the POM

Do you know any solution?, thanks


Solution

  • There isn't a way to do this using the PageFactory.InitElements method, But you can create a separate method inside your class for this, something like:

    public IWebElement GetIFrame(string id)
    {
        return driver.FindElement(By.Id(id));
    }
    

    So you can call it like this later:

    alumnoPage.GetIFrame("your-unique-id");
    

    If you set up your class like this, you can also use WebDriverWait to get your iframe:

    class CrearAlumnoPage
    {
        private IWebDriver driver;
        private WebDriverWait wait;
    
        public CrearAlumnoPage(IWebDriver driver)
        {
            this.driver = driver;
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
            // WebDriverWait is in OpenQA.Selenium.Support.UI
        }
    
        public IWebElement GetIFrame(string id)
        {
            return wait.Until(ExpectedConditions.ElementLocated(By.Id(id)));
        }
    }