Currently Browsing

Author Archive

Selenium – How to check if an element has focus

Problem:

We have a contact form with Name, Email and Comment fields, and if invalid data is entered to the fields, the validation system will highlight that field and it will be focused, so the user can change it quickly. In our tests we want to check if the proper field gets the focus.

<div class="contact-form">
    <label> Name </label>
    <input id="contact-name" type="text" />
    <label> Email </label>
    <input id="contact-email" type="text" />
    <label> Comment </label>
    <input id="contact-comment" type="text" />
</div>

(this is just an example code, and not a functional one)

Solution:

Selenium suggest to use css selectors, so we will start with that. To see if the element has the focus we will use css pseudo-class “:focus” and check if an element is present like this:

Assert.IsTrue(selenium.IsElementPresent("css=#contact-name:focus"));

This looks quite nice and easy, but you may encounter some problems with it. The element #contact-name can be found, but #contact-name:focus can not. In this case we have to think outside of the box, and come up with another solution. We can use the Javascript DOM locator to get the element that is selected (has focus), and compare it to the element we expect to have focus:

Assert.AreEqual(selenium.GetElementIndex("dom=document.activeElement"),
selenium.GetElementIndex("css=#contact-name"));

Binding data with Spring Forms

Spring has its own tag library, which makes JSPs easier to develop, maintain and read. One of these libraries is the form tag library, and in this post we’ll demonstrate how to use it.

For this we will use a sample project that will let us modify the user’s account details. In order to display the stored values for the account in the form, we have previously tied an object named foo to the session that holds all the information about our user’s account.

The form used for displaying and sending the account data is displayed here:

<form action="/account" method="post">
	<fieldset>
	<legend>Edit account information:</legend>
	<table>
		<tr>
			<td><label for="name" >Name:</label></td>
			<td><input type="text" id="name" name="name" value=${foo.name} /></td>
		</tr>
		<tr>
			<td><label for="email">E-mail:</label></td>
			<td><input type="text" id="email" name="email" value=${foo.email} /></td>
		</tr>
		<tr>
			<td><label for="website">Website:</label></td>
			<td><input type="text" id="website" name="website" value=${foo.website} /></td>
		</tr>
		<tr><td>&nbsp;</td><td><input type="submit" value="Save" /></td></tr>
	</table>
	</fieldset>
</form>

Now lets do the implementation of the form tag library on this code:

Servlet Mapping for Google App Engine and Spring

This post is all about Spring MVC 3 and Google App Engine and their interaction.

The starting point for this project was the Google App Engine’s documentation, the Google Eclipse plugin, and some tutorials found on various sites.

After reading all about the App Engine and Spring, the first thing to do was to create a project in our favorite editor, Eclipse. This generated some necessary files, and gave us a good starting point. As we want to use Spring to perform all its magic, we’ve created a web.xml like this:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>