Watin ContainsText method fails to find text in FireFox - watin

The ContainsText method finds the text only in specific area in the html but fails to find id in other parts of the page.
The text that located under 'div id="content"' can be found
But the text in other area of the html is not found (f.e 'form id="aspnetForm"')
Browser b = new FireFox("http://localhost:8668/login.aspx");
b.Button("login.login.button")).Click();
bool blah = b.ContainsText("Hello");
I'm using the latest watin release.
The issue is reproduced with FF3.0, FF3.5 and FF3.6
In IE it's working fine for the tested text.

Used workaround:
By parsing the html with html agility pack
Looks like this:
public bool ContainsTextInternal(string text)
{
var htmldoc = new HtmlDocument();
htmldoc.LoadHtml(browser.Html);
return htmldoc.DocumentNode.InnerText.Contains(text);
}
html agility pack link

Related

svg images are not inserted in pdf while generating a pdf from html using itex7 html2pdf plugin

I am generating a pdf from multiple htmls. if any of the html consists img tag which refers to svg file like <img src="assets/6.svg" /> then in generated pdf doesnt have image included. If I keep jpg or png, then its included as part of generated pdf. Its happening only if I use below code but if I use HtmlConverter.convertToPdf() then svg files are showing fine in generated pdf.
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new File(pdfFileName)));
pdfDocument.setDefaultPageSize(PageSize.A4);
Document document = new Document(pdfDocument);
document.setMargins(40, 40, 40, 40);
for (String html : htmlFileList) {
List<IElement> elements = HtmlConverter.convertToElements(new FileInputStream(html), converterProperties);
System.out.println(elements.size());
for (IElement element : elements) {
System.out.println(element.getClass().getName());
document.add((IBlockElement)element);
}
}
document.close();
I am seeing error like below when generating a pdf from html using above code.
Unable to retrieve image with given base URI
(file:/C:/Projects/pdf_conversion/download/) and
image source path
(file:/C:/Projects/pdf_conversion/download/assets/cpc/img/icons/x_dont_gray.svg)
I am using above code because page content should be flown from page to page (dont want to create one pdf for one html). Any workaround is highly appreciated on this.

How to make a browser display any tag in XML as-is without using <xmp> or <pre>

I am trying to display an Ajax returned xml file in String format in IE9.
Since it is in String, IE takes all xml tags as un-recognizable html tags and tosses them away. Only text data are display and it is really a mess. I can use <xmp> or <pre> but then when I save the xml by the file->save as..., the <xmp> <pre>is there. I do have the option to select all from the web page and copy paste to some text editor but in my case it is preferrable to use save as.
So I tried to escape all those < and > with entity reference in Java with StringEscapeUtils.escapeXml(). But still the problem persists. Do I have it right in Java? And is there any simple way to display xml input stream (not a file) as-is in a brwoser?
Here is the Java code for Ajax service.
public String retrieveXML() {
String unescapedXML = getXMLByRestfulService();
formatXMLMsg(unescapedXML);
String finalXML = StringEscapeUtils.escapeXml(unescapedXML);
return SUCCESS;
}
Here is the Ajax client code. Some parameters can be safely ignored.
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();a
} else if ( window.ActiveXObject ){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if ( req ) {
req.onreadystatechange=function(){
if ( req.readyState == READY_STATE_COMPLETE ){
var myXML = req.responseText;
var myWin = window.open("", "aWindow", "width=800, height=500");
myWin.document.write("<xmp>"+myXML+"</xmp>");
myWin.focus();
}
};
req.open("POST", url, true );
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send (params);
}
I'm a little unclear on your question in regards to "as is" in browser. But, I'll take a shot.
You may need to enable the XML DOM plugin.
Click Tools(gear icon)->Manage Add-ons; this will pop-up a dialog
select "All add-ons". I don't have IE9 loaded, but you need to (may look like below):
Find "XML DOM Document" add-on and make sure its enabled
Close all IE windows and retest

Showing Base64String as Image works in IE but not in Chrome

C# code:
public ActionResult GetDoucmentImage(long onBaseDocumentId)
{
onBaseDocumentId = 1111;
OnBaseDataAccess db = new OnBaseDataAccess();
OnBaseDocument document = db.GetDocument(onBaseDocumentId);
return View("GetDoucmentImage", document);
}
View Code:
#{
var base64 = Convert.ToBase64String(Model.Data);
var imgSrc = String.Format("data:image/png;base64,{0}", base64);}
<img src="#imgSrc" style="width:200px; height:400px;"/>
Trying to show an image in browser, it works in IE but not in chrome
using chromium, select your image element and right click and select the "Inspect Element" from the context menu....
the chromium Developer tool will open and highlight the client source of the image element....
compare the src value with that shown in the IE Developer tool (f12>Find tab)....
I think MSIE and Gecko browsers sanitise base64 encodings (toStaticHTML), to prevent malicious script injections...
Maybe your Model.Data is not actually a png image.

Clickable Url in Twitter

i have implemented a simple twitter reader in my app. I am able to get the tweets of a
user. But, if there is a url in this tweet, i cant click on it, as its not detected as an URL.
Is there a possibility to implement this function, so that urls in the tweet are displayed
as clickable url, and then launch for example a webbrowser?
Thank you very much
I assume you are using a TextBlock to show the tweet text, correct? If so, change it to a RichTextBox and all you need to do is use Run for text and Hyperlink for the links!
Also, make sure you set the IsReadOnly property of the RichTextBox to true in order for it to work properly!
Next, parse the tweet text with a regular expression to find links, and use the Hiperlink class to create a clickable link on it, and Run on the remaining text!
Here's a sample function that will parse a tweet and build the content for a RichTextBox:
private Block ParseTweet(string tweetText)
{
var paragraph = new Paragraph();
var lastIndex = 0;
foreach (Match m in Regex.Matches(tweetText, #"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&=]\S*)?"))
{
if (m.Index > 0)
paragraph.Inlines.Add(tweetText.Substring(lastIndex, m.Index));
var hyperlink = new Hyperlink()
{
NavigateUri = new System.Uri(m.Value, System.UriKind.RelativeOrAbsolute),
TargetName = "_blank"
};
hyperlink.Inlines.Add(m.Value);
paragraph.Inlines.Add(hyperlink);
lastIndex = m.Index + m.Length;
}
if (lastIndex < tweetText.Length)
paragraph.Inlines.Add(tweetText.Substring(lastIndex));
return paragraph;
}
You should call this function like so:
var tweetText = #"Testing: http://twitter.com -> link for twitter";
MyRichTextBox.Blocks.Add(ParseTweet(tweetText));
I think it's not possible but but you can parse your text to find URL (with regex) and display a hyperlink below the text.
1) you search for URLs in the text with a regex
2) if a URL is found, you create a HyperlinkButton with this URL

Images in MHT files from MS Word do not display in email

When I email myself sample mhtml files (e.g. from here) images display fine in Outlook. However, when I convert a Word document to mht (Web Archive) format, the images do not display. If I open the file in a browser, the images display fine, or if I attach the mht file and double click on the attachment. But if the file is inlined in the email, then I get the red X box with 'Right click here to download pictures', and if I select download pictures, then 'file can not be displayed...may have moved...'.
Any ideas why images in Word docs converted to MHTML do not like to display inline in emails?
An MHTML document is a multi-part MIME document. The first part of the document is HTML and has links to the images in the other parts. The problem is that the links don't work in an inline email even though they do work in a browser. Looking at some examples, you can see the links must be prefixed by "cid:", and the part after the "cid:" must have a Content-ID in the header of the corresponding MIME part.
The link can be as simple as "cid:image002.gif" with the Content-ID in the corresponding MIME part being:
Content-ID: <image002.gif>
If all of the links are fixed in this way, the html with the images will display inline in Outlook.
As mentioned above, you use the Content ID to link attachments to image tags within the HTML body of your email. Below is a full program for opening an MHT file, adjusting the links, and emailing the results.
I have a client that is using the Word Automation Service to convert incoming emails to MHT files and emailing them. The issue is that Outlook didn't care much for the raw MHT and didn't inline the images. Here is my POC for a solution. I utilized the MimeKit and MailKit (http://www.mimekit.net/) in the code, the Bouncy Castle C# API (http://www.bouncycastle.org/csharp/) to cover a dependency within the MailKit, and Antix SMTP Server for Developers (http://antix.co.uk/Projects/SMTP-Server-For-Developers) running on the local server to receive the SMTP traffic for testing the code in dev. Below is the POC code that opens an existing MHT file and emails it with embedded images.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using MimeKit;
using MailKit;
using MimeKit.Utils;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
MimeMessage messageMimeKit = MimeMessage.Load(#"c:\test.mht");
var images = messageMimeKit.BodyParts.Where(x => x.ContentLocation.LocalPath.EndsWith("png"));
var bodyString = messageMimeKit.HtmlBody;
var builder = new BodyBuilder();
foreach (var item in images)
{
item.ContentId = MimeUtils.GenerateMessageId();
bodyString = bodyString.Replace(GetImageName(item), "cid:" + item.ContentId.ToString());
builder.LinkedResources.Add(item);
}
builder.HtmlBody = bodyString;
messageMimeKit.Body = builder.ToMessageBody();
messageMimeKit.From.Add(new MailboxAddress("from address", "NoReply_SharePoint2013Dev#smithmier.com"));
messageMimeKit.To.Add(new MailboxAddress("to address", "larry#smithmier.com"));
messageMimeKit.Subject = "Another subject line";
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect("localhost");
client.Send(messageMimeKit);
client.Disconnect(true);
}
}
private static string GetImageName(MimeEntity item)
{
return item.ContentLocation.Segments[item.ContentLocation.Segments.Count() - 2] +
item.ContentLocation.Segments[item.ContentLocation.Segments.Count() - 1];
}
}
}

Resources