Thursday, April 5, 2012

SQL to find items that have more than one row

Query examples to find items that have more than one row in a table: SELECT department, COUNT(*) as "Number of employees" FROM employees WHERE salary > 25000 GROUP BY department HAVING COUNT(*) > 10; SELECT key FROM table group by key having COUNT(*) > 1

Tuesday, March 20, 2012

Javascript to strip or replace characters

In Selenium IDE, I wanted to verify one amount was greater than another. The amounts were in dollar/comma format ($3,400.00), so I needed to strip the dollar sign and comma before comparing the values.

Strip the dollar sign from variable1:
storeEval storedVars['variable1'].replace(/\$/g,'') dx

Strip the comma from 'dx' (created when stripping the dollar sign in previous step):
storeEval storedVars['dx'].replace(/\,/g,'') x

Verify that 'x' is greater than 'y'
verifyEval storedVars['x'] > storedVars['y'] true

Monday, March 5, 2012

C# - Taking Screenshots

Here's some code I'm using to take screenshots in my Selenium tests:
            // Get Screenshot
            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            string screenshot = ss.AsBase64EncodedString;
            byte[] screenshotAsByteArray = ss.AsByteArray;
            ss.SaveAsFile("test.jpg", ImageFormat.Jpeg);

The file is saved to the bin\debug folder for your VS project.

I think you only need to add the System.Drawing reference, and add this to the class:
using System.Drawing;
using System.Drawing.Imaging;

Javascript to get current date: m/d/yyyy

I used the following javascript in Selenium IDE to get the current date in the format m/d/yyyy (ex: 2/5/2012 or 12/10/2012).  No zero's preceed the month and day.

storeEval var d=new Date(); ((d.getMonth()+1))+'/'+d.getDate()+'/'+d.getFullYear(); date1

Selenium IDE commands that don't export to C# WebDriver

I'm working on a Selenium 2 automation framework in C#.  After recording scripts in IDE, I export them to C# (WebDriver).  Some IDE commands will not export to C#, and you end up with messages like this:
// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]

So, obviously verifyTextPresent and assertTextPresent do not export.  I plan to figure out the best way to code this in C# because "verifyTextPresent" is useful in things like smoke scripts, where I don't want the script to fail easily because a page element moved or was renamed.

If you're using the flowControl extension, "gotoIf" and "label" commands do not export; but that's not really a big deal.

getEval and storeEval do not export.  Sometimes I use these javascript commands to get the current date or generate a random value.  Again no big deal.

These are the only ones I've run into so far.

Selenium IDE Conditional commands

Using the flowControl extension, you can include conditional commands in Selenium IDE.  The conditional statements do not export to WebDriver (Selenium 2) C# scripts; but it's still a very handy tool for IDE.

I used javascript to define conditions.  In the following example, if the variable, aVar, is greater than 550, the script moves to the line that is labeled 'target1'.  If the variable is 550 or less, the verifyText command executes.

gotoIf storedVars.aVar > 550 target1
verifyText css=span.amount $500.00
label target1

Here's an example of javascript checking if the variable is less than 550 or greater than 599, the script skips to the line labeled "target2". If the variable is between 550 and 599, the verifyText command executes.

gotoIf storedVars.aFico < 550 || storedVars.aFico > 599 target2
verifyText css=span.current-amount $500.00
label target2

One thing to keep in mind is that each "label" value has to be unique throughout the script. In other words, you cannot have two "target1" labels.

Selenium IDE Data-driven testing

You can do data-driven testing using Selenium IDE data driven extension.  However, your data must be in an XML file, and the data must be defined in attributes of the "test" element.  It is possible to create an XML file from an Excel file (just look it up in MS Excel Help); however, the data is imported as elements, not attributes.