selenium
DESCRIPTION
These are the slides I used to introduce one of my classes to web automation using SeleniumTRANSCRIPT
![Page 2: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/2.jpg)
Lecture Objective
Give students an introduction to web automation using the Selenium framework.
![Page 3: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/3.jpg)
What is Selenium?
• Selenium is a web test tool that runs in the browser
• Because it runs in the browser, it does exactly what a user does
![Page 4: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/4.jpg)
What tests can Selenium do?
• Browser compatibility – One script, many browsers
• Regression
![Page 5: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/5.jpg)
Javascript
• Selenium is written in Javascript
• Javascript is how AJAX applications are written, so Selenium can test them too
![Page 6: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/6.jpg)
Where to get it?
Selenium can be downloaded and installed for free from http://www.openqa.org
![Page 7: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/7.jpg)
QTP vs. Selenium
• QTP is not cross platform, Selenium is• QTP costs a lot of money, Selenium is free• QTP needs VBScript, Selenium has lots of
language bindings• The default format of testing in Selenium is
HTML
• QTP can control other types of applications other than web
![Page 8: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/8.jpg)
Multiple Seleniums?
There are 3 versions of Selenium
• Selenium Core – The main component of Selenium
• Selenium RC – A scripting layer over Selenium Core
• Selenium IDE – a Firefox extension with record / playback functionality
![Page 9: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/9.jpg)
Selenium IDE
• Selenium IDE adds a layer of Record / Playback to Selenium
• Is available for Firefox only
![Page 10: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/10.jpg)
First Script
Command Target Value
open /jobmining/
type queryTitle qa
select ct_category label=Banking
clickAndWait Submit01
clickAndWait link=sqa
![Page 11: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/11.jpg)
Checkpoints
Of course, scripts wouldn’t be tests if they didn’t check something
• assert* tests fail the test immediately• verify* tests keep track of results and continue
the script regardless
verifyTextPresent Job Description \n dgds
asserTextPresent Job Requirements \n sdrfasf
![Page 12: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/12.jpg)
Locators
Selenium identifies what a component is through the use of a locator
• link=name• dom=document.images[56]• xpath=//table[@id='table1']//tr[4]/td[2]• css=a[href="#id3"]
Depending on your application, there might be major performance differences
![Page 13: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/13.jpg)
Playback
Playback of a single script is handled through the IDE
• Run – Go as fast as the script can process
• Walk – Slows down the execution
• Step – Executes the next step
![Page 14: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/14.jpg)
Test Suites
In order to run multiple scripts, you need to chain them together in a Test Suite
• Just another html table
• Runs inside Firefox, but not in S-IDE
• Saved in the same directory as the tests that are included in it
![Page 15: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/15.jpg)
Test Suites
<table> <tr> <td>Job Search test suite</td> </tr> <tr> <td><a target="testFrame" href=“selenium-ide-
01.html">Job Search</a></td> </tr></table>
![Page 16: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/16.jpg)
Test Suites
The URL has a specific format
chrome://selenium-ide/content/selenium/TestRunner.html?baseURL=http://your_hose:port&test=file:///c://temp/qa109/testsuite.html&auto=true&multiWindow=false
![Page 17: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/17.jpg)
Selenium RC
Selenium IDE is great for quick recording of tests, but it somewhat lacks for power
Selenium RC gives you the ability to drive Selenium from a real programming language (Java, Perl, Python, Ruby, and more)
![Page 18: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/18.jpg)
Why do you want a real language?
By using Selenium inside a full fledged language you can do the following
• Seed the database• Check the database• Control external services• Launch multiple windows• Run multiple browsers in parallelIn addition to running the actual test.
![Page 19: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/19.jpg)
Proxy
Because the commands for Selenium RC are embedded in a script, a proxy is needed to control the browser.
![Page 20: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/20.jpg)
Python
There are python bindings for most of the Selenium calls
• Watch for naming differences
• While not necessary, most use the unittest module with Selenium
![Page 21: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/21.jpg)
import selenium, unittest
class JM(unittest.TestCase):
def setUp(self):
protocol = "http"
host = "your host"
port = your_port_number
self.verificationErrors = []
self.selenium = selenium.selenium("localhost", 4444, "*chrome", "%s://%s:%s" % (protocol, host, port)) self.selenium.start()
self.selenium.open("/jobmining/")
def test_doSearch(self):
sel = self.selenium
sel.open("/jobmining/")
sel.type("queryTitle", "qa")
sel.select("ct_category", "label=Banking")
sel.click("Submit01")
sel.wait_for_page_to_load("30000")
sel.click("link=sqa")
sel.wait_for_page_to_load("30000")
try:
self.failUnless(sel.is_text_present("Job Description \n dgds"))
except AssertionError, e:
self.verificationErrors.append(str(e))
def tearDown(self):
self.selenium.stop()
if __name__ == "__main__":
unittest.main()
![Page 22: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/22.jpg)
Data Driven
• One key concept when doing automation is to recycle your scripts through data driving them
• Use the underlying language you are using Selenium RC with to handle most of it for you
![Page 23: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/23.jpg)
Same Origin
Prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin – Mozilla security documentation
In other words, cannot work across server boundries
![Page 24: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/24.jpg)
Selenium Core
• Selenium Core is used by both Selenium IDE and RC
• Runs test suites on the same server to avoid the Same Origin problem
• Don’t have same flexibility as RC, but tests and code under test is in the same spot
![Page 25: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/25.jpg)
Tips
• Start and stop your script from the same spot
• Record your script in S-IDE, then use it as a base for a S-RC script
• Use Firebug to give you the XPath
![Page 26: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/26.jpg)
Designing for Selenium
• Proper use of tables and CSS div tags makes Selenium much easier
![Page 27: Selenium](https://reader036.vdocuments.site/reader036/viewer/2022062510/54b6013b4a795954558b4656/html5/thumbnails/27.jpg)
Support
Because this is free, open source software there is no official support channel
• Mailing lists• Wiki• ForumsAre your main sources of assistence.
But don’t forget your peers!