<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The BrowserMob Blog &#187; FAQ</title>
	<atom:link href="http://blog.browsermob.com/category/faq/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.browsermob.com</link>
	<description>All about browsers, performance testing, and load testing</description>
	<lastBuildDate>Tue, 27 Dec 2011 17:00:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Scripting for BrowserMob using WebDriver</title>
		<link>http://blog.browsermob.com/2011/10/scripting-for-browsermob-using-webdriver/</link>
		<comments>http://blog.browsermob.com/2011/10/scripting-for-browsermob-using-webdriver/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 04:44:58 +0000</pubDate>
		<dc:creator>Anu Sandhanam</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[Load Testing Best Practices]]></category>
		<category><![CDATA[Load Testing Tips]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=1846</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Hello Readers!! It gives me immense pleasure in meeting you with yet another article on WebDriver. WebDriver has been creating a lot of buzz among the Selenium community lately and BrowserMob, an active member of the Selenium community, wasted no time in announcing its support for the WebDriver API. I hope you had the chance <a href='http://blog.browsermob.com/2011/10/scripting-for-browsermob-using-webdriver/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>Hello Readers!! It gives me immense pleasure in meeting you with yet another article on WebDriver. WebDriver has been creating a lot of buzz among the Selenium community lately and BrowserMob, an active member of the Selenium community, wasted no time in announcing its support for the WebDriver API.</p>
<p>I hope you had the chance to read the great <a href="http://blog.browsermob.com/2011/07/introducing-selenium-2-webdriver-support/" target="_blank">introduction article on the Webdriver API</a> by fellow blogger Ben.</p>
<p>In this article I would like to touch on the basics of scripting with the WebDriver and the BrowserMob API.<br />
I’m sure many of you, who are new to this API are asking yourselves these questions just like I did.</p>
<ul>
<li>What is WebDriver?</li>
<li>Why is it needed?</li>
</ul>
<p>WebDriver is a framework for automated testing of web applications. WebDriver is a part of Selenium 2.0, which is a major departure from the 1.0 model. Selenium 1.0 is a client-server architecture which uses the RC libraries to program tests that communicate with the server, and the server relays those commands to a browser.</p>
<p>The primary downside of the 1.0 model is that it is written in JavaScript. Browsers impose a strict security model on any JavaScript that they execute in order to protect a user from malicious scripts. Additionally, the API for RC has grown over time that users find it hard to keep up with this ever growing dictionary of methods. The WebDriver helps overcome both these hurdles with an object oriented API that controls the browser itself rather than running as a JavaScript application within the browser.</p>
<p>WebDriver has five major Interfaces &#8211; <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.ImeHandler.html" target="_blank">ImeHandler</a>, <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Navigation.html" target="_blank">Navigation</a>, <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Options.html" target="_blank">Options</a>, <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html" target="_blank">TargetLocator</a> , <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html" target="_blank">Timeouts</a>, the methods of which fall into following three categories.</p>
<ul>
<li>Control of the browser itself
<p>Eg: get(), navigate(), close()etc</li>
<li>Selection of WebElements
<p>Eg: findElement(), findElements() etc</li>
<li>Debugging aids
<p>Eg: getCurrentUrl(), getPageSource(), getTitle() etc</li>
</ul>
<p>Now that we have a clear understanding of what WebDriver is and what its benefits are, let’s take a deep dive into the various useful WebDriver methods exposed via BrowserMob through some example scripts. Let’s look at this simple script below, to begin with.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> driver <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">openBrowserWebDriver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> tx <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> step <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://seleniumhq.org/docs/06_test_design_considerations.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// polls DOM every 500 ms for ten seconds until the text 'Test Design Considerations' is found</span>
driver.<span style="color: #660066;">manage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">timeouts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">implicitlyWait</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>  TimeUnit.<span style="color: #660066;">SECONDS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Content validation - find element by xpath</span>
driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[@id='test-design-considerations']/h1[contains(text(), 'Test Design Considerations')]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">endTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Deciphering the script…</p>
<ul>
<li><em>var driver = browserMob.openBrowserWebDriver();</em><br />
The openBrowserWebDriver()method of WebDriver starts a WebDriver browser session.</li>
<p></p>
<li><em>driver.get(&#8220;http://seleniumhq.org/docs/06_test_design_considerations.html&#8221;);</em><br />
    The ‘get’ method which is equivalent to selenium.open() is used to navigate to any web page.</li>
<p></p>
<li><em>driver.manage().timeouts().implicitlyWait(10,  TimeUnit.SECONDS);<br />
driver.findElement(By.xpath(&#8220;//div[@id='test-design-considerations']/h1[contains(text(), 'Test Design Considerations')]&#8220;));</em></p>
<p></p>
<p>WebDriver has a blocking API. i.e. WebDriver will wait until the page has fully loaded (that is, the &#8220;onload&#8221; event has fired) before returning control to your test or script. However, under some conditions it is possible for a ‘get’ call to return before the page has finished loading. The classic example is JavaScript starting to run after the page has loaded (triggered by onload). Browsers (e.g. Firefox) will notify WebDriver when the basic HTML content has been loaded, which is when WebDriver returns. It&#8217;s difficult to know when JavaScript has finished executing, since JS code may schedule functions to be called in the future, depend on server response, etc. WebDriver cannot wait for all conditions to be met before the test proceeds because it does not know them. The solution to ensure whether the element involved in the next interaction is present and ready is to use the ‘Wait’ class to wait for a specific element to appear. This class simply calls findElement over and over, until the element is found (or a timeout has expired). Since this is the behavior desired by default for many users, a mechanism for implicitly-waiting for elements to appear has been implemented. This is accessible through the <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html" target="_blank">Timeouts() interface of the WebDriver class</a> (WebDriver.manage().timeouts() ). In the example script above, the driver.manage.timeouts().implicitlyWait call polls the DOM every 500 ms for 10 seconds until the text ‘Test Design Considerations’ is found.</li>
</ul>
<p><strong>Tips: </strong></p>
<ul>
<li>Optionally you can reset the Implicit Timeout to zero using the code below, as once set, it remains effective until the life of the WebDriver instance. The default setting is zero, i.e zero wait time.

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">manage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">timeouts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">implicitlyWait</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>  TimeUnit.<span style="color: #660066;">SECONDS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>It is possible to set a timeout for the entire step as such using the setStepTimeout method of the browserMob API. 

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Setting a step timeout of 10 seconds</span>
browserMob.<span style="color: #660066;">setStepTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
…….
<span style="color: #660066;">browserMob</span>.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Alternatively, browserMob’s  waitFor() method could be used in place of the implicitlyWait() for content validation. Modifying our script with waitFor()…

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> step <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
driver.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://seleniumhq.org/docs/06_test_design_considerations.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">waitFor</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> check <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[@id='test-design-considerations']/h1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test design considerations&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>check<span style="color: #009900;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #3366CC;">&quot;Content validation failed&quot;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<p>Great!! We just wrote our first WebDriver script. We now know how to interact with page elements via the findElement method of WebDriver’s WebElement Interface using XPath. But does WebDriver provide other alternatives for interaction? Absolutely.  Below are few alternative ways. As implied through the script, using the ‘findElement’ is a great way to validate page content before proceeding to the next step in the script.</p>
<p><strong>By.cssSelector:</strong><br />
<em>driver.findElement(By.cssSelector(&#8220;div#test-design-considerations &gt; h1&#8243;));</em></p>
<p><strong>By.linkText:</strong><br />
<em>driver.findElement(By.linkText(&#8220;Data Driven Testing&#8221;));</em></p>
<p><strong>By.partialLinkText:</strong><br />
<em>driver.findElement(By.partialLinkText(&#8220;Data Driven&#8221;));</em></p>
<p><strong>By.id:</strong><br />
<em>driver.findElement(By.id(&#8220;side&#8221;));</em></p>
<p><strong>By.className:</strong><br />
<em>driver.findElement(By.className(&#8220;expandable&#8221;));</em></p>
<p><strong>By.name:</strong><br />
<em>driver.findElement(By.name(&#8220;q&#8221;));</em></p>
<p><strong>By.tagName:</strong><br />
<em>driver.findElement(By.tagName(&#8220;input&#8221;));</em></p>
<p><strong>driver.findElements:</strong><br />
In situations where you have to iterate through multiple elements with the same id/class/tag name etc or have to pick one randomly, the ‘findElements’ method comes in handy. The example script below uses this method to pick a random suggestion from an AutoSuggest list.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;AutoSuggest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.simon.com/default.aspx&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">cssSelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[id*='KeywordTextBox']&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// clear the search box first</span>
elem.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// type Carlsbad in the search box</span>
elem.<span style="color: #660066;">sendKeys</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;carlsbad&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// wait for autosuggest to populate</span>
driver.<span style="color: #660066;">manage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">timeouts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">implicitlyWait</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> TimeUnit.<span style="color: #660066;">SECONDS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> autoSuggest <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElements</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[@class='ac_results']/ul/li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// 'autoSuggest' from above is a Java List. Either use size() to get its length </span>
<span style="color: #006600; font-style: italic;">// or convert this to an array - autoSuggest.toArray().length</span>
&nbsp;
browserMob.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>autoSuggest.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// click on a random element – picking the last element in this case</span>
<span style="color: #003366; font-weight: bold;">var</span> selected <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[@class='ac_results']/ul/li[&quot;</span><span style="color: #339933;">+</span>autoSuggest.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// throw an error if content validation returns false</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>driver.<span style="color: #660066;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Search Results&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #3366CC;">'Content error: &quot;Search Results&quot; not found'</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I’ve also managed to introduce a few other useful methods through this script. The comments above each one of them describe their purpose. </p>
<p><strong>Tips: </strong></p>
<ul>
<li>sendKeys can also be used to simulate pressing of the keyboard keys by using the &#8220;Keys&#8221; class. It is possible to call sendKeys on any element. The code below triggers the “ENTER’ key press to force a form submission. The list of Keys supported can be found <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/Keys.html" target="_blank">here</a>.

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">id</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'finalExpenses'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sendKeys</span><span style="color: #009900;">&#40;</span>Keys.<span style="color: #000066; font-weight: bold;">RETURN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>With advanced locators, it is possible to traverse down the DOM with a single line of code as in below:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">findElements</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">tagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">findElements</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">tagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
As the index starts with zero, the line of code above would click on the third “li” of the second “ul” on the page. </li>
</ul>
<p>A few more common page element interactions in detail…</p>
<p><strong>How to handle frames?</strong></p>
<p>Swinging between frames is easy with the <strong>switchTo() method of the TargetLocator interface of the WebDriver class.</strong></p>
<ul>
<li>Switch to the frame from main window</li>
<ul>
<li>By index

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">frame</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>By frame name

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">frame</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;frameName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<li>Switch back to the parent window

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">defaultContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<p>The following example illustrates a simple swinging between two frames on a web page. Let’s assume the page has two frames, frame1 and frame2, out of which frame1 has a link and frame2 has a form.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">frame</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;frame1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">linkText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SPAZ&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">defaultContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">frame</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;frame2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">id</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;field_0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sendKeys</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;anu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">className</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form_button&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">defaultContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Try running this snippet commenting out the “driver.switchTo().defaultContent();” before switching to frame2 and you would end up with an error that frame2 cannot be located. This explains the need for switching back to the parent page from inside a frame first, before navigating to another frame on the same page.</p>
<p><strong>How to handle JavaScript Alert and Confirmation boxes?</strong></p>
<p>Confirmation boxes are handled the same way as the Alerts.</p>
<ul>
<li>Switch focus to the alert/confirmation box

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">alert</span> <span style="color: #339933;">=</span> driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Dismiss – equivalent to hitting “Cancel” button on the Confirmation box or ignoring an Alert by closing it.

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span>.<span style="color: #660066;">dismiss</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Alternatively accept – equivalent to hitting “OK” on the Alert or the Confirmation box

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span>.<span style="color: #660066;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Handle Confirmation boxes the JavaScript way

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">executeScript</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;window.confirm = function(msg){return true;};&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<p><strong>How to handle Pop-up Windows?</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Handle Popup Windows&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
driver.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.javascript-coder.com/files/window-popup/javascript-window-open-example1.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Get the handle to the current window</span>
<span style="color: #003366; font-weight: bold;">var</span> currentHandle <span style="color: #339933;">=</span> driver.<span style="color: #660066;">getWindowHandle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
driver.<span style="color: #660066;">manage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">timeouts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">implicitlyWait</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>  TimeUnit.<span style="color: #660066;">SECONDS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Open Pop-up</span>
driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">partialLinkText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open the JavaScript Window&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Switch focus to Pop-up using its name</span>
driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">window</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mywindow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Alternate way - if the Pop-up window doesn't have a name, which is very common - </span>
<span style="color: #006600; font-style: italic;">// get the handle to the last opened window</span>
<span style="color: #003366; font-weight: bold;">var</span> handles <span style="color: #339933;">=</span> driver.<span style="color: #660066;">getWindowHandles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> handles.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span>
driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">window</span><span style="color: #009900;">&#40;</span>handles<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//close popup</span>
driver.<span style="color: #660066;">executeScript</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;self.close ();&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//switch focus to parent window</span>
driver.<span style="color: #660066;">switchTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">window</span><span style="color: #009900;">&#40;</span>currentHandle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>How to interact with SELECT drop-downs?</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> lastItem <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//select[@class='dropDown']/option[last()]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
lastItem.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>How to simulate advanced interaction such as Drag and Drop?</strong></p>
<p>The <a href="http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/interactions/Actions.html" target="_blank">Actions</a> subclass of the Selenium Interactions class makes emulation of these complex user gestures easier.</p>
<ul>
<li>Drag an element to drop on to another element

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://devfiles.myopera.com/articles/735/example.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> from <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//ul[@id='Fav']/li[3]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> to <span style="color: #339933;">=</span> driver.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//ul[@id='Tol']/li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Actions<span style="color: #009900;">&#40;</span>driver<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dragAndDrop</span><span style="color: #009900;">&#40;</span>from<span style="color: #339933;">,</span>to<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">perform</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Drag an element to an offset

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">driver.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://jqueryui.com/demos/draggable/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> draggable <span style="color: #339933;">=</span> browser.<span style="color: #660066;">findElement</span><span style="color: #009900;">&#40;</span>By.<span style="color: #660066;">id</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;draggable&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
Actions<span style="color: #009900;">&#40;</span>driver<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dragAndDropBy</span><span style="color: #009900;">&#40;</span>draggable<span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">perform</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<p>I hope this article served as a comprehensive tutorial, covering most of the basics of WebDriver, if not all. See you soon with more useful WebDriver tips and tricks. Happy testing until then!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/10/scripting-for-browsermob-using-webdriver/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>FAQ:  How do I annotate a load test?</title>
		<link>http://blog.browsermob.com/2011/08/faq-how-do-i-annotate-a-load-test/</link>
		<comments>http://blog.browsermob.com/2011/08/faq-how-do-i-annotate-a-load-test/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 23:43:58 +0000</pubDate>
		<dc:creator>Mindy Hovern</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[New Feature]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=1761</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Select any time coordinate in your test plan and simply add a comment in the annotation field.  Your annotations are tracked and numbered with a flag, so to review, simply reference the number on the flag in the list of annotations made for that test plan.  Don’t worry about making mistakes, you can delete an <a href='http://blog.browsermob.com/2011/08/faq-how-do-i-annotate-a-load-test/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>Select any time coordinate in your test plan and simply add a comment in the annotation field.  Your annotations are tracked and numbered with a flag, so to review, simply reference the number on the flag in the list of annotations made for that test plan.  Don’t worry about making mistakes, you can delete an annotation at any time.<a href="http://blog.browsermob.com/wp-content/uploads/2011/08/load-test-annotation.jpg"><img class="alignright size-full wp-image-1762" src="http://blog.browsermob.com/wp-content/uploads/2011/08/load-test-annotation.jpg" alt="" width="902" height="723" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/08/faq-how-do-i-annotate-a-load-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Repost: In the Driver’s Seat: Load Testing Best Practices – Scripting</title>
		<link>http://blog.browsermob.com/2011/08/repost-in-the-driver%e2%80%99s-seat-load-testing-best-practices-%e2%80%93-scripting/</link>
		<comments>http://blog.browsermob.com/2011/08/repost-in-the-driver%e2%80%99s-seat-load-testing-best-practices-%e2%80%93-scripting/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 11:51:32 +0000</pubDate>
		<dc:creator>Anu Sandhanam</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[Load Testing Best Practices]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[BrowserMob]]></category>
		<category><![CDATA[load testing best practices]]></category>
		<category><![CDATA[Neustar]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Webmetrics]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=1487</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Hello Readers!! Welcome back to Part 3 of our Load Testing Best Practices series, which focuses on writing good load testing scripts. As I’m sure you would agree to, load testing is an imperative part of every development effort. One of the key components to your load testing efforts is a good load testing script <a href='http://blog.browsermob.com/2011/08/repost-in-the-driver%e2%80%99s-seat-load-testing-best-practices-%e2%80%93-scripting/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>Hello Readers!! Welcome back to Part 3 of our Load Testing Best Practices series, which focuses on writing good load testing scripts.</p>
<p>As I’m sure you would agree to, load testing is an imperative part of every development effort. One of the key components to your load testing efforts is a good load testing script that simulates real user behavior on your web site in the most realistic and accurate way possible.</p>
<p>Before you take a deep dive into the scripting, there are few things you need to plan ahead of time.</p>
<ul>
<li> The first one obviously is the scenario outline. Many clients seek the help of their analytics to identify their most visited pages.</li>
<li> Secondly, identify a reasonable range of pause times between steps.</li>
<li> As the next step, identify if your script will involve randomization or parameterization. If so, are you planning on using a datafile or an array of random values in the script?</li>
</ul>
<p>If you have the answers for all of the above then you are half way through already as what&#8217;s remaining is packaging this into a script which is the objective of this post.  This post is a step by step tutorial on how to write a good load testing  script using the Selenium API and the BrowserMob platform.  However,  some of the tips and information can be used to help with script  creation on other platforms as well. We will be focusing on how to  organize your scripts, how and why to perform content validation, how to  build robust and reusable scripts with the help of optimized selenium  locators amongst other tips and guidelines on the best practices. I’m  confident that at the end of this article you will be able to create  your own load testing scripts in no time.</p>
<p>If you haven’t done so already, you are encouraged to install the following to assist in building the load testing scripts.</p>
<ul>
<li><a href="http://seleniumhq.org/projects/ide/" target="_blank">Selenium IDE</a></li>
<li><a href="http://getfirebug.com/" target="_blank">Firebug</a></li>
<li><a href="https://addons.mozilla.org/en-us/firefox/addon/xpath-finder/" target="_blank">Xpath Finder</a></li>
</ul>
<p><strong>What makes a good load testing script?</strong></p>
<ol>
<li><em><strong>Detailed Comments and Descriptions</strong></em></li>
<p>Although the presence or absence of these is in no way going to affect the performance of your load test, it is always a good practice to include comments wherever possible as they make a good reference for later use, irrespective of the scripting language under use. It is easy to add comments while scripting with the IDE. Right click on the IDE and choose ‘Insert New Comment’.</p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/comments2.bmp"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/comments2.bmp" alt="" /></a></p>
<p><em><strong>Tip:</strong> All comments added in the IDE are automatically converted to “Step Descriptions” when the script is uploaded to BrowserMob. i.e the comment in the picture above would be converted to a BrowserMob step description like below:</em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Giving each step/page a brief description rather than just “Step 1”, “Step 2” etc also helps find out easily how each step/page is performing during the load testing.</p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/graph.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/graph.png" alt="" width="679" height="363" /></a></p>
<li><em><strong>Content validation combined with proper use of ‘waitForPageToLoad’ and ‘waitForNetworkTrafficToStop’</strong></em></li>
<p>“Content validation” is an essential part of every load testing script. Content validation is the only means to confirm the right page got downloaded or the right action was performed during a load test and this can be accomplished by searching for an appropriate piece of content in every step that confirms the validity of the page being downloaded. Selenium has various useful commands that can be used for content validation such as verifyTextPresent, VerifyElementPresent, assertTextPresent, assertElementPresent, waitForTextPresent, waitForElementPresent etc.</p>
<p><em><strong>Tip:</strong> While recording actions with IDE, it is easy to add content validation at the appropriate places. Highlight the appropriate piece of content in each page load, right click and choose the appropriate content validation command as in the screenshot below:</em></p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/verify_text.bmp"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/verify_text.bmp" alt="" /></a></p>
<p>Consider the snippets below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cnn.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Latest news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1a]</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cnn.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Latest news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1b]</p>
<p><em>Which one of the above is the right one? </em></p>
<p>The first one will fail immediately because the page needs to load completely or at least partially until the content “Latest news” becomes visible. The second one may not fail but is recommended only if you intend to measure the “Perceived Load Time” rather than the complete “Page Load Time”. What is the difference between the two? In the second snippet above (script 1b), the script is going to wait only until the content “Latest news” becomes visible before moving on to the next step. To a user that intends to load the CNN Home page and click on the first article under “Latest news”, this perceived load time is all they are going to care about. Whereas, the complete “Page Load Time” is measured only if the script waits for the page to load completely before moving on to the next step and this can be accomplished by using <a href="http://static.browsermob.com/api/com/browsermob/api/Selenium.html#waitForPageToLoad(java.lang.String)" target="_blank">selenium API’s waitForPageToLoad</a> and/or <a href="http://static.browsermob.com/api/com/browsermob/api/BrowserMob.html#waitForNetworkTrafficToStop(long,%20long)" target="_blank">BrowserMob API’s waitForNetworkTrafficToStop</a>.</p>
<p>Modifying the second snippet from above:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">60000</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cnn.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Latest news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1c]</p>
<p><em>When is BrowserMob API’s waitForNetworkTrafficToStop helpful? </em></p>
<p>Consider the scenario wherein the user loads the site united.com and tries to find the airport code using the site’s AutoSuggest feature.</p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/UNITED.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/UNITED.png" alt="" width="521" height="506" /></a></p>
<p>Let’s consider the snippet below that tries to simulate the above scenario:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 1: United Airlines Home&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.united.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span>“Travel information”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 2: Choose Flights using AutoSuggest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id=shop_from0_temp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">typeKeys</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id=shop_from0_temp&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;SAN &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">mouseOver</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div#autoSuggestDiv &amp;gt; div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">clickAt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div#autoSuggestDiv &amp;gt; div&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
………
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1d]</p>
<p>Running a validation for the script above threw the error “Element css=div#autoSuggestDiv &gt; div not found”. What was the reason? Look at the waterfall chart for united.com Home page below:</p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/index.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/index.png" alt="" width="743" height="28" /></a></p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/united_1a.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/united_1a.png" alt="" width="929" height="577" /></a><br />
<a href="http://blog.browsermob.com/wp-content/uploads/2011/08/united_2a.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/united_2a.png" alt="" width="929" height="540" /></a><br />
<a href="http://blog.browsermob.com/wp-content/uploads/2011/08/united_3a.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/united_3a.png" alt="" width="850" height="560" /></a><br />
<a href="http://blog.browsermob.com/wp-content/uploads/2011/08/united_4a.png"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/united_4a.png" alt="" width="929" height="305" /></a></p>
<p>The ‘waitForPageToLoad’ at Step 1 captured the traffic only until the document was complete. But as the waterfall chart above shows, several images, JavaScript files and iframes loaded asynchronously after the document was complete, especially the JavaScript file ‘airportSuggestion.js’ which was required for the autosuggestion at Step 2. Since the script simulated entering of the airport code ‘SAN’ even before the airportSuggestion.js was loaded, the autosuggest did not work as expected, resulting in a script failure.</p>
<p>Here is the modified script that works as expected:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 1: United Airlines Home &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.united.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">waitForNetworkTrafficToStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2000</span><span style="color: #339933;">,</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Travel information&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 2: Choose Flights using AutoSuggest &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id=shop_from0_temp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">typeKeys</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id=shop_from0_temp&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;SAN &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// wait for the network traffic to stop and the autosuggest to populate</span>
browserMob.<span style="color: #660066;">waitForNetworkTrafficToStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2000</span><span style="color: #339933;">,</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">mouseOver</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div#autoSuggestDiv &amp;gt; div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">clickAt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div#autoSuggestDiv &amp;gt; div&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1e]</p>
<p>I’d like to highlight one more important aspect of the ‘waitForPageToLoad/waitFor[Text/Element]Present’ and ‘waitForPageToLoad/verify[Text/Element]Present’ combination. While both work fine it is recommended to use the combination waitForPageToLoad/verify[Text/Element]Present for load testing. Why?</p>
<p>Consider the snippets below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">60000</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cnn.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Latest news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1f]</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">60000</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cnn.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Latest news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>[Example: 1g]</p>
<p>Both would exhibit the same behavior when the page loads successfully. However, in the instance of an error, say an ‘Internal Server’ error or a ‘Connection Reset’ error, the script would declare an error immediately after the error page loads with script 1f while it’ll wait for 60 more seconds to declare the same error with script 1g as the ‘waitForTextPresent(“Latest news”) is going to wait for 60 more seconds, which is the timeout we’ve set in the script, to declare the same content error, irrespective of the page’s status code. Besides,  script 1f will help simulate more transactions in the same amount of time than script 1g, especially in the event of errors.</p>
<li><em><strong>Setting Proper Timeouts</strong></em></li>
<p>The default selenium timeout is 30 seconds (30000 milliseconds) and the default TimeUnit in use by BrowserMob is milliseconds. It must be remembered that most of the &#8216;waitFor&#8217; commands like the &#8216;waitForElementpresent&#8217;, &#8216;waitForVisible&#8217;, &#8216;waitFortextPresent&#8217; etc wait for however long this default selenium timeout is set to. Timeouts can be set the following way in the script:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">60000</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is recommended to set the timeout value before the actual transaction begins in the script. Studies show that the ideal page load time is 5 seconds. Any amount of load time greater than this is considered unacceptable for real users. However, 5 seconds is too small of a timeout threshold for load testing as load times are expected to rise with increase in user load. Hence, 60 seconds to 120 seconds would be considered ideal. The upper limit on the timeout threshold set by BrowserMob is 120 seconds.</p>
<p>Occasionally, you may have the necessity to set a different timeout value for different page loads which can be accomplished by the solution proposed in this <a href="http://blog.browsermob.com/2010/06/advanced-handling-of-page-timeouts-in-selenium/" target="_blank">article</a>. The article on <a href="http://blog.browsermob.com/2011/03/selenium-tips-wait-with-waitforcondition/" target="_blank">waitForCondition</a> may also make an interesting read to learn more about advanced handling of timeouts.</p>
<li><em><strong>Optimization of Locators</strong></em></li>
<p>Selenium provides several ways of locating elements on pages. It may be worthwhile to read through this before proceeding further: <a href="http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.html" target="_blank">http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.html</a></p>
<p>The most commonly used locator is the xpath. FireFox plugins like xpath finder, firebug may be used to identify the correct xpath for the appropriate elements. Below are a couple of useful articles on this topic:</p>
<p><a href="http://blog.browsermob.com/2008/12/using-firebug-for-load-testing/" target="_blank">http://blog.browsermob.com/2008/12/using-firebug-for-load-testing/ </a></p>
<p><a href="http://blog.browsermob.com/2009/04/test-your-selenium-xpath-easily-with-firebug/" target="_blank">http://blog.browsermob.com/2009/04/test-your-selenium-xpath-easily-with-firebug/</a></p>
<p><a href="http://blog.browsermob.com/wp-content/uploads/2011/08/xpath1.bmp"><img src="http://blog.browsermob.com/wp-content/uploads/2011/08/xpath1.bmp" alt="" /></a></p>
<p>Optimization of the locators used is extremely important for script robustness and re-usability. In the screenshot below, the IDE has captured the xpath ‘link=Arrested GI had gunpowder, ammo’ when clicking on the first article under “Latest News”. Obviously, the script will work only until this article is available on the Home Page. The xpath shown by firefbug for the same is ‘/html/body/div[5]/div/div[4]/div[3]/div/div[3]/div/ul[2]/li/a’ . Again, this might work temporarily but the moment the site is updated and the DOM changes, the script could break because of this complex xpath as the hierarchy of the elements could have been altered. ‘//ul[@class='cnn_bulletbin']/li[1]/a’ would be a much more optimized and a less brittle xpath for this example. Learn more about locators and pattern matching <a href="http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/" target="_blank">here</a>.</p>
<li><em><strong>Pause Times</strong></em></li>
<p>Inserting pause times in between steps is essential in order to simulate real user actions more closely. A range of pause times is recommended over a fixed time as different users browse through a web site at different paces. Both <a href="http://static.browsermob.com/api/com/browsermob/api/BrowserMob.html#pause(long)" target="_blank">BrowserMob</a> and <a href="http://static.browsermob.com/api/com/browsermob/api/Selenium.html#pause(java.lang.String)" target="_blank">Selenium API’s pause</a> method can be used to insert think/pause times in between steps.</p>
<li><em><strong>Randomization</strong></em></li>
<p>In the context of load testing, I want to emphasize the extreme importance of having large datasets available for testing. Many important bugs simply do not surface unless you deal with very large entities such thousands of users, thousands of mail server mailboxes etc. For scenarios involving product checkout, browsing through product pages etc, consider randomizing the product being purchased or browsed through, from a list of several products in order to simulate real user behavior more realistically as not all users are going to be purchasing the same product at the same time. Randomization and parameterization can be achieved in a few ways which are explained in depth <a href="http://blog.browsermob.com/2010/10/parameterizing-your-tests-part-2/" target="_blank">here</a> and <a href="http://blog.browsermob.com/help/creating-a-test/parameterizing-your-tests/" target="_blank">here</a>.</p>
<li><strong><em>Use of quickStop() and stopExpected()</em></strong></li>
<p>The BrowserMob API has two methods that come in handy when you want to pause a test that has exceptionally lengthy scripts running, such as loops that repeat certain actions several times over the course of the script execution.</p>
<p>By default, when a test is paused, BrowserMob will wait for the actively running scripts to finish executing and complete the current transaction. Long running scripts with loops or scripts testing a streaming video will not be interrupted until they complete. So in order to abort such transactions immediately on pausing, it is important to include the method <a href="http://static.browsermob.com/api/com/browsermob/api/BrowserMob.html#quickStop%28%29" target="_blank">browserMob.quickStop()</a> at the top of the script.</p>
<p>However, forcefully aborting the scripts could result in an increased error rate, skewing the results. This can be avoided by using quickStop() in conjunction with <a href="http://static.browsermob.com/api/com/browsermob/api/BrowserMob.html#stopExpected%28%29" target="_blank">stopExpected()</a>. This instructs BrowserMob that the script is expecting to be stopped at any time and if that happens, the sudden stop should not be considered an error.</p>
<p>Here is a complete script, incorporating all of the above:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">quickStop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">stopExpected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> selenium <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">openBrowser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">getActiveHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  Set the timeout threshold</span>
<span style="color: #006600; font-style: italic;">//  Set a different timeout threshold for local validation as it has a limit on the</span>
<span style="color: #006600; font-style: italic;">// transaction length, capped at 2 minutes.</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>browserMob.<span style="color: #660066;">isValidation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 	<span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">30000</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">120000</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
selenium.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//*************** Randomize think time *********************//</span>
<span style="color: #003366; font-weight: bold;">function</span> randomXToY<span style="color: #009900;">&#40;</span>minVal<span style="color: #339933;">,</span>maxVal<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #003366; font-weight: bold;">var</span> randVal <span style="color: #339933;">=</span> minVal<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>maxVal<span style="color: #339933;">-</span>minVal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 	<span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>randVal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> sleep<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>browserMob.<span style="color: #660066;">isValidation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  		browserMob.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span>randomXToY<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 	<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
  		browserMob.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span>randomXToY<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//********** Randomly pick an articleId, SearchString combination *************//</span>
<span style="color: #003366; font-weight: bold;">function</span> randOption<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> list <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;1,PERMALINK;2,Related Links;3,Related Articles;4,Most Popular&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> list.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">//pick a random combination from the array every time</span>
	<span style="color: #003366; font-weight: bold;">var</span> position <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>myArray.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> myArray1 <span style="color: #339933;">=</span> myArray<span style="color: #009900;">&#91;</span>position<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> myArray1<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> selected <span style="color: #339933;">=</span> randOption<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> selected<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> string <span style="color: #339933;">=</span> selected<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Logging the variables being set provides a good means of troubleshooting</span>
browserMob.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Begin Transaction</span>
transaction <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Open CNN Home&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cnn.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Latest news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
sleep<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click on a random article&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Randomly clicking on an article with every script run</span>
selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//ul[@class='cnn_bulletbin']/li[&quot;</span><span style="color: #339933;">+</span>id<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]/a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">endTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As web sites get complex, the scenarios for the load testing tend to get complex and so does the scripting and other load testing requirements such as configuration and set up. Our team of trained load testing experts is waiting to assist you with all your complex load testing requirements. <a href="http://www.webmetrics.com/About-Us/Contact-Us" target="_blank">Contact us</a> right away and get all your load testing needs fulfilled!!</p>
<p>Hope you enjoyed reading this article as much as I enjoyed writing it. Coming up next in this Load Testing Best Practices blog series is “Test Execution” so be sure to stay tuned.</p>
<p>Happy Testing!!</ol>
<hr />
<p><strong>More articles<em>In the Driver&#8217;s Seat</em> Series:</strong><br />
<a href="http://blog.browsermob.com/2011/07/repost-in-the-drivers-seat-load-testing-best-practices-planning-2/">Part One: Planning</a><br />
<a href="http://blog.browsermob.com/2011/07/repost-in-the-driver%e2%80%99s-seat-load-testing-best-practices-configuration/">Part Two: Configuration</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/08/repost-in-the-driver%e2%80%99s-seat-load-testing-best-practices-%e2%80%93-scripting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>FAQ: How do I load test cached pages?</title>
		<link>http://blog.browsermob.com/2011/08/faq-how-do-i-load-test-cached-pages/</link>
		<comments>http://blog.browsermob.com/2011/08/faq-how-do-i-load-test-cached-pages/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 18:55:50 +0000</pubDate>
		<dc:creator>David C.</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[Cache]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=1557</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Occasionally, users may want to conduct a load test using cached pages. BrowserMob offers this ability through scripting using RBUs. Typical behavior of a RBU is to the run the script once, close the browser, clear the cache, and start over. To prevent the browser from closing and the cache from being cleared, the steps <a href='http://blog.browsermob.com/2011/08/faq-how-do-i-load-test-cached-pages/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p style="text-align: left;">Occasionally, users may want to conduct a load test using cached pages. BrowserMob offers this ability through scripting using RBUs. Typical behavior of a RBU is to the run the script once, close the browser, clear the cache, and start over. To prevent the browser from closing and the cache from being cleared, the steps of the script are placed in a loop.  The loop encases everything between <code>browserMob.beginTransaction();</code> and<code> browserMob.endTransaction();</code> which leaves the browser open and prevents the cache from being cleared until the final iteration of the loop and the end of the script.</p>
<p style="text-align: left;">The first step in setting up a cached load test is to determine the percentage of transactions to run cached. For example, 10 loops will produce a 90% cache rate, 4 loops will produce 75% because the first iteration of the loop will not be cached; there would be 9/10 or 3/4 cached loops respectively.</p>
<p style="text-align: left;">Once the number of loops is determined, define a variable for the number of loops:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> loops <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 90% cache rate</span></pre></div></div>

<p style="text-align: left;">For script validation, ensure the number of loops has been reduced using the command <code>browserMob.isValidation(); </code> If the loops are not reduced for validation, an invalid script may result with an error message of: “Script took too long and reached timeout limit.”</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>browserMob.<span style="color: #660066;">isValidation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  loops <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: left;">Next, enclose the steps of the script inside of a “for” loop, the size of the script does not matter, but make sure the proper syntax is followed and brackets are opened and closed properly. Also, make sure <code>browserMob.beginTransaction();</code> and <code>browserMob.endTransaction();</code> are inside the loop. Otherwise an inaccurate count of the number of transactions will result:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> loops<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  browserMob.<span style="color: #660066;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// rest of script here...</span>
  <span style="color: #006600; font-style: italic;">// and here...</span>
  <span style="color: #006600; font-style: italic;">// and here...</span>
  <span style="color: #006600; font-style: italic;">// etc...</span>
  browserMob.<span style="color: #660066;">endTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: left;">Finally, the commands <code>browserMob.quickStop();</code> and <code>browserMob.stopExpected();</code> need to be somewhere in the script. Normally when a load test is paused or the load level drops to fewer concurrent users, BrowserMob allows the actively running scripts to finish executing and complete the current transaction. Sometimes, especially for very long running scripts, it is desirable to interrupt the script immediately. Calls to <code>browserMob.quickStop();</code> tell BrowserMob that when a &#8220;stop&#8221; event (such as a pause, ramp down, or end of a test) occur, the script should attempt to stop executing as quickly as possible. The <code>browserMob.stopExpected();</code> command tells BrowserMob that the script is expecting that it could be stopped at any time and that if that happens, it should not be considered an error.</p>
<p style="text-align: left;">Here are all the components put together:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">quickStop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">stopExpected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> timeout <span style="color: #339933;">=</span> <span style="color: #CC0000;">60000</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> selenium <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">openBrowser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> loops <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 90% cache rate</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>browserMob.<span style="color: #660066;">isValidation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  loops <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> loops<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  browserMob.<span style="color: #660066;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// rest of script here...</span>
  browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// and here...</span>
  <span style="color: #006600; font-style: italic;">// and here...</span>
  <span style="color: #006600; font-style: italic;">// etc...</span>
  browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  browserMob.<span style="color: #660066;">endTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/08/faq-how-do-i-load-test-cached-pages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FAQ: When should I use BrowserMob timeout values?</title>
		<link>http://blog.browsermob.com/2011/05/faq-when-should-i-use-browsermob-timeout-values/</link>
		<comments>http://blog.browsermob.com/2011/05/faq-when-should-i-use-browsermob-timeout-values/#comments</comments>
		<pubDate>Wed, 11 May 2011 02:51:45 +0000</pubDate>
		<dc:creator>Connie</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Selenium]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=1140</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
As you build out your script and plan for a successful load test, it’s important to design the script that incorporates timeout values. BrowserMob has a few types of timeouts that you can use: HTTP-level, Selenium page-level, and Script-level timeouts. HTTP-level timeouts are controlled by the HTTP client, usually the “c” variable. var c = <a href='http://blog.browsermob.com/2011/05/faq-when-should-i-use-browsermob-timeout-values/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p style="text-align: left;">As you build out your script and plan for a successful load test, it’s important to design the script that incorporates timeout values. BrowserMob has a few types of timeouts that you can use: HTTP-level, Selenium page-level, and Script-level timeouts.</p>
<p style="text-align: left;">HTTP-level timeouts are controlled by the HTTP client, usually the “c” variable. var c = browserMob.getActiveHttpClient(); and there are 3 commands relating to HTTP timeouts: setConnectionTimeout, setRequestTimeout, and setSocketOperationTimeout.  For more information, check out our <a href="http://static.browsermob.com/api/com/browsermob/api/HttpClient.html">API</a> documentation which outlines each of these commands.</p>
<p style="text-align: left;">Selenium page-level timeouts are controlled via selenium.setTimeout() and affect waitForXyz commands and open commands. The default value is 30 seconds and can be set up to 120 seconds. However, if set higher; the timeout will cause an error when you run a load test.</p>
<p style="text-align: left;">If the page-level timeouts are changed the HTTP-level timeouts setConnectionTimeout and setSocketOperationTimeout are automatically updated to two times the Selenium timeout (setRequestTimeout is not changed because it is -1 by default, which is infinity).</p>
<p style="text-align: left;">Selenium commands timeouts are how long Selenium waits for a command to return. This is hardcoded to 135 seconds (purposely designed to be more than the 120 second Selenium timeout). If a command does not return in this time frame, we assume the transaction is broken, shut it down, and throw out the results. This is how we detect crashed browsers and other errors.</p>
<p style="text-align: left;">Script-level timeout is the time a script has to complete the value. Typically, this is set to infinity for load testing, two minutes for validation, and three minutes for monitoring. Most transactions can complete within two minutes, but some scripts may take longer. The most common reason is the addition of long pauses using browserMob.pause();.  In these cases, include  logic to make those long pauses shorter during validation using the isValidation(); command. See example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>browserMob.<span style="color: #660066;">isValidation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  browserMob.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  browserMob.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: left;">In a load test, simulating think time is important, but during validation it may not be as important. However, if your script still does not validate, try validating it locally using the local-validation tool and then check &#8220;bypass validation&#8221; when saving the script. Next,  run a small load test (between five to ten users) before running a larger one to confirm the script works as expected.  If there are still issues, feel free to contact <a href="mailto:support@browsermob.com">support@browsermob.com</a> as you may need to get an  override code to resolve the issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/05/faq-when-should-i-use-browsermob-timeout-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FAQ: When should I use “waitForElementPresent” command in my script?</title>
		<link>http://blog.browsermob.com/2011/04/faq-when-should-i-use-%e2%80%9cwaitforelementpresent%e2%80%9d-command-in-my-script/</link>
		<comments>http://blog.browsermob.com/2011/04/faq-when-should-i-use-%e2%80%9cwaitforelementpresent%e2%80%9d-command-in-my-script/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 19:31:08 +0000</pubDate>
		<dc:creator>Connie</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[Load Testing]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Selenium IDE]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=1095</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
selenium.waitForElementPresent(“myelement”); The waitForElementPresent commands polls the page waiting for the specified element to load. If you are familiar with Webmetrics/WATIR scripting, the command is very similar to the wait_until() command. The waitForElementPresent command is most often used on sites containing dynamic content such as Ajax, JavaScript, etc. Scripts are designed to execute as quickly as <a href='http://blog.browsermob.com/2011/04/faq-when-should-i-use-%e2%80%9cwaitforelementpresent%e2%80%9d-command-in-my-script/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p><strong>selenium.waitForElementPresent(“myelement”);</strong></p>
<p>The <strong>waitForElementPresent</strong> commands polls the page waiting for the specified element to load. If you are familiar with Webmetrics/WATIR scripting, the command is very similar to the <strong>wait_until()</strong> command. The <strong>waitForElementPresent</strong> command is most often used on sites containing dynamic content such as Ajax, JavaScript, etc. Scripts are designed to execute as quickly as possible and Selenium tends to fire steps faster than the DOM can render changes, causing issues/errors with the script playback.</p>
<p>There are a few ways to determine if you need this command:</p>
<p><strong> </strong></p>
<p>The first is to check the playback in SeleniumIDE:</p>
<p><img class="aligncenter size-full wp-image-1096" title="Playback in SeleniumIDE" src="http://blog.browsermob.com/wp-content/uploads/2011/04/Image-6.png" alt="Playback in SeleniumIDE" width="490" height="156" /></p>
<p>This type of error may indicate a timing problem, i.e., the element specified by a locator in your command wasn’t fully loaded when the command was executed. Try putting a pause 5000 before the command to determine whether the problem is indeed related to timing. If so, investigate using an appropriate <strong>waitFor*</strong> or <strong>*AndWait</strong> command before the failing command(s).</p>
<p>If playback is successful in the IDE but not on the local-validator or in BrowserMob try adding the command to the script and see if the script passes. For example, you are clicking on a link; if SeleniumIDE picked-up the element as myFooElement when you recorded the script just add the <strong>waitForElementPresent</strong> command before the click command:</p>
<p><strong>selenium.waitForElementPresent(“myFooElement”);</strong></p>
<p><strong>selenium.click(“myFooElement”);</strong></p>
<p>A third way to check is to examine the validation.txt (generate by the local-validation service) or click the “View Log” link in BrowserMob for the script. If the validation.txt or the log shows an error like the one below try adding the wait command, especially if you are certain the element exists on the page.</p>
<pre><strong>***** ERROR DETECTED *****</strong></pre>
<pre><strong>Error Message: Wrapped com.thoughtworks.selenium.SeleniumException: ERROR: Element myFooElement not found</strong></pre>
<pre><strong>Error Line Num: 25</strong></pre>
<pre><strong>***** ERROR DETECTED *****</strong></pre>
<p>The above error indicates the element could not be found. This may be an indication the script executed the command before the site had completely loaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/04/faq-when-should-i-use-%e2%80%9cwaitforelementpresent%e2%80%9d-command-in-my-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FAQ: What blend of RBUs and VUs would you recommend for my load tests?</title>
		<link>http://blog.browsermob.com/2011/03/faq-what-blend-of-rbus-and-vus-would-you-recommend-for-my-load-tests/</link>
		<comments>http://blog.browsermob.com/2011/03/faq-what-blend-of-rbus-and-vus-would-you-recommend-for-my-load-tests/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 23:41:40 +0000</pubDate>
		<dc:creator>Connie</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[RBU]]></category>
		<category><![CDATA[VU]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=950</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Setting up your load tests with a blend of RBUs and VUs can be ideal but it does depend on the type of scripts you are running.  Does your script require the user to fill out a form or does the site use AJAX? For those instances, RBUs work best. If your script is about <a href='http://blog.browsermob.com/2011/03/faq-what-blend-of-rbus-and-vus-would-you-recommend-for-my-load-tests/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>Setting up your load tests with a blend of RBUs and VUs can be ideal but it does depend on the type of scripts you are running.  Does your script require the user to fill out a form or does the site use AJAX? For those instances, RBUs work best. If your script is about browsing the site as a read-only, VUs work just as well; plus saves you money. VUs is simply HTTP and may be harder to script (but 1/10th the price!).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/03/faq-what-blend-of-rbus-and-vus-would-you-recommend-for-my-load-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selenium Tips &#8211; Wait with WaitForCondition</title>
		<link>http://blog.browsermob.com/2011/03/selenium-tips-wait-with-waitforcondition/</link>
		<comments>http://blog.browsermob.com/2011/03/selenium-tips-wait-with-waitforcondition/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 01:02:23 +0000</pubDate>
		<dc:creator>Anu Sandhanam</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[Monitoring]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=915</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Hello Readers, Welcome Back !! As the first part of this Selenium Tips series, this article intends to summarize the different ways to interact with dynamic sites that refresh content asynchronously wherein the script will have to wait for certain elements to appear or disappear before proceeding further. As you are already aware of, Selenium <a href='http://blog.browsermob.com/2011/03/selenium-tips-wait-with-waitforcondition/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>Hello Readers, Welcome Back !!</p>
<p>As the first part of this Selenium Tips series, this article intends to summarize the different ways to interact with dynamic sites that refresh content asynchronously wherein the script will have to wait for certain elements to appear or disappear before proceeding further.  As you are already aware of, Selenium has several ‘waitFor’ commands that fulfill this purpose. waitForCondition is one among them. ‘waitForCondition’ basically takes in two arguments, a JavaScript snippet and a timeout period in milliseconds. The snippet is executed either until it returns true or until the timeout period, after which the command will return an error. Now enough of theory and let’s get into action.</p>
<p><strong>Example:</strong><br />
<strong><i>Variation 1:</i></strong><br />
Consider the following example where there are two drop-downs, one for the states and the other for counties.<br />
The county drop-down is populated based on the state selection. </p>
<p>(<a href="http://www.bitrepository.com/apps/viewDemo/?originalPost=http://www.bitrepository.com/dynamic-dependant-dropdown-list-us-states-counties.html&amp;demoPage=http://www.bitrepository.com/demo/dynamic-dependent-dropdown-list/%22%20target=%22_blank%22%3Ehttp://www.bitrepository.com/apps/viewDemo/?originalPost=http://www.bitrepository.com/dynamic-dependant-dropdown-list-us-states-counties.html&amp;demoPage=http://www.bitrepository.com/demo/dynamic-dependent-dropdown-list/">Example</a> courtesy of bitrepository.com)</p>
<p>You can see in the site above, when the state is selected, a spinner shows up for a few seconds before the county drop-down is populated. Using waitForCondition and some JavaScript, we wait until display style attribute changes to ‘none’ in the snippet below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.bitrepository.com/apps/viewDemo/?originalPost=http://www.bitrepository.com/dynamic-dependant-dropdown-list-us-states-counties.html&amp;amp;demoPage=http://www.bitrepository.com/demo/dynamic-dependent-dropdown-list/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">selectFrame</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mainFrame&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;state&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;label=California&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForCondition</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;var value = selenium.browserbot.findElementOrNull('loading_county_drop_down'); value.style.display == 'none'&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">select</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;county&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;label=Amador County&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong><i>Variation 2:</i></strong><br />
You may also use logical operators to add additional logic.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">waitForCondition</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;var value = selenium.browserbot.findElementOrNull('loading_county_drop_down'); value.style.display == 'none' || selenium.browserbot.getUserWindow().document.form.county.options[0].text == 'Select a county of California';&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong><i>Variation 3:</i></strong><br />
The same can also be accomplished by simply using waitForNotVisible and/or waitForSelectOptions.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">waitForNotVisible</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;loading_county_drop_down&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
selenium.<span style="color: #660066;">waitForSelectOptions</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;county&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;regexpi:Amador County&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You may wonder why you need to go into the trouble of using complex JavaScript with waitForCondition while you can accomplish the same with one of the other selenium waitFor commands. The additional advantage of using waitForCondition is that you are able to declare a timeout value explicitly whereas the other waitFor commands simply honor the default selenium timeout value.</p>
<p><strong><i>Other useful variations:</i></strong><br />
You may have seen many AJAX intensive sites displaying  a ‘Loading..’ or ‘Saving..’ or ‘Processing…’ message when a button or a link is clicked. In such cases you may find this to be helpful to wait for the status text to change or disappear.</p>
<p>Assuming that a ‘Saving’ message is displayed within a div tag when a fictional ‘Save’ button is clicked in the snippet below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">waitForCondition</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;var value = selenium.getText(<span style="color: #000099; font-weight: bold;">\&quot;</span>//div[@class='save-text']<span style="color: #000099; font-weight: bold;">\&quot;</span>);value != <span style="color: #000099; font-weight: bold;">\&quot;</span>Saving<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;20000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In general, .Net sites that use the web form controls, panel controls etc, trigger an asynchronous postback everytime a form control is filled or a .Net tab or panel control is clicked etc. In such cases, I’ve found the following to be very helpful in waiting for the postback to complete.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">waitForCondition</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;selenium.browserbot.getUserWindow().Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() == false;&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now for the killer part, for sites that use jQuery, if all you need is to confirm there aren’t any active asynchronous requests, then the following does the trick:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">waitForCondition</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;selenium.browserbot.getUserWindow().$.active == 0&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, there are several possible variations to a single command. The possibilities are endless with Selenium.</p>
<p>Happy Testing !!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/03/selenium-tips-wait-with-waitforcondition/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Do you support functional tests for load testing?</title>
		<link>http://blog.browsermob.com/2011/02/do-you-support-functional-tests-for-load-testing/</link>
		<comments>http://blog.browsermob.com/2011/02/do-you-support-functional-tests-for-load-testing/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 17:15:03 +0000</pubDate>
		<dc:creator>Patrick Lightbody</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Functional Tests]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=906</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
While we support Selenium as our core API and Selenium is a popular functional testing toolkit, we do not currently support running tests written in languages like Java/C#/Ruby/Python/etc. There are two reasons we don&#8217;t support general unit test-style tests: First, Often these kinds of tests contain references to resources that aren&#8217;t accessible when running the <a href='http://blog.browsermob.com/2011/02/do-you-support-functional-tests-for-load-testing/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>
While we support Selenium as our core API and Selenium is a popular functional testing toolkit, we <strong>do not</strong> currently support running tests written in languages like Java/C#/Ruby/Python/etc. There are two reasons we don&#8217;t support general unit test-style tests:
</p>
<p>
<strong>First</strong>, Often these kinds of tests contain references to resources that aren&#8217;t accessible when running the test in the cloud. For example, we&#8217;ve seen tests that have hard coded URLs (ie: http://localhost:8080) and reference databases and web services to verify backend functionality.
</p>
<p>
We run 1000&#8242;s of Selenium instances in the cloud and so it is a hard requirement that the tests run &#8220;nearby&#8221; the browsers to be able to capture the best performance data. As such, these tests are pretty much impossible for us to allow to run from behind your firewall as if they were simple functional tests.
</p>
<p>
<strong>Secondly</strong>, and more importantly, functional tests are rarely written to work well as load tests or performance tests. A good performance test has two unique properties:
</p>
<ul>
<li><strong>Data parameterization</strong> &#8211; A good load test does not send the same data to the server repeatedly. Instead, it mixes the data up to reflect real world usage. Most functional tests have fixed data inputs specifically to test certain use cases, so these goals are at odds with each other. You can read more about parameterizations <a href="http://blog.browsermob.com/help/creating-a-test/parameterizing-your-tests/">here</a> and <a href="http://blog.browsermob.com/2010/10/parameterizing-your-tests-part-2/">here</a>.</li>
<li><strong>Performance timings</strong> &#8211; A good performance test also breaks up the functional interactions in to &#8220;steps&#8221; that are aggregated and charted, helping testers easily understand what pages and interactions are slow. A typical functional test has no concern (or even API!) to group parts of the test together.</li>
</ul>
<p>
Because of these reasons, we require that tests be written in a JavaScript. We still permit access to the Selenium API, but we also offer additional APIs that allow for things like data parameterization and capturing performance timings. We chose JavaScript because it&#8217;s a neutral language that just about everyone knows and understands.
</p>
<p>
We are continuing to evaluate whether we can support more traditional functional tests written in Java/C#/Ruby/Python/etc and we may change are policy on this matter in the future. But for now we encourage you to base new performance tests based on existing functional tests, but plan on making customizations to ensure you&#8217;re running a load test following all our recommended best practices!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/02/do-you-support-functional-tests-for-load-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expressions and Pattern Matching with BrowserMob and Selenium</title>
		<link>http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/</link>
		<comments>http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 23:35:32 +0000</pubDate>
		<dc:creator>Anu Sandhanam</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Load Testing Tips]]></category>
		<category><![CDATA[Monitoring]]></category>

		<guid isPermaLink="false">http://blog.browsermob.com/?p=872</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
Hello Readers, Welcome back!! This article intends to explain a few ways of using regular expressions with BrowserMob and Selenium. As we all know already, regular expressions are extremely helpful in scripting dynamic web sites, especially when you run into situations such as picking the first product from a dynamic list of products, clicking on <a href='http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top:40%;
left:60px;
z-index:1;
background-color:#F0F4F9}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;
width:55px;
height:60px;
padding-bottom:2px;
}


#bottomcontainerBox {
float:left;
height:30px;
width:100%;
background-color:#F0F4F9}

#bottomcontainerBox .buttons {
float:left;
height:30px;
width:85px;
margin:4px 4px 4px 4px;
}

</style>
<p>Hello Readers, Welcome back!!</p>
<p>This article intends to explain a few ways of using regular expressions with BrowserMob and Selenium. As we all know already, regular expressions are extremely helpful in scripting dynamic web sites, especially when you run into situations such as picking the first product from a dynamic list of products, clicking on the last link of a dynamic drop-down etc. </p>
<p>To begin with, let&#8217;s see a couple of examples for using regular expressions with BrowserMob&#8217;s VU (Virtual User) scripts. Not sure what VU is? Check it out <a href="http://blog.browsermob.com/2008/12/faq-what-are-virtual-users/" target="_blank">here</a> or contact <a href="http://browsermob.com/website-monitoring-load-testing-support" target="_blank">BroserMob Support</a> at anytime.</p>
<p><em><strong>Example 1:</strong></em><br />
<em>Single-line Regex</em> &#8211; In this example we are trying to find a match against a piece of content spanning across a single line, which is made easy with the BrowserMob method <a href="http://static.browsermob.com/api/com/browsermob/api/BrowserMob.html#findRegexMatches(java.lang.String,%20java.lang.String)" target="_blank">findRegexMatches</a></p>
<p>In the snippet below, we are trying to parse the first item under the &#8216;News&#8217; section on yahoo.com using single-line regex.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Yahoo Home'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> c.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://www.yahoo.com/'</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// single-line regex</span>
<span style="color: #006600; font-style: italic;">// getBody() returns the body of the HTTP response</span>
<span style="color: #003366; font-weight: bold;">var</span> matches <span style="color: #339933;">=</span> browserMob.<span style="color: #660066;">findRegexMatches</span><span style="color: #009900;">&#40;</span>response.<span style="color: #660066;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a class=<span style="color: #000099; font-weight: bold;">\&quot;</span>small<span style="color: #000099; font-weight: bold;">\&quot;</span> href=<span style="color: #000099; font-weight: bold;">\&quot;</span>(.*?)<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// logging for troubleshooting purposes</span>
browserMob.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> matches<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Follow the First News Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// go to the first new item parsed from the previous step</span>
<span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> c.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><em><strong>Example 2:</strong> </em><br />
<em>Multi-line Regex</em> &#8211; In this example we are trying to find a match against a piece of content spanning across multiple lines. The Javascript &#8216;RegExp&#8217; object comes handy in this instance. </p>
<p>In the snippet below, we are trying to parse the first hyperlink from html that spans across multiple lines as indicated below. As you can see, there are few newlines separating the &#8216;ul&#8217; and &#8216;li&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;menu&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;/website-load-testing&quot;</span><span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'BM Home'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> c.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://browsermob.com/performance-testing'</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// multi-line regexp</span>
<span style="color: #006600; font-style: italic;">// The regular expression uses '\s' which is any whitespace, including newline, OR \S which is anything NOT a white space</span>
<span style="color: #003366; font-weight: bold;">var</span> re <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&lt;ul class=&quot;menu&quot;&gt;[\s|\S]*?&lt;li&gt;&amp;lt;a href=\&amp;quot;(.*?)\&amp;quot;/i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> response.<span style="color: #660066;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myArray <span style="color: #339933;">=</span> re.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> myArray<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
browserMob.<span style="color: #660066;">beginStep</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Load Testing Home'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// 'item' is the url parsed from the previous step</span>
<span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> c.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://browsermob.com/'</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
browserMob.<span style="color: #660066;">endStep</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That was easy !! Now let&#8217;s look at a few ways of pattern matching with Selenium for <a href="http://blog.browsermob.com/2009/12/faq-whats-the-difference-between-a-real-browser-user-and-a-virtual-user/" target="_blank">BrowserMob&#8217;s RBU</a> scripts. </p>
<p>Selenium supports a few methods that help match text patterns. However, selenium locators don&#8217;t accept regular expressions. Only patterns or values accept them.</p>
<p><strong><em>Globbing:</em></strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link=glob:*Gifts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Clicks on any link with text suffixed with 'Gifts'</span>
selenium.<span style="color: #660066;">verifyTextPresent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;glob:*Gifts*&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><em><strong>Regular Expressions:</strong></em>[regexp, regexpi]</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;link=regexpi:^Over <span style="color: #000099; font-weight: bold;">\\</span>$[0-9]+$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//matches links such as 'Over $75', 'Over $85' etc</span></pre></div></div>

<p><em><strong>Contains:</strong></em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">highlight</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[contains(@class,'cnn_sectbin')]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//highlights the first div with class attribute that contains 'cnn_sectbin'</span>
selenium.<span style="color: #660066;">highlight</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div#cat_description:contains(<span style="color: #000099; font-weight: bold;">\&quot;</span>to last<span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//locating a div containing the text 'to last' using css selector</span></pre></div></div>

<p><em><strong>Starts-with:</strong></em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//img[starts-with(@id,'cat_prod_image')]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//clicks on the first image that has an id attribute that starts with 'cat_prod_image'</span>
selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[starts-with(@id,'tab_dropdown')]/a[last()]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//clicks on the last link within the div that has a class attribute starting with 'tab_dropdown'</span>
selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//div[starts-with(@id,'tab_dropdown')]/a[position()=2]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//clicks on the second link within the div that has a class attribute starting with 'tab_dropdown'</span>
selenium.<span style="color: #660066;">highlight</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div[class^='samples']&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//highlights div with class that starts with 'samples'</span></pre></div></div>

<p><em><strong>Ends-with:</strong></em></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">selenium.<span style="color: #660066;">highlight</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;css=div[class$='fabrics']&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//highlights div with class that ends with 'fabrics'</span>
selenium.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;//img[ends-with(@id,'cat_prod_image')]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//clicks on the first image that has an id attribute that ends with 'cat_prod_image'</span></pre></div></div>

<p>[<strong>Note:</strong> ends-with is supported only by Xpath 2.0. FF 3 might throw an error for this.]</p>
<p>Happy Testing !!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.browsermob.com/2011/02/regular-expressions-and-pattern-matching-with-browsermob-and-selenium/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced)

Served from: blog.browsermob.com @ 2012-02-07 01:45:43 -->
