public void SetGetValuesFromDOM() { // HtmlControl/Elements now have a generic SetValue/GetValue that allows developers // to set/get values directly from the DOM for any property on the control. Whether it is inherited, // readonly or an emitted attribute. // Get whether a checkbox is enabled or disabled. HtmlInputCheckBox cks = Find.ById <HtmlInputCheckBox>("checkbox1"); bool disabled = cks.GetValue <bool>("disabled"); // Disable it cks.SetValue <string>("disabled", "true"); Assert.IsTrue(cks.GetValue <bool>("disabled")); // Is it visible. IsVisible follows the CSS chain to determine if the // element is actually visible or not. An element is visible when // the CSS the display style is not 'none' and the visibility style // is not 'hidden'. Assert.IsTrue(cks.IsVisible()); // Get the color style HtmlSpan mySpan = Find.ById <HtmlSpan>("Warning"); HtmlStyle styleColor = mySpan.GetStyle("color"); string strColor = mySpan.GetStyleValue("color"); // Getting the computed style will follow the CSS chain and return the // style. HtmlStyle styleMargin = mySpan.GetComputedStyle("margin"); string strMargin = mySpan.GetComputedStyleValue("margin"); }
public void HtmlStyleClass() { // Verify the color of the warning text is Red HtmlSpan warningSpan = Find.ById <HtmlSpan>("Warning"); HtmlStyle warningColorStyle = warningSpan.GetStyle("color"); // Verify the style's value is a color value. // In reality it's not necessary to test using both IsColor and IsInt. // They're both used here for demonstration purposes only. Assert.IsTrue(warningColorStyle.IsColor()); Assert.IsFalse(warningColorStyle.IsInt()); Color warningColor = warningColorStyle.ToColor(); Assert.AreEqual <Color>(Color.Red, warningColor); // If we don't want to use Assert, we can use IsSameColor instead. if (!HtmlStyle.IsSameColor(Color.Red, warningColor)) { Log.WriteLine(LogType.Error, string.Format("Warning color is not red. It is {0}", warningColor.ToString())); } // We have the option of converting a .NET Color object into an HTML color string string htmlColorString = HtmlStyle.ToHtmlColor(Color.SaddleBrown); htmlColorString = HtmlStyle.ToHtmlColor(Color.FromArgb(33, 44, 55)); // Let's log the name and value of all the styles attached to this element foreach (string strStyle in warningSpan.Styles) { // NOTE: Some of the styles have a '-' in the middle of the name in the HTML (e.g. TEXT-ALIGN). // But when we go to fetch the styles attributes by that name, it doesn't exist in JavaScript. // The equivalent does exist without the '-'. So we will blindly strip out any '-' characters // prior to calling GetStyle in order to convert it to be JavaScript compatible. string modStyle = strStyle.Replace("-", ""); HtmlStyle aStyle = warningSpan.GetStyle(modStyle); Log.WriteLine(LogType.Information, string.Format("Style name: {0}, Style Value {1}", aStyle.Name, aStyle.Value)); } // Verify the margin is set to 30 units (but note we don't know what the unit of measure is) HtmlStyle warningMarginStyle = warningSpan.GetStyle("margin"); Assert.IsTrue(warningMarginStyle.IsInt()); int warningMargin = warningMarginStyle.ToInt(); // Unfortunately the value we get from Firefox is different than all other browsers. if (ActiveBrowser.BrowserType == BrowserType.FireFox) { Assert.AreEqual(30303030, warningMargin); } else { Assert.AreEqual(30, warningMargin); } // GetStyle returns the value of an explicit style applied to the element. // If the element does not have an explicit style applied, GetStyle returns an empty value. // Since our warning span does not have the backgroundColor style explicitly applied to it, // GetStyle returns a style with an empty value. HtmlStyle backgroundStyle = warningSpan.GetStyle("backgroundColor"); Assert.IsTrue(string.IsNullOrEmpty(backgroundStyle.Value), "Actual padding value was: \"{0}\"", backgroundStyle.Value); // However GetComputedStyle will follow the CSS until it finds the currently active style value. // Our warning span is contained within a form that has a backgroundColor style. // Therefore GetComputedStyle on the warning span will return the value set in the parent form tag. backgroundStyle = warningSpan.GetComputedStyle("backgroundColor"); Assert.IsFalse(string.IsNullOrEmpty(backgroundStyle.Value), "Actual padding value was: \"{0}\"", backgroundStyle.Value); }