/// <summary> /// Binds the table element property to the property expression stack. /// </summary> /// <param name="propertyInfo">The property information.</param> /// <param name="context">The page builder context.</param> /// <param name="variableList">The variable list.</param> /// <param name="propertyExpressions">The property expressions.</param> /// <param name="attribute">The attribute.</param> /// <param name="customAttributes">The custom attributes.</param> private void BindTableElementProperty( PropertyInfo propertyInfo, PageBuilderContext context, ICollection <ParameterExpression> variableList, List <Expression> propertyExpressions, ElementLocatorAttribute attribute, IEnumerable <object> customAttributes) { // Get any types and methods var propertyType = propertyInfo.PropertyType; var cellType = propertyType.GetGenericArguments().First(); var enumeratorType = typeof(IEnumerable <>).MakeGenericType(cellType); var assignItemInfo = propertyType.GetMethod("SetDriver", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic); // Create any variables and other experssions var driverVariable = Expression.Variable(enumeratorType, "driver"); variableList.Add(driverVariable); var driverExpression = this.CreateElementListItem(context, this.GetTableDriverType(), variableList, propertyExpressions, attribute, customAttributes); // Add any necessary property expressions var itemVariable = context.CurrentElement.Expression; propertyExpressions.Add(Expression.Assign(itemVariable, Expression.Property(context.Document.Expression, propertyInfo))); propertyExpressions.Add(Expression.IfThen( Expression.Equal(itemVariable, Expression.Constant(null, propertyType)), Expression.Assign(itemVariable, Expression.New(propertyType)))); propertyExpressions.Add(Expression.Assign(driverVariable, Expression.Convert(driverExpression, enumeratorType))); propertyExpressions.Add(Expression.Call(itemVariable, assignItemInfo, new Expression[] { driverVariable })); }
public void TestAttributeWithIdReturnsIdLocator() { var attribute = new ElementLocatorAttribute { Id = "MyId" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.Id("MyId"), item); }
public void TestAttributeWithCssClassReturnsCssClassLocator() { var attribute = new ElementLocatorAttribute { Class = ".something" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.ClassName(".something"), item); }
public void TestAttributeWithIdReturnsCssSelectorLocator() { var attribute = new ElementLocatorAttribute { CssSelector = "div#MyId" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.CssSelector("div#MyId"), item); }
public void TestAttributeWithNameReturnsNameLocator() { var attribute = new ElementLocatorAttribute { Name = "MyName" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.Name("MyName"), item); }
/// <summary> /// Creates the element list item. /// </summary> /// <param name="context">The context.</param> /// <param name="propertyType">Type of the property.</param> /// <param name="variableList">The variable list.</param> /// <param name="propertyExpressions">The property expressions.</param> /// <param name="attribute">The attribute.</param> /// <param name="customAttributes">The custom attributes.</param> /// <returns>The expression used to create the item.</returns> private NewExpression CreateElementListItem( PageBuilderContext context, Type propertyType, ICollection <ParameterExpression> variableList, List <Expression> propertyExpressions, ElementLocatorAttribute attribute, IEnumerable <object> customAttributes) { // Special case for lists Type concreteType; if (propertyType.IsClass && !propertyType.IsAbstract) { concreteType = propertyType; } else { var collectionType = this.GetElementCollectionType(); concreteType = collectionType.MakeGenericType(propertyType.GetGenericArguments()); } var constructor = concreteType.GetConstructors().First(); var parentListType = constructor.GetParameters().First().ParameterType; var parentListVariable = Expression.Variable(parentListType, "collectionParent"); variableList.Add(parentListVariable); // Save the current context item to create the parent, then set it back. var currentItem = context.CurrentElement; context.CurrentElement = new ExpressionData(parentListVariable, parentListType); propertyExpressions.AddRange(this.CreateHtmlObject(context, attribute, customAttributes)); context.CurrentElement = currentItem; return(Expression.New(constructor, parentListVariable, context.Browser.Expression)); }
/// <summary> /// Creates the HTML object for each property that is part of the parent. /// </summary> /// <param name="context">The context.</param> /// <param name="attribute">The attribute.</param> /// <param name="nativeAttributes">The native attributes.</param> /// <returns>The expressions needed to create the list</returns> private IEnumerable <Expression> CreateHtmlObject(PageBuilderContext context, ElementLocatorAttribute attribute, IEnumerable nativeAttributes) { var buildElement = context.CurrentElement ?? context.Document; var objectType = this.GetPropertyProxyType(buildElement.Type); var propConstructor = this.GetConstructor(objectType, context); if (propConstructor == null) { throw this.CreateConstructorException(buildElement.Name, objectType); } var itemVariable = buildElement.Expression; return(new[] { (Expression)Expression.Assign(itemVariable, Expression.New(propConstructor.Item1, propConstructor.Item2)), Expression.Call(Expression.Constant(this), this.assignMethodInfo, Expression.Convert(itemVariable, typeof(TElement)), Expression.Constant(attribute, typeof(ElementLocatorAttribute)), Expression.Constant(nativeAttributes, typeof(object[]))) }); }
public void TestAttributeWithTagNameReturnsTagLocator() { var attribute = new ElementLocatorAttribute { TagName = "input" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.TagName("input"), item); }
public void TestAttributeWithLinkTextReturnsLinkTextLocator() { var attribute = new ElementLocatorAttribute { Text = "Hello World" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.LinkText("Hello World"), item); }
public void TestAttributeWithImageTagAndUrlReturnsXPathLocator() { var attribute = new ElementLocatorAttribute { TagName = "img", Url = "myimage.png" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.XPath("//img[@src='myimage.png']"), item); }
public void TestAttributeWithNoTagNameAndPropertyThrowsAnException() { var attribute = new ElementLocatorAttribute { Type = "submit" }; LocatorBuilder.GetElementLocators(attribute); }
public void TestAttributeWithIdAndTagNameReturnsTwoLocators() { var attribute = new ElementLocatorAttribute { Id = "MyId", TagName = "a" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(2, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.Id("MyId"), item); var item2 = resultList.Last(); Assert.AreEqual(By.TagName("a"), item2); }
public void TestAttributeWithTagNameAndTypeAndTitleReturnsCompoundXPathLocator() { var attribute = new ElementLocatorAttribute { TagName = "input", Type = "email", Title = "my title" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.XPath("//input[@title='my title' and @type='email']"), item); }
public void TestAttributeWithTagNameAndTypeAndIndexReturnsComplexXPathLocator() { var attribute = new ElementLocatorAttribute { TagName = "input", Type = "email", Index = 1 }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.XPath("(//input[@type='email'])[0]"), item); }
public void TestAttributeWithLinkAreaTagAndUrlReturnsXPathLocator() { var attribute = new ElementLocatorAttribute { TagName = "area", Url = "mylink.htm" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.XPath("//area[@href='mylink.htm']"), item); }
/// <summary> /// Assigns the element attributes. /// </summary> /// <param name="control">The control.</param> /// <param name="attribute">The attribute.</param> /// <param name="nativeAttributes">The native attributes.</param> protected abstract void AssignElementAttributes(TElement control, ElementLocatorAttribute attribute, object[] nativeAttributes);
/// <summary> /// Assigns the page element attributes. /// </summary> /// <param name="control">The control.</param> /// <param name="locatorAttribute">The locator attribute.</param> protected virtual void AssignPageElementAttributes(TOutput control, ElementLocatorAttribute locatorAttribute) { }
public void TestAttributeWithTagNameAndAltReturnsXPathLocator() { var attribute = new ElementLocatorAttribute { TagName = "input", Alt = "test" }; var resultList = LocatorBuilder.GetElementLocators(attribute); Assert.AreEqual(1, resultList.Count); var item = resultList.First(); Assert.AreEqual(By.XPath("//input[@alt='test']"), item); }