Stap 6: Watir-Webdriver Cheat Sheet
Hier is een lijst van de meest gebruikte Watir-Webdriver functie en hun toepassingen. Deze stap kan fungeren als een quick reference guide voor het geval u vergeten wat een methode wordt aangeroepen of wat het voor werd gebruikt. Deze stap zal ook de methoden die werden niet behandeld in dit instructable maar zijn gemakkelijk te achterhalen volgens hun naam. Voor meer gedetailleerde informatie kunt u de rubydoc-website vindt u alle informatie met betrekking tot Watir-Webdriver (http://www.rubydoc.info/gems/watir-webdriver/0.6.11/Watir)
Browser-object:
#Setup the browsers driver and create the browser #driver = Selenium::WebDriver.for :firefox<br>#driver = Selenium::WebDriver.for :chrome #driver = Selenium::WebDriver.for :ie browser = Watir::Browser.new(driver) #Setup misc browser settings browser.driver.manage.timeouts.page_load = 30 browser.window.resize_to(1000, 600) browser.window.move_to(0, 0) #Interact with the browser browser.goto("http://website.com") browser.refresh browser.quit
TextBox-object:
#Collections browser.text_fields(:id => "text") #Set value browser.text_field(:id => "text").set("This is text") #Get value browser.text_field(:id => "text").value #Clear value browser.text_field(:id => "text").clear
Button, object:
#Collections browser.buttons(:id => "button") #Check if button is enabled browser.button(:id => "button").enabled? #Get buttons text browser.button(:id => "button").text #Click on the button browser.button(:id => "button").click
CheckBox-object:
#Collections browser.checkboxes(:id => "checkbox") #Check the checkbox browser.checkbox(:id => "checkbox").set browser.checkbox(:id => "checkbox").set(true) #Uncheck the checkbox browser.checkbox(:id => "checkbox").clear browser.checkbox(:id => "checkbox").set(false) #Check if checkbox is checked browser.checkbox(:id => "checkbox").set?
ListBox-object:
#Collections browser.select_lists(:id => "list") #Select from list using text name browser.select_list(:id => "list").select("var") #Select from list using value browser.select_list(:id => "list").select_value("var2") #Check if value is selected browser.select_list(:id => "list").selected?("var2") #Get current selected value puts browser.select_list(:id => "list").value #Iterate through all values and print them browser.select_list(:id => "list").options.each do |i| puts "#{i.text}" end
Radio-object:
#Collections browser.radios(:id => "radio") #Select the button browser.radio(:id => "radio").set #Check is button is selected browser.radio(:id => "radio").set?
Image-object:
#Collections browser.images(:src => "img.gif") #Check if image is loaded browser.image(:src => "img.gif").loaded? #Get the image height browser.image(:src => "img.gif").height #Get the image width browser.image(:src => "img.gif").width #Click on the image browser.image(:src => "img.gif").click #Click on the first image loaded on the page browser.images[0].click
Div-object:
#Collections browser.divs(:class => "body") #Get div text browser.div(:class => "body").text #Get text of second div when it appears browser.divs[1].when_present.text
Table-object:
#Collections browser.tables(:id => "table") #Get row 1, col 1 text browser.table(:id => "table")[0][0].text #Get row 1, col 2 text (alternitive) browser.table(:id => "table").tr{0}.cell{1}.text #Get row 2 entire text puts browser.table(:id => "table")[1].text #Click on row 4 puts browser.table(:id => "table")[3].click #Get column count browser.table(:id => "table").row.cells.length #Get row count browser.table(:id => "table").row_count browser.table(:id => "table").rows.length
Algemene
#[exists?] - See if object exists browser.text_field(:id => "text").exists? #[enabled?] - See if object is enabled browser.select_list(:id => "list").enabled? #[present?] - See if object is present browser.element(:id => "e").present? #[tag_name] - Return objects tag name browser.element(:id => "e").tag_name #[screenshot] - Save screenshot of current page browser.screenshot.save("c:\\page.png") #[to_subtype] - Returns HTML object browser.element(:id => "button").to_subtype #[index] - Click 2nd image on page browser.image(:index => 1).click # [loops] - Get names of all text-fields browser.text_fields.each do |i| puts i.name end #Get the name of first text-field puts browser.text_fields[0].name #Get the name of second text-field puts browser.text_fields[1].name
Wachten
#[wait_until_present] - Do nothing until object is present browser.button(:id => "button").wait_until_present #[when_present] - Only do something when object is present browser.button(:id => "button").when_present.click browser.button(:id => "button").when_present(10).click #[wait_while_present] - Do nothing while object is present browser.button(:value => "submit").click browser.button(:value => "submit").wait_while_present