Guest User

Untitled

a guest
Nov 24th, 2017
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "rubygems"
  4. gem "rspec"
  5.  
  6. require './Helpers/email.rb'
  7. require './Helpers/utils.rb'
  8. require './Helpers/selen.rb'
  9. require '../Settings/data.rb'
  10.  
  11. require 'postmark'
  12.  
  13.  
  14. describe "Email through API by IP" do
  15.  
  16. attr_accessor :postmarkusername,
  17. :postmarkpassword,
  18. :message,
  19. :emailaccounts,
  20. :emailaccount
  21.  
  22. include Utils
  23. include Selen
  24.  
  25. before(:all) do
  26.  
  27. @message = Mail.new
  28. @message.from = $FROM_EMAIL
  29. @message.to = $TO_EMAIL
  30.  
  31. @servicetype = $API
  32.  
  33. @message.content_type = "text/html"
  34. @message.body = "Test email sent"
  35.  
  36. @message.delivery_method Mail::Postmark, {:api_key => $API_TOKEN}
  37.  
  38. # read all email accounts to which emails would be sent and checked
  39. file = File.open("../Settings/emailaccountsettings")
  40. filecontent = file.read
  41. file.close
  42.  
  43. @email_accounts = filecontent.split("\n")
  44.  
  45. @emailaccount = Array.new(@email_accounts.length)
  46.  
  47. # go through all email accounts and read settings for every one of them and create array of email account settings
  48. for i in 0..@email_accounts.length-1
  49.  
  50. @emailaccount[i] = @email_accounts[i].split(",")
  51.  
  52. end
  53.  
  54. @postmarkusername = $USERNAME
  55. @postmarkpassword = $PASSWORD
  56.  
  57. init_selenium($PORT,$BROWSERTYPE + $BROWSERURL,$WEBSITE)
  58.  
  59. end
  60.  
  61. before(:each) do
  62. start_selenium_session
  63. end
  64.  
  65. append_after(:each) do
  66. stop_selenium_session
  67. end
  68.  
  69. it "should be sent to gmail account through #{$WAPI1}" do
  70.  
  71. # subject with random number so it's unique
  72. @message.subject = "Test email sent " + get_random_number(5).to_s + " by #{$WAPI1}"
  73. Postmark.host = $webapi[$WAPI1]
  74.  
  75. puts "check #{@emailaccount[0][0]} account through #{$WAPI1}:"
  76.  
  77. @message.to = @emailaccount[0][1]
  78. send_email(@message.to)
  79.  
  80. # check if email arrived
  81. check_email(@emailaccount[0][2],@emailaccount[0][3],@emailaccount[0][4],@emailaccount[0][5],@emailaccount[0][6],@emailaccount[0][7] == "true")
  82.  
  83. # check if email showed up in activity page
  84. check_email_in_activity_page
  85.  
  86. end
  87.  
  88. it "should be sent to gmail account through #{$WAPI2}" do
  89.  
  90. # subject with random number so it's unique
  91. @message.subject = "Test email sent " + get_random_number(5).to_s + " by #{$WAPI2}"
  92. Postmark.host = $webapi[$WAPI2]
  93.  
  94. puts "check #{@emailaccount[0][0]} account through #{$WAPI2}:"
  95.  
  96. @message.to = @emailaccount[0][1]
  97. send_email(@message.to)
  98.  
  99. # check if email arrived
  100. check_email(@emailaccount[0][2],@emailaccount[0][3],@emailaccount[0][4],@emailaccount[0][5],@emailaccount[0][6],@emailaccount[0][7] == "true")
  101.  
  102. # check if email showed up in activity page
  103. check_email_in_activity_page
  104.  
  105. end
  106.  
  107. it "should be sent to gmail account through #{$WAPI3}" do
  108.  
  109. # subject with random number so it's unique
  110. @message.subject = "Test email sent " + get_random_number(5).to_s + " by #{$WAPI3}"
  111. Postmark.host = $webapi[$WAPI3]
  112.  
  113. puts "check #{@emailaccount[0][0]} account through #{$WAPI3}:"
  114.  
  115. @message.to = @emailaccount[0][1]
  116. send_email(@message.to)
  117.  
  118. # check if email arrived
  119. check_email(@emailaccount[0][2],@emailaccount[0][3],@emailaccount[0][4],@emailaccount[0][5],@emailaccount[0][6],@emailaccount[0][7] == "true")
  120.  
  121. # check if email showed up in activity page
  122. check_email_in_activity_page
  123.  
  124. end
  125.  
  126. it "should be sent to gmail account through #{$WAPI4}" do
  127.  
  128. # subject with random number so it's unique
  129. @message.subject = "Test email sent " + get_random_number(5).to_s + " by #{$WAPI4}"
  130. Postmark.host = $webapi[$WAPI4]
  131.  
  132. puts "check #{@emailaccount[0][0]} account through #{$WAPI4}:"
  133.  
  134. @message.to = @emailaccount[0][1]
  135. send_email(@message.to)
  136.  
  137. # check if email arrived
  138. check_email(@emailaccount[0][2],@emailaccount[0][3],@emailaccount[0][4],@emailaccount[0][5],@emailaccount[0][6],@emailaccount[0][7] == "true")
  139.  
  140. # check if email showed up in activity page
  141. check_email_in_activity_page
  142.  
  143. end
  144.  
  145. # methods used for scenarios
  146.  
  147. # send email through pm API
  148. def send_email(emailaddress)
  149.  
  150. @message.to = emailaddress
  151.  
  152. @message.deliver
  153.  
  154. end
  155.  
  156. # check 5 last emails if email was received in period of $HOWLONGTOCHECK minutes
  157. def check_email(method,address,port,username,password,ssl)
  158.  
  159. # set account to check
  160. set_email_account(method,address,port,username,password,ssl)
  161.  
  162. # check if email is received
  163.  
  164. email = ""
  165.  
  166. ($HOWLONGTOCHECK*60).times {
  167.  
  168. emails = get_last_emails(5)
  169.  
  170. for i in 0..emails.length-1 do
  171.  
  172. email = emails[i]
  173. break if ( @message.subject.include? email.subject)
  174.  
  175. end
  176.  
  177. break if ( @message.subject.include? email.subject)
  178. sleep 1
  179.  
  180. }
  181.  
  182. # verify email was received
  183. (@message.subject.include? email.subject).should == true
  184.  
  185. end
  186.  
  187. # check if email appeared in activity page
  188. def check_email_in_activity_page
  189.  
  190. login_to_postmark(@postmarkusername,@postmarkpassword)
  191.  
  192. page.click "link=Details"
  193. page.wait_for_page_to_load "30000"
  194.  
  195. page.click "link=Activity"
  196. page.wait_for_page_to_load "30000"
  197.  
  198. page.type "bounces-filter", @message.subject
  199. page.click "//input[@value='Search']"
  200. page.wait_for_page_to_load "30000"
  201.  
  202. emailto = convert_email_to_string(@message.to)
  203.  
  204. # verify message by email address
  205. page.is_element_present("//div[@class=\"activity-item sent\"]/h3/a[contains(text(),'#{emailto}')]").should be_true
  206.  
  207. # verify message by email subject
  208. page.is_element_present("//div[@class=\"activity-item sent\"]/p[contains(text(),'#{@message.subject}')]").should be_true
  209.  
  210. # verify message by status
  211. page.is_element_present("//div[@class=\"activity-item sent\"]/p[contains(text(),'Email Sent')]").should be_true
  212.  
  213. end
  214.  
  215. end
Add Comment
Please, Sign In to add comment