I have the necessity of dynamically create instance of a page (using selenium) at runtime. The idea is to retrieve the class name from a string and initialize it.
Let's say that I have a "GenericPage":
public class GenericPage
{
public void Proceed(string testName)
{
//Basic implementation
}
}
The second class inherit from GenericPage implementing Proceed
public class WhatIsYourNamePage : GenericPage
{
[FindsBy(How = How.Id, Using = "fieldName")]
[CacheLookup]
private IWebElement UserName { get; set; }
[FindsBy(How = How.Id, Using = "fieldSurname")]
[CacheLookup]
private IWebElement UserSurname { get; set; }
[FindsBy(How = How.Id, Using = "submit-first-inner-1")]
[CacheLookup]
private IWebElement Submit { get; set; }
new public void Proceed(string testName)
{
var userData = ExcelDataAccess.GetTestData(testName);
UserName.EnterText(userData.fieldName, Costants.UserNameElementName);
UserSurname.EnterText(userData.fieldSurname, Costants.SurnameElementName);
Thread.Sleep(5000);
Submit.ClickOnIt(Costants.SubmitButtonElementName);
Thread.Sleep(5000);
}
}
So here it is what I have tried to dynamically create WhatIsYourNamePage:
var type = Type.GetType("WhatIsYourNamePage");
var myObject = (GenericPage)Activator.CreateInstance(type);
myObject.Proceed("LogInTest");
Issue is that in this way it calls GenericPage.Proceed, but I want WhatIsYourNamePage.Proceed.
Of course I can change:
var myObject = (WhatIsYourNamePage)Activator.CreateInstance(type);
But I need to do this dynamically from a string. How?