Annotated slides of the lightning talk I gave at the 2011 selenium conference. The transcript of the poem is included in boxes at the bottom of each slide. The audio/video can be seen on YouTube: http://www.youtube.com/watch?v=5HmvzaLodL0
Seconf2011 - annotated lightning talk - a smattering of selenium tips
1. Tips
A Poem By
Aaron Silverman
YouTube Video: http://www.youtube.com/watch?v=5HmvzaLodL0 (0:10 – 3:10)
Like Patrick kindly did last evening, I want to take advantage of being on the soapbox and share some tips I found useful and
that I think will help you all out. So, borrowing from the official selenium blog title, I present you a smattering of selenium
tips: A poem about lessons learned.
2. XPATH <
Locating elements by XPATH is one of the deadly selenium sins
It is even worse, and slower, than using Windows ME
But if you need to traverse up and down the DOM you cannot win
You will have to use XPATH, but proceed carefully!
3. Complicated Page With Dynamic Content
ID = my_parent
BUTTON
#my_parent .orange_star
What we found works best is to use XPATH only one time
Find a useful parent element and then get or assign it an ID
Use this ID to reference child nodes without the XPATH crime
Your tests will be faster and more maintainable as you will see
4. Issue Threat Level Solution
Velociraptor 3 Shotgun
XPATH 7 CSS
Delayed Flight 5 Beer
[[‘Velociraptor’, ‘3’, ‘Shotgun’],[‘XPATH’,’7’,’CSS’],[‘Delayed Flight’,’5’,’Beer’]]
Testing Code
Often we validate the contents of an entire table—which can be a lot!
We found that grabbing the contents cell by cell is way too slow
Instead use JavaScript injection to grab and return the contents in one shot
Returning a JSON 2D array of the table contents is the way to go
5. Complicated Page With Dynamic Content
Name: Robert’) DROP TABLE students;--
Quest:
Favorite Color:
JavaScript injection can helps in many other situations too!
Like randomly filling in every input on the page as fast as lightning
Users love to break things so I think this is something you should do
The fact that somebody may be named Robbert’); Drop Table Students is quite frightening!
6. Complicated Page With Dynamic Content
Am I visible?
(Invisible Element)
Of Course!
Display: none?
Class: hidden?
LIAR!
I am not a crook!
Often you want to know if an element is visible, which makes sense
But visible(), like Nixon, will sometimes just lie to your face
We use JavaScript injection to traverse the style or class attributes of parents
Combined with a regex or two, we determine visibility with grace
7. Oh no!
Complicated Page With Dynamic Content
Selenium tries its best, but in some cases like a deer in headlights it may freeze
To prevent everything from hanging we run commands in a separate thread
If the join timeout is reached, we the log the error and end the test as we please
Allowing our teardown to properly run, instead of the whole execution being dead
8. Testing Code
The test did this
And then this
And then this
Abrah
Cadabra!
Log everything you can about each run in the database!
Inputs, outputs, Log statements, and screenshots are what engineers crave
You can always delete information you no longer need, when that’s the case
But only David Copperfield can produce information you did not save
9. Testing Code
Click
Screenshot
Upload Screenshot to DB
Validate
Click
Type
Click
Speaking of screenshots, they are an engineer’s best friend—they are divine!
But sending them over the series of tubes can sometimes be quite slow
We found that having your test wait for them to upload is a sub-par design
Use a separate thread to upload, and letting your test proceed, is the way to go