Dear all,
So I am trying to run a watir test application. Everything seems to be going well except I need to log each step that happens.
I am able to log using the php script, but have not yet figured out how to write to a log file using ruby.
Now since I am more experienced with php, I thought it'd be wise to pass the ruby data back to php before I can write to the log file. Below is my code;
// Call ruby script with watir/selenium webdriver to run test
function run_test ($browser, $country_code, $test)
{
//echo $test["test_name"];
//-------------------------------------------------------------------------------------------------------
$launch_time = date('Y-m-d_His');
echo "At $launch_time Running Test " . $test["test_number"] . " - " .$test["test_name"] . " ... \n";
$arg0 = str_replace(" ", "...",$browser);
$arg1 = str_replace(" ", "...",$country_code);
$arg2 = str_replace(" ", "...",$test["test_name"]);
$arg3 = str_replace(" ", "...",$test["home_url"]);
$arg4 = str_replace(" ", "...",$test["start_id"]);
$arg5 = str_replace(" ", "...",$test["receiver_price_local"]);
$arg6 = str_replace(" ", "...",$test["receiver_price_foreign"]);
$arg7 = str_replace(" ", "...",$test["receiver_phone"]);
$arg8 = str_replace(" ", "...",$test["receiver_first_name"]);
$arg9 = str_replace(" ", "...",$test["receiver_last_name"]);
$arg10 = str_replace(" ", "...",$test["paypal_email"]);
$arg11 = str_replace(" ", "...",$test["paypal_pwd"]);
$arg12 = str_replace(" ", "...",$launch_time);
$string_to_write = "------------------------------------------------------------------------------------------------------- \n";
$string_to_write.= "A test has been carried out with the following details \n";
$string_to_write.= "Browser: ".$browser."\n";
$string_to_write.= "Country Code: ".$country_code."\n";
$string_to_write.= "Test name: ".$test["test_name"]."\n";
$string_to_write.= "Home URL: ".$test["home_url"]."\n";
$string_to_write.= "Test ID: ".$test["start_id"]."\n";
$string_to_write.= "Item Price (local): ".$test["receiver_price_local"]."\n";
$string_to_write.= "Item Price (Foreign): ".$test["receiver_price_foreign"]."\n";
$string_to_write.= "Receiver first name: ".$test["receiver_first_name"]."\n";
$string_to_write.= "Receiver last name: ".$test["receiver_last_name"]."\n";
$string_to_write.= "Receiver phone: ".$test["receiver_phone"]."\n";
$string_to_write.= "Client email: ".$test["paypal_email"]."\n";
// launches the ruby irb script to run selenium driver for browser
//$mytest ="ruby C:/wamp/www/voucher_test/test_purchase.rb $arg0 $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8 $arg9 $arg10 $arg11 $arg12";
//echo $mytest;
//exit();
$outp = system("ruby C:/wamp/www/voucher_test/test_purchase.rb $arg0 $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8 $arg9 $arg10 $arg11 $arg12");
// Check for fail
echo "Watir script returned:\n" . strtoupper($outp) . "\n dbg pos:" . strpos("PASS", strtoupper($outp)) . "\n";
if (strpos("FAIL", strtoupper($outp)) || strpos("ERROR",strtoupper($outp)) )
{
echo "Test Failed .. emailing Admin ..\n\n";
//writeToLog($browser, $country_code, $test, $launch_time, "FAIL");
$string_to_write.= "Test failed. Watir returned output: ".strtoupper($outp)."\n";
} elseif (strpos(strtoupper($outp), "PASS") ) {
echo "Test Passed \n\n";
//writeToLog($browser, $country_code, $test, $launch_time, "PASS");
$string_to_write.= "Test passed. Watir returned output: ".strtoupper($outp)."\n";
} else
{
echo "Unknown test status\n$outp\n\n";
$string_to_write.= "Unknown test status. Watir returned output: ".strtoupper($outp)."\n";
//writeToLog($browser, $country_code, $test, $launch_time, "FAIL");
}
$string_to_write.= "------------------------------------------------------------------------------------------------------- \n \n";
//Write to the log file
test_log_write($string_to_write);
}
And this is my ruby code;
#
# Generic test for header.php links
#
# Reference: https://code.google.com/p/selenium/wiki/RubyBindings
#
# Get commandline arguments
browser_select = ARGV[0].dup
browser_select.gsub! '...', ' '
country_code = ARGV[1].dup
country_code.gsub! '...', ' '
test_name = ARGV[2].dup
test_name.gsub! '...', ' '
home_url = ARGV[3].dup
home_url.gsub! '...', ' '
start_id = ARGV[4].dup
start_id.gsub! '...', ' '
receiver_price_local = ARGV[5].dup
receiver_price_local.gsub! '...', ' '
receiver_price_foreign = ARGV[6].dup
receiver_price_foreign.gsub! '...', ' '
receiver_phone = ARGV[7].dup
receiver_phone.gsub! '...', ' '
receiver_first_name = ARGV[8].dup
receiver_first_name.gsub! '...', ' '
receiver_last_name = ARGV[9].dup
receiver_last_name.gsub! '...', ' '
paypal_email = ARGV[10].dup
paypal_email.gsub! '...', ' '
paypal_pwd = ARGV[11].dup
paypal_pwd.gsub! '...', ' '
launch_time = ARGV[12].dup
launch_time.gsub! '...', ' '
screenshot_url = "C:/wamp/www/voucher_test/screenshots/"
p "Running test: " + test_name
p " launch_time:" + launch_time
p " country_code:" + country_code
p " home_url:" + home_url
p " start_id:" + start_id
p " receiver_price_local:" + receiver_price_local
p " receiver_price_foreign:" + receiver_price_foreign
p " receiver_phone:" + receiver_phone
p " receiver_first_name:" + receiver_first_name
p " receiver_last_name:" + receiver_last_name
p " paypal_email:" + paypal_email
p " paypal_pwd:" + paypal_pwd
#
# Start watir/selenium
require "selenium-webdriver"
p "Running " + browser_select + " browser "
if browser_select.include? "firefox"
browser = Selenium::WebDriver.for :firefox
elsif browser_select.include? "chrome"
browser = Selenium::WebDriver.for :chrome
elsif browser_select.include? "opera"
browser = Selenium::WebDriver.for :opera
elsif browser_select.include? "internet_explorer"
browser = Selenium::WebDriver.for :internet_explorer
end
status = "NOT RUN"
# wait for a specific element to show up
wait = Selenium::WebDriver::Wait.new(:timeout => 15) # seconds
# Start at home page
sleep(3)
browser.navigate.to home_url
#browser.get home_url
sleep(10)
# wait till timeout
wait.until {
browser.current_url.include? home_url
}
url = browser.current_url
if url.include? home_url
p "Successfully navigated to home url:" + home_url
status = "OK"
else
p "ERROR0001: Failed to navigate to home_url:" + home_url
browser.save_screenshot(screenshot_url + test_name + "_ERROR0001_Homepage_" + launch_time + ".jpg")
status = "FAIL ERROR0001"
end
#-----------------------------------------------------------------
# Go to item link on home page
if status == "OK"
p "Description attempting to select item: " + start_id + " ..... "
browser.find_element(id: start_id).click
dest_url = home_url + "/receiver_vchr.php"
#dest_text = "Voucher's Value"
dest_text = "receiver_amount_in_local";
#sleep(3)
# wait till timeout
wait.until {
browser.current_url.include? dest_url
}
url = browser.current_url
if url.include? dest_url
p "Navigated to " + dest_url + " successfully"
# Check page contents
src = browser.page_source
if src.include? dest_text
p "OK > Found familiar content on destination page"
# now load form
browser.find_element(id: "receiver_first_name").send_keys receiver_first_name
sleep(1)
browser.find_element(id: "receiver_last_name").send_keys receiver_last_name
sleep(1)
browser.find_element(id: "receiver_phone").send_keys receiver_phone
sleep(1)
browser.find_element(id: "phoneConfirm").send_keys receiver_phone
# submit form
sleep(1)
browser.find_element(name: "send").submit
status = "OK"
else
p "ERROR0002.2: Failed to see familiar text destination url: " + dest_url + ", saving screenshot"
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.2_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.2"
end
else
p "ERROR0002.1: Failed to navigate to dest_url:" + dest_url
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.1_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.1"
end
end
#-----------------------------------------------------------------
# Go to confirm page
if status == "OK"
p "Description attempting to submit confirmation form ..... "
dest_url = home_url + "/process_vchr.php"
#sleep(3)
# wait till timeout
wait.until {
browser.current_url.include? dest_url
}
url = browser.current_url
if url.include? dest_url
p "Navigated to " + dest_url + " successfully"
# Check page contents
src = browser.page_source
if ( (src.include? receiver_first_name) &&
(src.include? receiver_last_name) &&
(src.include? receiver_phone) &&
(src.include? receiver_price_foreign) &&
(src.include? receiver_price_local) )
p "OK > Found familiar content on destination page"
# submit form
sleep(1)
browser.find_element(name: "send").submit
status = "OK"
else
p "ERROR0002.4: Failed to see familiar text destination url: " + dest_url + ", saving screenshot"
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.4_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.4"
end
else
p "ERROR0002.3: Failed to navigate to dest_url:" + dest_url
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.3_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.3"
end
end
#-----------------------------------------------------------------
# Go to paypal
if status == "OK"
p "Description attempting to log into paypal for payment, choose payment method ..... "
dest_url = "paypal.com"
#sleep(3)
# wait till timeout
wait.until {
browser.current_url.include? dest_url
}
url = browser.current_url
if url.include? dest_url
p "Navigated to " + dest_url + " successfully"
# Check page contents
src = browser.page_source
if ( (src.include? "Pay with my PayPal account") &&
(src.include? "login_email") &&
(src.include? "login_password"))
p "OK > Found familiar content on destination page"
# submit form
browser.find_element(name: "login_email").send_keys paypal_email
sleep(1)
browser.find_element(name: "login_password").send_keys paypal_pwd
sleep(1)
browser.find_element(name: "login.x").submit
status = "OK"
else
if ( (src.include? "Pay with my PayPal account")
(src.include? "login_button"))
p "OK > Choosing paypal account login"
browser.find_element(id: "loadLogin").click
status = "OK"
sleep(4)
src = browser.page_source
if ( (src.include? "Pay with my PayPal account") &&
(src.include? "login_email") &&
(src.include? "login_password"))
p "OK > Found familiar content on destination page"
# submit form
browser.find_element(id: "login_email").send_keys paypal_email
sleep(1)
browser.find_element(id: "login_password").send_keys paypal_pwd
sleep(1)
browser.find_element(name: "login.x").click
status = "OK"
else
p "ERROR0002.7: Failed to see familiar text destination url: " + dest_url + ", saving screenshot"
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.7_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.7"
end
else
p "ERROR0002.6: Failed to see familiar text destination url: " + dest_url + ", saving screenshot"
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.6_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.6"
end
end
else
p "ERROR0002.5: Failed to navigate to dest_url:" + dest_url
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.5_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.5"
end
end
#-----------------------------------------------------------------
# Go to paypal info review and pay confirmation
if status == "OK"
p "Description attempting to review into paypal info and submit payment ..... "
#dest_url = "paypal.com"
dest_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
#sleep(3)
# wait till timeout
wait.until {
browser.current_url.include? dest_url
}
url = browser.current_url
if url.include? dest_url
p "Navigated to " + dest_url + " successfully"
# submit form
sleep(5)
browser.find_element(name: "continue").click
status = "OK"
else
p "ERROR0002.8: Failed to navigate to dest_url:" + dest_url
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.8_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.8"
end
end
#-----------------------------------------------------------------
# Return to home_url
if status == "OK"
p "Description attempting to redirect to home url for completion and messaging submit to receiver ..... "
dest_url = home_url
#sleep(3)
# wait till timeout
wait.until {
browser.current_url.include? dest_url
}
url = browser.current_url
if url.include? dest_url
p "Navigated to " + dest_url + " successfully"
# Check page contents
src = browser.page_source
if ( (src.include? "Thank you for your purchase!") &&
(src.include? receiver_price_foreign) &&
(src.include? receiver_price_local) )
p "OK > Found familiar content on destination page"
# Done
status = "PASS"
else
p "ERROR0002.11: Failed to see familiar text destination url: " + dest_url + ", saving screenshot"
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.11_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.11"
end
else
p "ERROR0002.10: Failed to navigate to dest_url:" + dest_url
browser.save_screenshot(screenshot_url + test_name + "_ERROR0002.10_" + launch_time + ".jpg")
# end and close browser
status = "FAIL ERROR0002.10"
end
end
browser.close
# status string must be on last line of output
p "Done test:" + test_name + " : " + status + ".. exiting"
exit
Any help??