Locating heading text using CSS - c#

I have some C# code which will verify the heading text on a web page, currently located via xpath as follows.
Assert.AreEqual("Permissions", driver.FindElement(By.XPath(".//*[#id='navigation']/li[6]/h3")).Text);
This, as I understand, will check the text found at the end of the XPath matches the word "Permissions".
The above currently works but I would rather use CSS locators. I hear its best not to use XPath if possible.
I'm new to website testing so am not yet familiar with all this, any help will be much appreciated.
Let me know if there is more you require than what is provided above or if you have any alternate suggestions to the method already used.

I may not fully understand what you are trying to do but since this has no answer after 7 hours i though i might at least mention that if you are trying to get the innerhtml of the header you can use Agile Html http://htmlagilitypack.codeplex.com/ its very easy to use. Might not be what you are looking for though.

Related

How to reduce the Recaptcha difficulty?

I use RecaptchaControl, and the users complain that the image is not that clear (the black part in the the captcha is too hard to read). Is there any property to make the image less difficult (less noisy)?
<recaptcha:RecaptchaControl ID="recaptcha" runat="server" PublicKey="XXX" PrivateKey="YYY" OverrideSecureMode="true" />
To put it simply, no.
As others have said you can only customise the UI.
I'm the author of the article about the recaptcha API tutorial #web-development-blog.com and must say it's often very hard to read the text created images by recaptcha. While using the API I got sometimes better results while using a different language for the challenges.
In one of my projects I got a lot of complains and switched to a different script:
http://code.google.com/p/cool-php-captcha/
The challenges are less hard to solve and the script is very easy to use.
Possibly not what you are looking for but there are lots of custom captcha controls out there i.e.
CaptchaNET_2.aspx
With a bit of knowledge of the Graphics classes in C# it isn't a hard job to tweak the code to make the captcha simplier (or harder). We use one for a mobile phone web site and control is important otherwise it is unreadable in lower res phones
In some reCaptchas (I'm guessing this is somehow configurable), you only have to get one word right (I'm not sure how close the second word must be).
I've tested in the reCaptcha in my own website and it worked (two words wrong = fail, one word right + one wrong = success); tested in stackoverflow's reCaptcha and it didn't... I had to get both words right. That's why I think it's configurable.
I found this post along with the one below (where I read that only one word had to be right) exactly because I too am finding it too hard to get through reCaptcha's; too often having to refresh more than 5 times to get a readable pair. Other post:
Are reCAPTCHA CAPTCHAs getting harder or is just me
Google now allows you to change the difficulty setting.

Reasons and benefits for using a literal in asp over html

I am fairly new to asp.net so this may seem like a stupid question, but:
I am attempting to modify a pre-exsiting help menu in a web-app.
In this menu the previous developer, just used three literals seperated by paragraphs and a button linking to a 100 page manual. (i know doesn't seem to be much help, hence why i was planning to update and fix this)
What i had in mind was setting something up like a menu that would link to the different section of the page when the title is clicked.
So something along these lines:
<h1>Menu Contents</h1>
<ol>
<li>section1</li>
<li>section2</li>
<li>etc</li>
</ol>
After the contents section i would have the first section with it's details.
What i am wondering is why would i use asp-literals over just sticking to html?
So basically i would like to know what are the benefits or reasons for using asp literals and i guess when is it that i should use them?
I've attempted to find the solution to this on my own, but the only related matters i have been able to find have to deal with people arguing whether literals or labels are the best option so it's not entirely helpful.
Any help or suggestions are greatly appreciated.
Thanks.
You generally use literals if you want to be able to control the text you display programmatically. Say your page has a way to edit the section titles of your manual, for instance. Or say your manual is in multiple languages and you want the user to be able to set her language and see the section titles displayed in that language.
In your case, by what you're saying, I infer none of this applies and your manual is quite static. In that case I would stick to simple HTML.
Maybe it's been done that way so it would allow the links to be built dynamically, something like:
litMenuItem1.Text = "section1";
thus giving full control over the output? There are better ways to do that however.

Sharepoint basics and any useful resources?

Sorry about having 3 questions in one but they are closely related and should be simple for someone familiar.
I'm used to coding Java/Obj C/PHP and am finding trying to modify a template somewhat annoying partly because it doesn't make sense but mostly I can't find resources.
I am editing a master template and have gotten the basics but some things still allude me.
With the ContentPlaceHolders, is there a way to use it more than once? Someone posted some code about how you can do this but it said I couldn't use code here.
Is there a way to modify these ContentPlaceHolders without using Sharepoint designer?
I thought using the SharePoint: tag would be a possible way around this, but I can't find any docos on the possible tags and what they do and where to use them.
Thanks for any help.
A ContentPlaceHolder in a master page only maps to one Content section in a page/page layout.
Yes, you can modify the contents of ContentPlaceHolder using code, building a control tree.
The SharePoint tag prefix is used to signify a different control namespace, but there is no sharepoint:placeholder
Maybe if you explain what you are trying to do, we might be able to advise you the best way.

Self learning regular expression or xpath query?

Is it possible to write code which generates a regular expression or XPath that parses links based on some HTML document?
What I want is to parse a page for some links. The only thing I know is that the majority of the links on the page is those links.
For a simple example, take a Google search engine results page, for example this. The majority of the links is from the search results and looks something like this:
<h3 class="r"><a onmousedown="return rwt(this,'','','res','1','AFQjCNERidL9Hb6OvGW93_Y6MRj3aTdMVA','')" class="l" href="http://stackoverflow.com/"><em>Stack Overflow</em></a></h3>
Is it possible to write code that learns this and recognizes this and is able to parse all links, even if Google changes their presentation?
I'm thinking of parsing out all links, and looking X chars before and after each tag and then work from that.
I understand that this also could be done with XPath, but the question is still the same. Can I parse this content and generate a valid XPath to find the serp links?
As I understand them, most machine learning algorithms work best when they have many examples from which they generalize an 'intelligent' behavior. In this case, you don't have many examples. Google isn't likely to change their format often. Even if it feels often to us, it's probably not enough for a machine learning algorithm.
It may be easier to monitor the current format and if it changes, change your code. If you make the expected format a configurable regular expression, you can re-deploy the new format without rebuilding the rest of your project.
If I understand your question, there's really no need to write a learning algorithm. Regular expressions are powerful enough to pick this up. You can get all the links in an HTML page with the following regular expression:
(?<=href=")[^"]+(?=")
Verified in Regex Hero, this regular expression uses a positive lookbehind and a positive lookahead to grab the url inside of href="".
If you want to take it a step further you can also look for the anchor tag to ensure you're getting an actual anchor link and not a reference to a css file or something. You can do that like this:
(?<=<a[^<]+href=")[^"]+(?=")
This should work fine as long as the page follows the href="" convention for the links. If they're using onclick events then everything becomes more complicated as you're going to be dealing with the unpredictability of Javascript. Even Google doesn't crawl Javascript links.
Does that help?

How to allow simple HTML tags in comments or anywhere?

In my web application I am developing a comment functionality, where user's can comment. But I am facing a problem which is I want to allow simple HTML tags in the comment box. HTML tags like <b>, <strong>, <i>, <em>, <u>, etc., that are normally allowed to enter in a commenting box. But then I also want when user presses enter then it will be automatically converted into breaks (<br /> tags) and get stored into database, so that when I'll display them in the web page then they'll look like as user entered.
Can you please tell me how to parse that user entered only allowed set of HTML tags and how to convert enters into <br /> tags and then store them in database.
Or if anyone have some better idea or suggestion to implement this kind of functionality. I am using ASP.NET 2.0 (C#)
I noticed that StackOverflow.com is doing the same thing on Profile Editing. When we edit our profile then below the "About Me" field "basic HTML allowed" line is written, I want to do almost the same functionality.
I don't have a C# specific answer for you, but you can go about it a few different ways. One is to let the user input whatever they want, then you run a filter over it to strip out the "bad" html. There are numerous open source filters that do this for PHP, Python, etc. In general, it's a pretty difficult problem, and it's best to let some well developed 3rd party code do this rather than write it yourself.
Another way to handle it is to allow the user to enter comments in some kind of simpler markup language like BBCode, Textile, or Markdown (stackoverflow is using Markdown), perhaps in conjunction with a nice Javascript editor. You then run the user's text through a processor for one of these markup languages to get the HTML. You can usually obtain implementations of these processors for whatever language you are using. These processors usually strip out the "bad" HTML.
Its rather "simple" to do that in php and python due to the large number of functions.I am still learning c# .lol. but havent yet come across the function.The chances are that it exists and all you need to do is search for it.I mean a function that can take the user input,search for the allowed tags (which are in an array of course) and replace the <> with something else like [] then use a function to escape the other html tags.In php we use htmlentities().
Something like
<code>
$txt=$_POST['comment'];
$txt=strreplace("<b>*</b>","[b]*[/b],"$txt");
$securetxt=htmlentities($txt);
$finaltxt=strreplace("[b]*[/b]","<b>*</b>","$securetxt");
//Now save to Db
I'm not sure, but I think you have to escape html characters when inserting in database and when retrieving echo them unescaped, so the browser can see it just like html.
I don´t know asp.net, but in php there´s an easy function, strip_tags, that let you add exceptions (in your case, b, em, etc.). If there´s nothing like that in C# you can write a regular expression that strips out all tags except the allowed ones but chances are that such an expression already exists so it should be easy to find.
replacing \n (or something similar) with br shouldn´t be a problem either with a simple search and replace.
This is a dangerous road to go down. You might think you can do some awesome regexes, or find someone who can help you with it, but sanitizing SOME markup and leaving other is just crazy talk.
I highly recommend you look into BBCode or another token system. Even something untokenized such as what SO uses, is probably a much better solution.

Categories

Resources