public static T Init <T>() where T : Page { IWebDriver driver = Driver.Current(); T page = (T)typeof(T).GetConstructor(new Type[] { typeof(IWebDriver) }).Invoke(new Object[] { driver }); foreach (FieldInfo field in typeof(T).GetFields()) { FindBy[] locators = (FindBy[])field.GetCustomAttributes(typeof(FindBy)); if (locators != null && locators.Length > 0) { FindBy locator = getLocatorForPlatform(locators, Configuration.Platform); if (locator == null) { locator = getLocatorForPlatform(locators, TargetPlatform.ANY_WEB); } if (locator == null) { locator = getLocatorForPlatform(locators, TargetPlatform.ANY); } if (locator != null) { Control control = (Control)field.FieldType.GetConstructor(new Type[] { typeof(Page), typeof(By) }).Invoke(new Object[] { page, toLocator(locator.Locator) }); field.SetValue(page, control); } } } return(page); }
public static T Init <T>() { IWebDriver driver = Driver.Current(); T page = (T)typeof(T).GetConstructor(new Type[] { typeof(IWebDriver) }) .Invoke(new Object[] { driver }); foreach (FieldInfo field in typeof(T).GetFields()) { FindBy[] locators = (FindBy[])field.GetCustomAttributes(typeof(FindBy)); if (locators != null && locators.Length > 0) { FindBy locator = getLocatorForPlatform(locators, Configuration.Platform); if (locator == null) { locator = getLocatorForPlatform(locators, TargetPlatform.ANY_WEB); } if (locator == null) { locator = getLocatorForPlatform(locators, TargetPlatform.ANY); } if (locator != null) { Control control = (Control)field.FieldType .GetConstructor(new Type[] { typeof(Page), typeof(By) }) .Invoke(new Object[] { page, toLocator(locator.Locator) }); control.ItemLocator = locator.ItemLocator; SubItem[] items = (SubItem[])field.GetCustomAttributes(typeof(SubItem)); if (items != null && items.Length > 0) { control.SubItems = items.Where <SubItem>(t => ( t.Platform.Equals(Configuration.Platform)) || t.Platform.Equals(TargetPlatform.ANY) || t.Platform.Equals(TargetPlatform.ANY_WEB) && Configuration.Platform.IsWeb()) .ToArray <SubItem>(); } control.ExcludeFromSearch = locator.ExcludeFromSearch; field.SetValue(page, control); } } } return(page); }