/// <summary> /// /// </summary> /// <param name="src">image source / url</param> /// <param name="alt">alternate text for image</param> public void InsertImage(string src, string alt) { try { using (new UndoUnit(Document, "Insert Image")) { // obtain a reference to the body node HtmlDomNode bodyNode = (HtmlDomNode)Document.body; HtmlImageElement img = (HtmlImageElement)Document.createElement("IMG"); img.src = src; img.alt = alt; // table processing all complete so insert into the DOM HtmlSelection selection = Document.selection; HtmlTextRange textRange = SelectionHelper.GetTextRange(Document); if (textRange != null) { textRange.pasteHTML(((HtmlElement)img).outerHTML);//"<img src=\"" + src + "\" alt=\"" + alt + "\">"); } else { HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // overwrite any controls the user has selected try { // clear the selection and insert the table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } controlRange.item(0).outerHTML = ((HtmlElement)img).outerHTML; // this should work with initial count set to zero // controlRange.add((HtmlControlElement)table); } catch (Exception ex) { throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "InsertImage", ex); } } else { // insert the table at the end of the HTML HtmlDomNode imgNode = (HtmlDomNode)img; bodyNode.appendChild(imgNode); } } } } catch (Exception ex) { throw new HtmlEditorException("Unable to insert image.", "InsertImage", ex); } }
private void btnGetImg_Click(object sender, EventArgs e) { lstImg.Items.Clear(); mshtml.HTMLDocument doc = explorer.Document as mshtml.HTMLDocument; var imgs = doc.getElementsByTagName("img"); foreach (var img in imgs) { mshtml.IHTMLImgElement imgElement = img as mshtml.IHTMLImgElement; if (imgElement != null && !lstImg.Items.Contains(imgElement.src)) { lstImg.Items.Add(imgElement.src); } } }
public void save_images(HtmlElement elem, string addr_dir = "") { mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser.Document.DomDocument; mshtml.IHTMLControlRange imgRange = (mshtml.IHTMLControlRange)((mshtml.HTMLBody)doc.body).createControlRange(); mshtml.IHTMLImgElement img = (mshtml.IHTMLImgElement)elem.DomElement; imgRange.add((mshtml.IHTMLControlElement)img); imgRange.execCommand("Copy", false, null); using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap)) { if (bmp != null) { if (addr_dir == "") { bmp.Save(@"C:\" + "aaaa"); } else { bmp.Save(addr_dir + img.nameProp); } } } }
void AddTestStep(HtmlElement element, String elementXPosition, String elementYPosition) { stepsList.SelectionBullet = true; WebBrowser browser = new WebBrowser(); if (tabControl2.SelectedIndex == 0) { browser = webBrowser1; } else { browser = webBrowser2; } //on, in, to String intent = "on"; //value of input field var inputValue = ""; // value of select list var selectValue = ""; // Check what type of element has been selected var elementType = ""; switch (element.TagName.ToLower()) { case "input": elementType = "input field"; intent = "in"; try { mshtml.IHTMLInputElement input = element.DomElement as mshtml.IHTMLInputElement; inputValue = input.value; } catch { } break; case "select": elementType = "select list"; intent = "on"; try { mshtml.IHTMLSelectElement select = element.DomElement as mshtml.IHTMLSelectElement; mshtml.HTMLOptionElement option = (mshtml.HTMLOptionElement)select.item(select.selectedIndex, null); if (select.selectedIndex != 0) { selectValue = option.text; } } catch { } break; case "a": elementType = "link"; intent = "on"; break; case "button": intent = "on"; elementType = "Button"; break; case "img": intent = "on"; elementType = "Slide"; break; } // Check if the selected element has an associated label we can use var elementLabel = ""; var labels = browser.Document.GetElementsByTagName("label"); foreach (HtmlElement lbl in labels) { //check if element has an ID so we can obtain a label for it if (element.Id != null) { //check if the current label for attribute matches one for our element //@'for=\"name"' String forLabel = lbl.OuterHtml.Replace("\"", ""); if (forLabel.Contains("for=" + element.Id)) { elementLabel = lbl.OuterText; } } } //get image alt text // vScreen products use images for slides!! if (elementType == "Slide") { mshtml.IHTMLImgElement img = element.DomElement as mshtml.IHTMLImgElement; //GenerateAndAddExpectedTextStep(elementType, img.alt, elementYPosition, elementXPosition); //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; stepsList.AppendText(string.Format("Click {0} '{1}' {2}{4}", intent, img.alt, elementType, user.ToUpper(), Environment.NewLine)); } // get text for link if (elementType == "link") { elementLabel = element.InnerText; intent = "on"; } // check if we're dealing with a an input button/submit/reset if (elementType == "input field" && elementLabel == "") { try { mshtml.IHTMLInputButtonElement button = element.DomElement as mshtml.IHTMLInputButtonElement; elementType = "button"; intent = "on"; elementLabel = button.value; if (elementLabel == null) { elementLabel = button.name; } } catch (Exception ex) { intent = "in"; elementType = "input"; } } if (element.TagName == "BUTTON") { mshtml.IHTMLElement button = element.DomElement as mshtml.IHTMLElement; //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; stepsList.AppendText(string.Format("Click {0} '{1}' {2}{4}", intent, button.innerHTML, elementType, user.ToUpper(), Environment.NewLine)); } //check type of input if (element.TagName == "SELECT") { if (!string.IsNullOrWhiteSpace(selectValue)) { //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; stepsList.AppendText(string.Format("Select '{0}'{2}", selectValue, user.ToUpper(), Environment.NewLine)); } else { //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; if (RecordElementPosition == true) { // we found the label associated to the selected input and it doesn't have a value input stepsList.AppendText(string.Format("Click {0} '{1}' {2} toward {4} {5} of page{6}", intent, elementLabel, elementType, user.ToUpper(), elementYPosition, elementXPosition, Environment.NewLine)); } else { // we found the label associated to the selected input and it doesn't have a value input stepsList.AppendText(string.Format("Click {0} '{1}' {2}{4}", intent, elementLabel, elementType, user.ToUpper(), Environment.NewLine)); } } } else { //check if input has a value if (string.IsNullOrWhiteSpace(inputValue)) { if (!string.IsNullOrWhiteSpace(elementLabel) && !string.IsNullOrWhiteSpace(elementType)) { //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; if (RecordElementPosition == true) { // we found the label associated to the selected input and it doesn't have a value input stepsList.AppendText(string.Format("Click {0} '{1}' {2} toward {4} {5} of page{6}", intent, elementLabel, elementType, user.ToUpper(), elementYPosition, elementXPosition, Environment.NewLine)); //stepsList.AppendText(string.Format("**{3}**{6}Click {0} '{1}' {2} toward {4} {5} of page{6}", intent, elementLabel, elementType, user.ToUpper(), elementYPosition, elementXPosition, Environment.NewLine)); } else { // we found the label associated to the selected input and it doesn't have a value input stepsList.AppendText(string.Format("Click {0} '{1}' {2}{4}", intent, elementLabel, elementType, user.ToUpper(), Environment.NewLine)); //stepsList.AppendText(string.Format("**{3}**{4}Click {0} '{1}' {2}{4}", intent, elementLabel, elementType, user.ToUpper(), Environment.NewLine)); } } } else { if (elementType == "button") { //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; if (RecordElementPosition == true) { // we found the label associated to the selected input and it doesn't have a value input stepsList.AppendText(string.Format("Click {0} '{1}' {2} toward {4} {5} of page{6}", intent, elementLabel, elementType, user.ToUpper(), elementYPosition, elementXPosition, Environment.NewLine)); } else { // we found the label associated to the selected input and it doesn't have a value input stepsList.AppendText(string.Format("Click {0} '{1}' {2}{4}", intent, elementLabel, elementType, user.ToUpper(), Environment.NewLine)); } } else { //BUG: Currently outputs i.e. **USER** even if we're not recording a valid action if (outputUser == true) { stepsList.SelectionBullet = false; //record which user is recording an action stepsList.AppendText("**" + user.ToUpper() + "**" + Environment.NewLine); //we only want to record the user when recording a new list of steps before an expected result outputUser = false; } stepsList.SelectionBullet = true; // we found the label associated to the selected input stepsList.AppendText(string.Format("Input '{0}'{2}", inputValue, user.ToUpper(), Environment.NewLine)); } } } stepsList.SelectionBullet = false; ScrollToRecordedStepsEnd(); }
void AddExpectedResult(HtmlElement element, String elementXPosition, String elementYPosition) { // whether or not to output the actor in the test steps script outputUser = true; //ensure bullet list for new line is off for expected result stepsList.SelectionBullet = false; //check which browser window is recording the result WebBrowser browser = new WebBrowser(); if (tabControl2.SelectedIndex == 0) { browser = webBrowser1; } else { browser = webBrowser2; } var elementType = ""; switch (element.TagName.ToLower()) { case "a": elementType = "Link"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "h1": elementType = "page heading"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "h2": elementType = "page heading"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "h3": elementType = "page heading"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "h4": elementType = "page heading"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "h5": elementType = "page heading"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "h6": elementType = "page heading"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "p": elementType = "page text"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "span": elementType = "page"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "label": elementType = "Label"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "div": elementType = "page text"; GenerateAndAddExpectedTextStep(elementType, element.InnerText, elementYPosition, elementXPosition); break; case "img": elementType = "Image"; mshtml.IHTMLImgElement img = element.DomElement as mshtml.IHTMLImgElement; GenerateAndAddExpectedTextStep(elementType, img.alt, elementYPosition, elementXPosition); break; case "input": //check if regular input or a button! try { mshtml.IHTMLInputElement input = element.DomElement as mshtml.IHTMLInputElement; if (input.type.ToLower() == "text") { elementType = "Input"; // Check if the selected element has an associated label we can use var inputLabel = ""; var inputLabels = browser.Document.GetElementsByTagName("label"); foreach (HtmlElement lbl in inputLabels) { //check if element has an ID so we can obtain a label for it if (element.Id != null) { //check if the current label for attribute matches one for our element String forLabel = lbl.OuterHtml.Replace("\"", ""); if (forLabel.Contains("for=" + element.Id)) { inputLabel = lbl.OuterText; } } } string inputValue = ""; if (input.value == null) { inputValue = input.name; } else { inputValue = input.value; } if (RecordElementPosition == true) { stepsList.AppendText(string.Format("{5}Expected Result{5}'{6} {0}' {1} toward {3} {4} of page contains text '{2}'{5}{5}", inputLabel, elementType, inputValue, elementYPosition, elementXPosition, Environment.NewLine, user)); } else { stepsList.AppendText(string.Format("{3}Expected Result{3}{4} '{0}' {1} contains text '{2}'{3}{3}", inputLabel, elementType, inputValue, Environment.NewLine, user)); } } else { elementType = "Button"; mshtml.IHTMLInputButtonElement inputButton = element.DomElement as mshtml.IHTMLInputButtonElement; //need to clean up - check if field is a password field! if (input.type.ToLower() == "password") { elementType = "Input"; } // Check if the selected element has an associated label we can use var inputLabel = ""; var inputLabels = browser.Document.GetElementsByTagName("label"); foreach (HtmlElement lbl in inputLabels) { //check if element has an ID so we can obtain a label for it if (element.Id != null) { //check if the current label for attribute matches one for our element String forLabel = lbl.OuterHtml.Replace("\"", ""); if (forLabel.Contains("for=" + element.Id)) { inputLabel = lbl.OuterText; } } } string inputValue = ""; if (input.value == null) { inputValue = input.name; } else { inputValue = input.value; } if (RecordElementPosition == true) { stepsList.AppendText(string.Format("{4}Expected Result{4}{5} {0} toward {2} {3} of page contains text '{1}'{4}{4}", elementType, inputValue, elementYPosition, elementXPosition, Environment.NewLine, user.ToUpper())); } else { // stepsList.AppendText(string.Format("Expected Result{2}{3} {0} contains text '{1}'{2}{2}", elementType, inputValue, Environment.NewLine, user)); if (inputLabel == "") { inputLabel = input.name; } stepsList.AppendText(string.Format("{3}Expected Result{3}{4} '{0}' {1} contains text '{2}'{3}{3}", inputLabel, elementType, inputValue, Environment.NewLine, user)); } } } catch { } elementType = "Input"; break; case "button": elementType = "Button"; mshtml.IHTMLElement button = element.DomElement as mshtml.IHTMLElement; if (RecordElementPosition == true) { stepsList.AppendText(string.Format("{4}Expected Result{4}{5} {0} toward {2} {3} of page contains text '{1}'{4}{4}", elementType, button.innerHTML, elementYPosition, elementXPosition, Environment.NewLine, user)); } else { stepsList.AppendText(string.Format("{2}Expected Result{2}{3} {0} contains text '{1}'{2}{2}", elementType, button.innerHTML, Environment.NewLine, user)); } break; case "select": elementType = "Select list"; mshtml.IHTMLSelectElement select = element.DomElement as mshtml.IHTMLSelectElement; mshtml.HTMLOptionElement option = (mshtml.HTMLOptionElement)select.item(select.selectedIndex, null); // use select list associated label name // Check if the selected element has an associated label we can use var elementLabel = ""; var labels = browser.Document.GetElementsByTagName("label"); foreach (HtmlElement lbl in labels) { //check if element has an ID so we can obtain a label for it if (element.Id != null) { //check if the current label for attribute matches one for our element String forLabel = lbl.OuterHtml.Replace("\"", ""); if (forLabel.Contains("for=" + element.Id)) { elementLabel = lbl.OuterText; } } } var selectValue = ""; if (select.selectedIndex != 0) { selectValue = option.text; } if (selectValue != "") { if (RecordElementPosition == true) { stepsList.AppendText(string.Format("{5}Expected Result{5}{6} '{0}' {1} toward {3} {4} of page contains option '{2}'{5}{5}", elementLabel, elementType, selectValue, elementYPosition, elementXPosition, Environment.NewLine, user)); stepsList.AppendText("\n"); } else { stepsList.AppendText(string.Format("{3}Expected Result{3}{4} '{0}' {1} contains option '{2}'{3}{3}", elementLabel, elementType, selectValue, Environment.NewLine, user)); stepsList.AppendText("\n"); } } break; } ScrollToRecordedStepsEnd(); }