Skip to content

Instantly share code, notes, and snippets.

@gunesmes
Last active December 12, 2015 00:18
Show Gist options
  • Save gunesmes/4682320 to your computer and use it in GitHub Desktop.
Save gunesmes/4682320 to your computer and use it in GitHub Desktop.
a sample and simple test framework for selenium (in python) and watir (in ruby) entegration
require "watir"
require "json"
def sign_up(url, name, email, password)
print "watir will run the internet explorer"
browser = Watir::Browser.start url
browser.link(:xpath, "/html/body/div/div[2]/div/div/div[4]/a").click
browser.link(:id, "registrationLink").click
browser.text_field(:id, "ap_customer_name").set(name)
browser.text_field(:id, "ap_email").set(email)
browser.text_field(:id, "ap_email_check").set(email)
browser.text_field(:id, "ap_password").set(password)
browser.text_field(:id, "ap_password_check").set(password)
browser.button(:src,"https://images-na.ssl-images-amazon.com/images/G/01/Quarterdeck/en_US/images/signin/continue_button_02092011._V168238734_.jpg").click
browser.radio(:id, "defaultDepartmentMen").set
browser.radio(:id, "shippingPreferenceNonUS").set
browser.radio(:id, "shippingPreferenceUS").set
browser.button(:src,"https://images-na.ssl-images-amazon.com/images/G/01/Quarterdeck/en_US/images/signin/start-shopping._V182581784_.png").click
end
ruby_par = String.new()
ruby_par = STDIN.gets
python_json = JSON.parse(ruby_par)
test_url = python_json['url']
test_name = python_json['name']
test_email = python_json['email']
test_password = python_json['password']
sign_up(test_url, test_name, test_email, test_password)
from test_case import MyHabit
from test_data import TestData
m = MyHabit("chrome", "test_server1")
m.sign_up()
# -*- coding: utf8 -*-
import locale
from selenium import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from test_data import TestData
import json
import sys
from subprocess import Popen, PIPE, STDOUT #communicate to external app
wait_time = 10
class MyHabit(unittest.TestCase):
def __init__(self, browser, serverName):
if browser == "firefox":
self.driver = webdriver.Firefox()
elif browser == "ie":
self.driver = webdriver.Ie()
elif browser == "chrome":
self.driver = webdriver.Chrome()
else:
print( "browsers: firefox, ie, chrome" )
sys.exit()
if serverName.upper() == 'TEST_SERVER1':
self.url = "http://www.myhabit.com/"
self.password = 'pass1'
elif serverName.upper() == 'TEST_SERVER2':
self.url = "test_server2.myhabit.com/"
self.password = 'pass2'
else:
print('\n\n' + serverName.upper() + ' is not a valid server name!\n')
print( 'please enter:\n test_server1 (for live site) or\n test_server2 (for dev. site)\n')
print "This just shows an example, I don\'t know what myhabit has its development env."
sys.exit()
print( '\nTests run on %s via %s\n' %(self.url.upper(), browser.upper()))
def setUp(self):
self.driver.implicitly_wait(30)
self.baseUrl = self.url
self.verificationErrors = []
def login(self):
driver = self.driver
"""
sample test code
"""
def sign_up(self):
#Test data which will send to watir
data = TestData()
email = data.createEmail()
name = "test_name"
password ="dummy_password"
#if you have only one paremeter to send the watir you can use the following
#case.communicate(email)
#we have 4 parameters as test data so they are sent as a Json
ruby_par = { "name":name, "email": email, "url": self.url, "password": password}
#create a subprocess object to communicate external app
case = Popen(["ruby", "watir/myhabit_sign_up_watir.rb"], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
case.communicate(json.dumps(ruby_par))
#printing if watir gives an error or out
print "stderr: %s\n" %str(case.stderr)
print "stdout: %s\n" %str(case.stdout)
case.__init__
(no last_used_email create_date used)
(-- ---------------------- ---------------- ----)
(1, 'dummy_test+1@email.com', '2013-01-25 11:37', 'YES')
(2, 'dummy_test+2@email.com', '2013-01-28 16:14', 'YES')
(3, 'dummy_test+3@email.com', '2013-01-28 16:24', 'YES')
(4, 'dummy_test+4@email.com', '2013-01-28 16:26', 'YES')
# -*- coding: utf8 -*-
import datetime
class TestData():
def createEmail(self):
baseEmail = "dummy_test"
emailSup = "@email.com"
# read the text file as a list of lines
# find the last line, change to a file you have
data = open('test_data.txt', "r")
lineList = data.readlines()
lastLineList = lineList[len(lineList)-1]
(lastUsedNumber, lastUsedEmail, Date, isRegistered) = lastLineList.split(",")
lastUsedNumber2 = lastUsedNumber.replace("(", "")
newEmail = (baseEmail + '+' + str(int(lastUsedNumber2) + 1) + emailSup)
data.close()
data = open('./test_data.txt', "a")
now = datetime.datetime.now()
newEmailFull = int(lastUsedNumber2)+1, str(newEmail), str(now.strftime("%Y-%m-%d %H:%M")), "YES"
data.write(str(newEmailFull))
data.write("\n")
data.close()
return newEmail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment