Skip to content

Instantly share code, notes, and snippets.

@pacifists
Created June 21, 2012 13:59
Show Gist options
  • Save pacifists/2965902 to your computer and use it in GitHub Desktop.
Save pacifists/2965902 to your computer and use it in GitHub Desktop.
How to use selenium web driver few example files of my own
# encoding: utf-8
require "selenium-webdriver"
require "net/http"
require 'pathname'
require 'json'
driver = Selenium::WebDriver.for :chrome, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate]
driver.navigate.to "http://localhost:3000/admin"
login_email = driver.find_element(:id, 'admin_email')
login_email.send_keys "rytis@avereast.lt"
login_psw = driver.find_element(:id, 'admin_password')
login_psw.send_keys 'SesiPaciai'
login_psw.submit
driver.navigate.to "http://localhost:3000/admin/content_types/questions/contents"
designs = driver.find_elements(:css, 'li.content strong a')
design_urls = []
designs.each do |d|
design_urls << d.attribute(:href)
end
results = []
design_urls.each do |p|
driver.navigate.to p
item = {}
item[:description] = driver.find_element(:id, 'content_description').text
item[:title] = driver.find_element(:id, 'content_title').attribute(:value)
item[:visible] = driver.find_element(:id, 'content_visible').attribute(:value)
results << item
end
File.open("duk_data.json", "w") do |f|
f.write(results.to_json)
end
puts driver.title
driver.quit
# encoding: utf-8
require "selenium-webdriver"
require "json"
require 'pathname'
driver = Selenium::WebDriver.for :chrome, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate]
# driver.file_detector = lambda do |args|
# # args => ["/path/to/file"]
# str = args.first.to_s
# str if File.exist?(str)
# end
puts "start navigate"
driver.navigate.to "http://nemokamos.dev/locomotive"
login_email = driver.find_element(:id, 'locomotive_account_email')
login_email.send_keys "rytis@avereast.lt"
login_psw = driver.find_element(:id, 'locomotive_account_password')
login_psw.send_keys 'SesiPaciai'
puts "logging in"
login_psw.submit
json = File.read('data.json')
designs = JSON.parse(json)
puts "starting to insert designs"
designs.each do |d|
sleep(2)
driver.navigate.to "http://nemokamos.dev/locomotive/content_types/designs/entries/new"
sleep(2)
driver.find_element(:id, 'content_entry_title').send_keys(d['title'])
driver.find_element(:id, 'content_entry_number').send_keys(d['number'])
# option = Selenium::WebDriver::Support::Select.new(driver.find_element(:id, 'content_entry_category_id'))
# option.select_by(:text, d[:category_id])
file_path = File.join( Dir.pwd, 'designs', d['image'])
# driver.find_element(:id, 'content_entry_image').click
driver.find_element(:id, 'content_entry_image').send_keys(file_path)
driver.find_element(:id, 'content_entry_target_url').send_keys(d['target_url'])
driver.find_element(:id, 'content_entry_title').click
# driver.find_element(:class, 'switchArea').click
sleep(1.5)
driver.find_element(:name, 'commit').submit
sleep(2)
end
puts driver.title
driver.quit
# encoding: utf-8
require "selenium-webdriver"
require "net/http"
require 'pathname'
require 'json'
driver = Selenium::WebDriver.for :chrome, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate]
driver.navigate.to "http://localhost:3000/admin"
login_email = driver.find_element(:id, 'admin_email')
login_email.send_keys "rytis@avereast.lt"
login_psw = driver.find_element(:id, 'admin_password')
login_psw.send_keys 'SesiPaciai'
login_psw.submit
driver.navigate.to "http://localhost:3000/admin/content_types/designs/contents"
designs = driver.find_elements(:css, 'li.content strong a')
design_urls = []
designs.each do |d|
design_urls << d.attribute(:href)
end
results = []
design_urls.each do |p|
driver.navigate.to p
item = {}
item[:title] = driver.find_element(:id, 'content_title').attribute(:value)
item[:number] = driver.find_element(:id, 'content_number').attribute(:value)
item[:target_url] = driver.find_element(:id, 'content_target_url').attribute(:value)
# item[:category] = driver.find_element(:id, 'content_category').get_selected_value
select = Selenium::WebDriver::Support::Select.new(driver.find_element(:id, 'content_category'))
select.options.each do |o|
if o.attribute(:selected)
item[:category] = o.text
end
end
# image
image = driver.find_element(:css, '.file .remove strong a').attribute('href')
filename = Pathname.new(image).basename
# system "wget -O designs/#{filename} #{image}"
item[:image] = filename
puts item
results << item
end
File.open("data.json", "w") do |f|
f.write(results.to_json)
end
puts driver.title
driver.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment