Disable active content in WebBrowser control - wpf

I have embedded a WebBrowser control in my application and display content that I receive from a server. Specifically: The control is bound to a string which contains the error message from a rest call, that sometimes is HTML.
I wonder if there is a security risk if active content, e.g. JavaScript would be sent as part of the error message. Is there a way to instruct the WebBrowser control to disable all active content?

There are several ways to do:
First way is remove javascript from your string before pass it to browser, from Elian Ebbing's answer:
The quick 'n' dirty method would be a regex like this:
var regex = new Regex(
"(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)",
RegexOptions.Singleline | RegexOptions.IgnoreCase
);
string ouput = regex.Replace(input, "");
The better* (but possibly slower) option would be to use
HtmlAgilityPack:
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlInput);
var nodes = doc.DocumentNode.SelectNodes("//script|//style");
foreach (var node in nodes) node.ParentNode.RemoveChild(node);
string htmlOutput = doc.DocumentNode.OuterHtml;
*) For a discussion about why it's better, see this thread.
That way seem better and easier.
Second way is use WinForms webbrowser control, which allow you control lower level of browser, but this involve some invoking to WinAPI.
You can see this link for more info.

Related

WinAppDriver - How do I get the list of properties available on an element

I am using Appium with WinAppDriver to control a WinForms / WPF application.
I am looking for a programmatic way to get the list of properties available on an element that has been retrieved.
My current thinking is to ask for the className and use this to look up a static dictionary of properties I have pre-configured.
var element = driver.FindElementByXPath(xPath);
var properties = element.getProperties(); // Is there something I can call here?
This is not the greatest solution but I figured I would mention it just in case someone else might find it useful:
It is possible to get the xml of the pagesource and look at the properties there. This can be accomplished by calling driver.PageSource which will return an xml string.
Hope someone finds this useful.
You can use element.GetAttribute("Value") to get the value. You can also use other attributes like LegacyState, Value.Value, IsEnabled, IsOffscreen, ControlType etc. You can catch hold of the list of attributes in Inspect.exe (UI access)that comes with windows tools

Microsoft UI Automation for Web Application

Has anyone tried "Microsoft UI Automation" for web application?
I have a WPF application which has a embedded wpfbrowser.
Since this is basically a desktop app, I cant use Selenium Webdriver.
I tried CodedUI but i am facing a issue, Which i have asked here:
Coded UI - Unable to identify a html controls on a Wpfbrowser
I am planning to use UIAutomation, But again itseems that i am unable to identify a control using id property
Ex:
<button id="but1">Click Me</button>
For this i have:
PropertyCondition ps = new PropertyCondition(AutomationElement.AutomationIdProperty, "but1");
AutomationElement Clickme = elementMainWindow.FindFirst(TreeScope.Descendants, ps);
But this is not working. "Clickme" is null.
How to do this is UIAutomation??
EDIT: Attaching a screeshot:
I would try actually navigating the tree view down to the control you are looking for instead of doing it based on decedents. Also another thing you could try is doing a retry loop if it is null. Here is an example of a generic Retry for FlaUI. So your code would look something like this.
PropertyCondition ps = new PropertyCondition(AutomationElement.AutomationIdProperty, "but1");
Func<AutomationElement> func = () => elementMainWindow.FindFirst(TreeScope.Descendants, ps);
Predicate<AutomationElement> retry = element => element == null;
AutomationElement clickMe = Retry.While<AutomationElement>(func, retry, TimeSpan.FromSeconds(1));
So this code will retry finding the element for 1 second and will retry finding it if the element comes back null or it exceptions. If either of those happens it waits 200 milliseconds and tries again. This will tell me if the elements are just not rendered when you try to find them or if their is a difference between how inspect finds them and how System.Windows.Automation is finding them.
If this doesn't work I will post a solution using the tree walker but I suggest using this solution over the tree walker because if this was an application others would want to write automation against they would expect these functions to work the way you are attempting to use them.
Not sure if <button id="but1"> equals with automationId. You can set automation id using AutomationProperties.AutomationId="but1" if you can use that namespace in the code where you define your UI (XAML), which is probaly only for WPF applications.
In your case if your UI defined in HTML I think you can use the button's caption.
So something like this.
var ps = new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
new PropertyCondition(AutomationElement.NameProperty, "Click Me"));
AutomationElement Clickme = elementMainWindow.FindFirst(TreeScope.Descendants, ps);
ControlTypeProperty can help in filtering results by type. Not mandatory, but it can help if you have automation elements with different type, but with same name property.

is it possible to fill in a windows credential prompt programmatically?

Issues of robustness, stability and you shouldn't do this aside, has anyone ever filled in a windows credential prompt via code (so that's one that looks like this:)
Is it possible to interact with these dialog boxes through Win32 APIs, or using SendKeys/send mouse / UI Automation? Any ideas / tips anyone has would be greatly appreciated!
I ended up using the UI Automation framework, which allowed me to grab a reference to the credential prompt and then fill it out and complete it that way.
Code snippet:
AutomationElement desktop = AutomationElement.RootElement;
//get all windows on the desktop
AutomationElementCollection windows = desktop.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
foreach (AutomationElement window in windows)
{
if (window.Current.ClassName.Equals("#32770")) //security dialog
{
// access the appropriate AutomationElements to enter credentials here
}
}
To interact with an element, you grab the appropriate Pattern object and call its methods (eg Textboxes have a ValuePattern which has a .SetValue() method.
I also used UISpy to find all the values for things like ClassNames, AutomationIds, etc to help find the correct item through .FindAll() and PropertyConditions objects.
Use something like AHK (Auto HotKey) it is a simple language that can be compiled to an EXE and is designed for automating the keyboard and mouse.
Or you could do it from WPF:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6fc7f1f6-f3e2-4b32-9d2b-9c7a2680e04a/
Or users could simply tick "Remember my credentials"

How to output HTML in Silverlight 4 to a string (like HtmlTextWriter)

I have a requirement to generate an HTML snapshot of an object - basically on my app you can click on the Clipboard button and generate either Text, BB Bode or HTML and it goes through the object and builds a string of text which is copied to the Clipboard. You can then paste this snippet on forums, blogs, websites (ie. a way of sharing).
What is the most efficient way of writing HTML output? In ASP.NET I would use HtmlTextWriter but I can't use the System.Web assembly in Silverlight. I could write the tags manually but I was hoping there was a better way.
Note: This is nothing to do with the current HTML page or displaying HTML in Silverlight or Silverlight in HTML. The requirements are valid ;)
The closest thing to HTmlTextWriter thats actually available in Silverlight is XmlWriter.
StringBuilder sb = new StringBuilder();
XmlWriter writer = new XmlWriter(sb);
// use writer to create html content.
string html = sb.ToString();
Not as slick as using HtmlTextWriter but better than using StringBuilder directly. Just watch out for those elements that need a closing tag such as <div></div>.

Dynamic Hyperlink in Livecycle Form

I am trying to figure out how to make a hyperlink in a Livecycle Form which points to a URL which will change on different days that the form is rendered. For example on one day I might want the hyperlink to point to:
mywebsite/mypage?option=XXX
and on another day I want it to point to:
mywebsite/mypage?option=YYY
The XXX and YYY can be passed into the form's data pretty easily as XML, but I just don't know how to make it so that the hyperlink is changed to correspond to this.
Any suggestions?
This can be accomplished with JavaScript in LiveCycle Designer. The following script, placed on the Form's docReady event will let you dynamically change the URL of a text object.
form1::docReady - (JavaScript, client)
// If this code is running on the server, you don't want it to run any code
// that might force a relayout, or you could get stuck in an infinite loop
if (xfa.host.name != "XFAPresentationAgent") {
// You would load the URL that you want into this variable, based on
// whatever XML data is being passed into your form
var sURL = "www.stackoverflow.com"; // mywebsite/mypage?option=xxx
// URLs are encoded in XHTML. In order to change the URL, you need
// to create the right XHTML string and push it into the Text object's
// <value> node. This is a super simple XHTML shell for this purpose.
// You could add all sorts of markup to make your hyperlink look pretty
var sRichText = "<body><p>Foo</p></body>";
// Assuming you have a text object called "Text1" on the form, this
// call will push the rich text into the node. Note that this call
// will force a re-layout of the form
this.resolveNode("Text1").value.exData.loadXML(sRichText, false, true);
}
There are a couple of caveats: URLs in Acrobat are only supported in Acrobat 9.0 and later. So if someone using an older version of Acrobat opens your form, the URLs won't work.
Also, as you can see from the "if (xfa.host.name !=...)" line, this code won't run properly if the form is being generated on the server, because forcing a re-layout of a form during docReady can cause problems on certain older versions of the LiveCycle server. If you do need to run this script on the server, you should probably pick a different event then form::docReady.
I a number of complaints from users in WorkSpace that clicking links opened them in the same tab so they lost their WorkSpace form, and there's no option to change that in Designer 11. I think the solution I came up with for that would work for you too.
I made buttons with no border and no background, and in their click event have this line (in Javascript, run at client)
app.launchURL("http:/stackoverflow.com/", true);
It would be easy to add some logic to choose the right URL based on the day and it doesn't cause any form re-rendering.
In some spots where the hyperlink is in line with other text, I leave the text of the link blue and underlined but with no hyperlink, and just place the button (no background, no border, no caption) over it. Does require positioned and not flowed subforms for that to work, so depending on your layout it could get a little clunky.
Wow, just realized I am super late to the party. Well, for anyone using ES4 facing a similar problem . . .
Ended up using a 3rd party component to manipulate the PDF's hyperlinks...wish there was a better solution as this one costs about $1000.

Resources