I have a step definition in my login page bindings
[When(@"I click the '(.*)' button")]
public void IClickTheButton(string buttonName)
{
LoginPage loginPage = new LoginPage();
loginPage.ClickTheButton(buttonName);
}
My Page objects are set up with the ClickTheButton method in the BasePage:
public class LoginPage : BasePage
{
public LoginPage(IWebDriver _driver)
{
driver = _driver;
}
// some methods
}
public class HomePage : BasePage
{
public HomePage(IWebDriver _driver)
{
driver = _driver;
}
// some methods
}
public class BasePage
{
//no constructor atm
public void ClickTheButton(string buttonName)
{
driver.GetFirstButtonWithTextContaining(buttonName).Click();
}
// more methods
}
All buttons in the application are formatted the same so the GetFirstButtonWithTextContaining method will click them all using
driver.FindElements(By.TagName("button")).Where(x => x.Text == buttonName).First();
The problem is I will be using ‘I click the '(.*)' button’ in all my feature files like the HomePage feature, so it doesn’t seem right to use the login page step definition which utilises an instance of the login page class for all the buttons on the other pages.
I was thinking of creating a common step defs file for these type of methods but when I add a constructor to the BasePage (same as the other page object classes) and do the following in the common step defs binding:
BasePage basePage = new BasePage();
basePage.ClickTheButton(buttonName);
Is there a better implementation?...it just seems wrong to use BasePage class but i cant see how to share a step definition across multiple features when using page objects. I’m trying to create as many common steps as possible to share across all features.