You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<html>
<head>
<title>Testing with Ruby and Selenium WebDriver</title>
</head>
<body bgcolor="antiquewhite">
<img src="images/freebsd_daemon.jpg">
<h2>
Version 1.0</h2><h1>
Test Automation Training</h1><h3>
Testing Web Applications with Ruby and Selenium WebDriver</h3></body>
</html>
Ruby Code:
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page1"# Timeout = 15 secwait=Selenium::WebDriver::Wait.new(:timeout=>15)# Find text on the page by regexpputs"Test Passed: Page 1 Validated"ifwait.until{/Testing Web Applications with Ruby and Selenium WebDriver/.match(browser.page_source)}browser.quit
Locators
Web Page
<html>
<head>
<title>Testing with Ruby and Selenium WebDriver</title>
</head>
<body bgcolor="antiquewhite">
<img src="images/freebsd_daemon.jpg">
<h2>
Version 1.0</h2><h1>
Test Automation Training</h1><h3>
Testing Web Applications with Ruby and Selenium WebDriver</h3></body>
</html>
Ruby Code
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page1"# Timeout = 15 secwait=Selenium::WebDriver::Wait.new(:timeout=>15)# Find text on the page by regexpputs"Test Passed: Page 1 Validated"ifwait.until{/Testing Web Applications with Ruby and Selenium WebDriver/.match(browser.page_source)}browser.quit
Forms
Web Page
<html>
<head>
<title>Testing with Ruby and Selenium WebDriver</title>
</head>
<body bgcolor="antiquewhite">
<img src="images/freebsd_daemon.jpg">
<h2>
Version 1.0</h2>
<h1>
Test Automation Training</h1><h3>
Testing Web Applications with Ruby and Selenium WebDriver</h3><p>
Enter something interesting in the text box and click a button!</p><form name="input" action="page1.html" method="post">
<p>
<input type="text" name="searchbox"></p><p>
<input type="submit" name="submit" value="submit"></p></form></body>
</html>
Ruby Code
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page3"wait=Selenium::WebDriver::Wait.new(:timeout=>15)# Add text to a text boxinput=wait.until{element=browser.find_element(:name,"searchbox")elementifelement.displayed?}input.send_keys("Information")# Check that the form existsform=wait.until{element=browser.find_element(:name,"input")elementifelement.displayed?}puts"Test Passed: Form input found"ifform.displayed?# Click the button based the form it is in (you can also call 'submit' method)form.find_element(:name,"submit").clickbrowser.quit
Images
Web Page
<html>
<head>
<title>Images</title>
</head>
<body bgcolor="lightsteelblue">
<img src="images/freebsd_daemon.jpg">
<h2>Version 1.0</h2>
<h1>We are testing images...</h1><h3>Testing Web Applications with Ruby and Selenium WebDriver</h3>
<img name="watergraphic" src="images/WaterFaucet.jpg" alt="Image of water faucet"/>
<a href="page1.html"><img alt="Image Button" src="images/totoro.gif"/></a>
</body>
</html>
Ruby Code
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page4"wait=Selenium::WebDriver::Wait.new(:timeout=>15)# Check that the image exists using different attributes and xpathputs"Test Passed: Found a graphic with the path 'images/WaterFaucet.jpg' in the source filename"ifwait.until{browser.find_element(:xpath=>"//img[@src='images/WaterFaucet.jpg']").displayed?}puts"Test Passed: Found a graphic with the name watergraphic"ifwait.until{browser.find_element(:name,"watergraphic").displayed?}puts"Test Passed: Found a graphic with 'Image of water faucet' as alt text"ifwait.until{browser.find_element(:xpath=>"//img[@alt='Image of water faucet']").displayed?}# Click the image buttonimage=wait.until{element=browser.find_element(:xpath=>"//img[@src='images/totoro.gif']")elementifelement.displayed?}image.click# Check the text on the new pageputs"Test Passed: Page 1 Validated"ifwait.until{/Test Automation Training/.match(browser.page_source)}browser.quit
Checkboxes
Web Page
<html>
<head>
<title>Testing with Ruby and Selenium WebDriver</title>
</head>
<body bgcolor="cadetblue">
<img src="images/freebsd_daemon.jpg">
<h2>Version 1.0</h2>
<h1>Test Automation Training</h1><h3>Testing Web Applications with Ruby and Selenium WebDriver</h3>
<form action="page1" method="post">
<p><Input type="checkbox" name="checkthebox"></p> <p><Input type="submit" value="submit"></p></form>
</body>
</html>
Ruby Code
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page5"wait=Selenium::WebDriver::Wait.new(:timeout=>15)cb=browser.find_element(:name,"checkthebox")# Check that the checkbox existscb=wait.until{element=browser.find_element(:name,"checkthebox")elementifelement.displayed?}puts"Test Passed: The check box exists"ifcb# Check the checkboxcb.click# Verify the state of checkboxputs"Test Passed: The check box is selected now"ifcb.selected? == truesleep2# Un-Check the checkboxcb.click# Verify the state of checkbox againputs"Test Passed: The check box is unselected now"ifcb.selected? == falsebrowser.quit
Radio Buttons
Web Page
<html>
<head>
<title>Testing with Ruby and Selenium WebDriver</title>
</head>
<body bgcolor="antiquewhite"">
<img src="images/freebsd_daemon.jpg">
<h2>Version 1.0</h2>
<h1>Test Automation Training</h1><h3>Testing Web Applications with Ruby and Selenium WebDriver</h3>
<form action="page1" method="post">
<p>Option 1<Input type="radio" name="radiobutton1"></p> <p>Option 2<Input type="radio" name="radiobutton2"></p> <p>Option 3<Input type="radio" name="radiobutton3"></p> <p><input type="submit" value="submit"></p></form>
</body>
</html>
Ruby Code
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page6"wait=Selenium::WebDriver::Wait.new(:timeout=>15)#### Check that the radio button existsputs"Test Passed: Radio button found"ifwait.until{browser.find_element(:name,"radiobutton1").displayed?}#### Change the state of the Radio Buttonscb1=wait.until{element=browser.find_element(:name,"radiobutton1")elementifelement.displayed?}cb1.clickifcb1.selected? == falsecb3=wait.until{element=browser.find_element(:name,"radiobutton3")elementifelement.displayed?}cb3.clickifcb3.selected? == falsebrowser.quit
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page7"wait=Selenium::WebDriver::Wait.new(:timeout=>15)#### Interact with the drop down boxselect_list=wait.until{element=browser.find_element(:name,"dropdown")elementifelement.displayed?}select_list.clear#### Extract all options from the select boxoptions=select_list.find_elements(:tag_name=>"option")#### Select the option "Volvo"options.eachdo |g|
ifg.text == "Volvo"g.clickbreakendend#### Select another option "Audi"options.eachdo |g|
ifg.text == "Audi"g.clickbreakendend#### Print currently selected optionsoptions.eachdo |g|
ifg.selected?putsg.textendendbrowser.quit
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page8"wait=Selenium::WebDriver::Wait.new(:timeout=>15)#### Check that the table with the given id is displayedtable=wait.until{element=browser.find_element(:id,"booktable")elementifelement.displayed?}puts"Test Passed: Book Table found"iftable#### Print full table contentsputs"Table with id=booktable:\n" + table.text#### Iterate through all cells of the tablebrowser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr/td").eachdo |r|
puts"Cell Value: " + r.textend#### Print the value from the 1'st column of the 2'nd row of the tableputs"Value from the 1'st column of the 2'nd row of the table: " + browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[1].text.split(' ')[0]#### Print the value from the 2'nd column of the 1'st row of the tableputs"Value from the 2'nd column of the 1'st row of the table: " + browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[0].text.split(' ')[1]browser.quit
Executing JavaScript code on the page. Handling popup dialogs.
Web Page
<script type="text/javascript">
function enter_name()
{
var name=prompt("Please enter your name","Artem");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
window.stop();
}
}
function enter_age()
{
var age=prompt("Please enter your age","27");
if (age!=null && age!="")
{
document.write("You are " + age + " years old.");
window.stop();
}
}
</script>
<input type="button" onclick="enter_name()" value="Name">
<p><input type="button" onclick="enter_age()" value="Age">
</p>
Ruby Code
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page9"wait=Selenium::WebDriver::Wait.new(:timeout=>15)# Execute 1st JavaScript function on the page and Cancel the prompt boxbrowser.execute_script("enter_name()")a=browser.switch_to.alertifa.text == 'Please enter your name'a.dismisselsea.acceptend# Execute 2nd JavaScript function on the page and Accept the prompt boxbrowser.execute_script("enter_age()")a=browser.switch_to.alertifa.text == 'Please enter your name'a.dismisselsea.send_keys("99")a.acceptend# Find the age on the page by regexpputs"Test Passed: Page 9 Validated"ifwait.until{/99/.match(browser.page_source)}browser.get"http://localhost/page9"# Execute any custom JavaScript codeputsbrowser.execute_script("return window.location.pathname")browser.quit
require'rubygems'require'selenium-webdriver'browser=Selenium::WebDriver.for:firefoxbrowser.get"http://localhost/page10"wait=Selenium::WebDriver::Wait.new(:timeout=>15)# Switch to the 1st framebrowser.switch_to.frame("frame1")# Find text in the 1st frame by regexpputs"Test Passed: Frame 1 Validated"ifwait.until{/An operating system is the set of basic programs and utilities that makeyour computer run/.match(browser.page_source)}# Switch to parent pagebrowser.switch_to.default_content# Switch to the 2nd framebrowser.switch_to.frame("frame2")# Find text in the 2nd frame by regexpputs"Test Passed: Frame 2 Validated"ifwait.until{/Welcome to the Linux Kernel Archives/.match(browser.page_source)}# Switch to parent pagebrowser.switch_to.default_content# Switch to the 3rd framebrowser.switch_to.frame("frame3")# Find text in the 3rd frame by regexpputs"Test Passed: Frame 3 Validated"ifwait.until{/Ubuntu is, and always will be, absolutely free/.match(browser.page_source)}browser.quit
Test/Unit
Ruby Code
require'rubygems'require'selenium-webdriver'require'test/unit'classExampleTestCase < Test::Unit::TestCase#### Starting browser before each testdefsetup@browser=Selenium::WebDriver.for:firefox@browser.get"http://localhost/page8"@wait=Selenium::WebDriver::Wait.new(:timeout=>15)end#### Closing browser after each test defteardown@browser.quitend#### Check that the table is theredeftest_table_existenceassert@wait.until{@browser.find_element(:id,"booktable").displayed?}end#### Checking values in the table cellsdeftest_table_values##### Waiting for the values to appearassert@wait.until{@browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[0].text.split(' ')[0] == "fraise"}assert@wait.until{@browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[0].text.split(' ')[1] == "orange"}##### Not waiting for the values to appear, assuming they are already visibleassert_equal(@browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[1].text.split(' ')[0],"vin")assert_equal(@browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[1].text.split(' ')[1],"eau")assert_not_equal(@browser.find_elements(:xpath=>"//table[@id='booktable']/tbody/tr")[1].text.split(' ')[0],"fake")endend
Test Suite
Ruby Code
#### Test suiterequire'rubygems'require'test/unit'#### Below you can include any number of files to your test suiterequire'example_testunit'