Skip to content

Instantly share code, notes, and snippets.

@motoroller95
Created November 8, 2017 10:47
Show Gist options
  • Save motoroller95/3ee601b3257c5cc182a6735b89ec6c71 to your computer and use it in GitHub Desktop.
Save motoroller95/3ee601b3257c5cc182a6735b89ec6c71 to your computer and use it in GitHub Desktop.
class MailParser
delegate :within_window, :within_frame, :find, :all, :window_opened_by, :page, :click_link, :first, to: :@capybara
def initialize(window, capybara)
@window = window
@capybara = capybara
@users_count = 0
@current_idx = 0
end
def call
loop do
within_window @window do
wait_element('frame')
if @current_idx != 0
page.evaluate_script('window.history.back()')
page.evaluate_script('window.history.back()')
page.evaluate_script('location.reload(true)')
end
@exchange_exists = within_frame(0) do
wait_element('#wrapper')
if link = first('#tmiexchange_web_emails a')
link.click
true
else
break
end
end
if @exchange_exists
wait_element('frame')
@user_id = nil
puts "#{@current_idx} ========================================================================="
within_frame(1) do
wait_element("#global_list tr:nth-child(#{@current_idx + 2}) a")
@users_count = all('#global_list tr:not(:first-child)').count
first("#global_list tr:nth-child(#{@current_idx + 2}) a").click
find('#fieldControl_cell1 a').click
@user_id = find('#title').text.scan(/.*\(ID= (\d+)\)/)[0][0]
@cp = window_opened_by { find('#ui_row_2 a').click }
end
end
end
if @cp
@password = get_password
data = [@user_id, @password]
system("echo '#{data.inspect}' >> result.txt")
end
@current_idx += 1
break unless @current_idx < @users_count
end
within_window(@window) { page.execute_script('window.close()') }
end
private
def wait_element(element)
while page.all(element).length.zero?
sleep 1
end
end
def get_password
within_window @cp do
wait_element('frame')
within_frame(0) { click_link('Почтовый ящик Exchange') }
wait_element('frame')
within_frame(1) { click_link('Общие') }
wait_element('frame')
owa_link = within_frame(1) { find('#exchange_owa_link')['onclick'] }
match_data = owa_link.match(/username=.*&password=(.*?)&.*$/)
page.execute_script('window.close()')
match_data[1]
end
end
end
require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'active_support/core_ext/module/delegation'
require_relative './mail_parser'
require 'byebug'
Capybara.run_server = false
Capybara.default_driver = :selenium
Capybara.app_host = 'https://cp.active.by'
Capybara.javascript_driver = :selenium
Capybara.ignore_hidden_elements = false
Capybara.default_wait_time = 10
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
Capybara::Selenium::Driver.new(app, profile: profile)
end
module MyCapybaraTest
class Test
include Capybara::DSL
def process(params)
sign_in(params[0], params[1])
credits = []
subscriptions_url = nil
within_frame(0) { click_link('to_bm') } # go to billing
within_frame(1) do
click_link('click_operational_director') # open menu item 'Operation director'
click_link('click_support_manager') # open support manager menu item
subscriptions_url = find('#click_subscriptions')['href']
end
visit(subscriptions_url) # go to subscriptions
current_page = 1
loop do
idx = 2
puts "PAGE: #{current_page} +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
loop do
wait_element('#global_list tr')
subscription = first("#global_list tr:nth-child(#{idx}) a")
break unless subscription
puts "#{subscription.text} ========================================================================="
subscription.click
wait_element('#AccountAccountID')
find('#AccountAccountID').click
if page.first('body', text: 'To Control Panel').nil?
puts "No CP link for ##{subscription.text}"
idx += 1
page.evaluate_script('window.history.back()')
page.evaluate_script('location.reload(true)')
next
end
cp = window_opened_by do
click_link('To Control Panel')
end
MailParser.new(cp, self).call
idx += 1
page.evaluate_script('window.history.back()')
page.evaluate_script('window.history.back()')
page.evaluate_script('location.reload(true)')
end
puts "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
next_page_link = first('#t1 + table a', text: 'Next>')
break unless next_page_link
current_page += 1
next_page_link.click
end
end
def sign_in(login, password)
visit('/single.html')
within_frame(0) do
fill_in('user', :with => login)
fill_in('password', :with => password)
select('English (United States)', :from => 'lang')
click_button('login')
end
end
def wait_element(element)
while page.all(element).length.zero?
sleep 1
end
end
end
end
if ARGV.length != 2
STDERR.puts 'Usage: ruby parser.rb USERNAME PASSWORD'
exit
end
MyCapybaraTest::Test.new.process([ARGV[0], ARGV[1]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment