Step1 - Create a static driver class
public sealed class Driver
{
[ThreadStatic]
private static IWebDriver driver = null;
public static IWebDriver Instance
{
get
{
if (driver == null)
{
driver = new ChromeDriver();
}
return driver;
}
}
public static void Testcleanup()
{
driver.Quit();
driver = null;
}
}
Step 2 - Create a base class where pageobjects would be initialized
public class TestBase
{
private LoginPage loginPage;
private HomePage homePage;
public TestBase()
{
loginPage = new LoginPage();
homepage = new HomePage();
PageFactory.InitElements(Driver.Instance, loginPage);
PageFactory.InitElements(Driver.Instance, homePage);
}
public static LoginPage Login
{
get { return Instance.loginPage; }
}
public static HomePage Home
{
get { return Instance.homePage; }
}
private static TestBase Instance
{
get
{
return new TestBase();
}
}
}
Step3 : Finally use the base class in test class
[TestClass]
public class PageInitTest : TestBase
{
[TestMethod]
public void PageInit()
{
Driver.Instance.Navigate().GoToUrl("url");
Login.Email.SendKeys("Demo@gmail.com");
}
}