Question

I'm moving from a Java environment to .NET and need to write Webdriver tests using a page object model.

In Java I would use the following annotation:

@FindBy(linkText = "More details")
WebElement moreDetailsButton;

Please would someone be able to tell me how to define a WebElement using C#? Also, is the PageFactory.initElements used the same way?

Thanks Steve

Was it helpful?

Solution

Yes, there is a direct translation.

You are looking for FindsBy:

[FindsBy(How = How.LinkText, Using = "More details")]
private IWebElement moreDetailsButton;

As for the PageFactory.initElements, yes, it's a very similar thing in .NET, usually called in the constructor of the Page Object:

public class LoginPage
{
    private IWebDriver _driver;

    public LoginPage(IWebDriver driver)
    {
        _driver = driver;
        PageFactory.InitElements(_driver);
    }
}

Note, that the Selenium project is entirely open source. You can easily see the source the Page Objects 'helper' classes here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top