# __author__ = 'Ztiny'
# -*-coding:utf-8-*-
from selenium.webdriver import Remote
from selenium import webdriver
# 启动浏览器驱动
def browser():
driver = webdriver.Firefox()
# host = '192.168.0.132:5555' #运行主机 :端口号(默认本机:127.0.0.1:4444)
# dc = {'browserName':'internet explorer','version':'','platfrom':'WINDOWS','javascriptEnabled':True}
# # dc = {'browserName':'firefox','version':'','platfrom':'ANY','javascriptEnabled':True,'marionette':False,}#指定浏览器 ('chrome','firefox')
# driver = Remote(command_executor='http://' + host + '/wd/hub',
# desired_capabilities=dc)
return driver
if __name__ == '__main__':
dr = browser()
dr.get("http://www.mayi.com")
dr.quit()
# __author__ = 'Ztiny'
#-*-coding:utf-8-*-
from selenium import webdriver
from driver import browser
import unittest
class MyTest(unittest.TestCase):
def setUp(self):
self.driver = browser()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
# __author__ = 'Ztiny'
#-*-coding:utf-8-*-
from selenium import webdriver
import os
#截图函数
def insert_img(driver, file_name):
base_dir = os.path.dirname(os.path.dirname(__file__))
base_dir = str(base_dir)
base_dir = base_dir.replace('\\','/')
base = base_dir.split('test_case')[0]
file_path = base + "report/image/" + file_name
driver.get_screenshot_as_file(file_path)
if __name__ == '__main__':
driver = webdriver.Ie()
driver.get("http://www.baidu.com")
insert_img(driver,'baidu.jpg')
driver.quit()
# __author__ = 'Ztiny'
#-*-coding:utf-8-*-
class Page(object):
'''
页面基础类,用于所有页面的继承
'''
baidu_url = 'https://www.baidu.com'
def __init__(self,selenium_driver,base_url = baidu_url,parent =None):
self.base_url = base_url
self.driver = selenium_driver
self.timeout = 30
self.parent = parent
def _open(self,url):
url = self.base_url + url
self.driver.get(url)
assert self.on_page(),'Did not land on %s' % url
def find_element(self,*loc):
return self.driver.find_element(*loc)
def find_elements(self,*loc):
return self.driver.find_elements(*loc)
def open(self):
self._open(self.url)
def on_page(self):
return (self.driver.current_url).encode('utf-8') == (self.base_url + self.url)
def script(self,src):
return self.driver.execute_script(src)
# __author__ = 'Ztiny'
# -*-coding:utf-8-*-
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep
class login(Page):
'''
用户登录界面
'''
url = '/'
#Action
baidu_login_user_loc = (By.LINK_TEXT,u'登录')
#弹出登录窗口
def baidu_login(self):
self.find_element(*self.baidu_login_user_loc).click()
login_username_loc = (By.ID,'TANGRAM__PSP_8__userName')
login_password_loc = (By.ID,'TANGRAM__PSP_8__password')
login_button_loc = (By.ID,'TANGRAM__PSP_8__submit')
#登录用户名
def login_username(self, username):
self.find_element(*self.login_username_loc).clear()
self.find_element(*self.login_username_loc).send_keys(username)
#登录密码
def login_password(self, password):
self.find_element(*self.login_password_loc).clear()
self.find_element(*self.login_password_loc).send_keys(password)
#登录按钮
def login_button(self):
self.find_element(*self.login_button_loc).click()
#统一登录入口
def user_login(self, username="**********@qq.com", password="*********"):
'''获取用户名和面登录'''
self.open()
self.baidu_login()
self.login_username(username)
self.login_password(password)
self.login_button()
sleep(2)
user_error_hint_loc = (By.LINK_TEXT,u"账号不能为空")
pawd_error_hint_loc = (By.LINK_TEXT,u"密码不能为空")
user_login_success_loc = (By.LINK_TEXT,u'Ztiny')
#用户名错误提示
def user_error_hint(self):
return self.find_element(*self.user_error_hint_loc).text
#密码错误提示
def pawd_error_hint(self):
return self.find_element(*self.pawd_error_hint_loc).text
#登录成功用户名
def user_login_success(self):
return self.find_element(*self.user_login_success_loc).text
复制代码
- # __author__ = 'Ztiny'
- #-*-coding:utf-8-*-
- from time import sleep
- import unittest, random ,sys
- sys.path.append("./models")
- sys.path.append("./page_obj")
- from models import myunit, function
- from page_obj.loginPage import login
- class loginTest(myunit.MyTest):
- '''测试用户登录'''
- def user_login_verify(self, username='',password=''):
- login(self.driver).user_login(username,password)
- def test_login1(self):
- '''用户名、密码为空登录'''
- self.user_login_verify()
- po = login(self.driver)
- self.assertEqual(po.user_error_hint(),"账号不能为空")
- self.assertEqual(po.pawd_error_hint()."密码不能为空")
- function.insert_img(self.driver,"user_pawd_empty.jpg")
- def test_login2(self):
- '''用户名正确,密码为空登录'''
- self.user_login_verify(username="*******")
- po = login(self.driver)
- self.assertEqual(po.pawd_error_hint(),"密码不能为空")
- function.insert_img(self.driver,"paqd_empty.jpg")
- def test_login3(self):
- '''用户名为空,密码正确'''
- self.user_login_verify(password="*******")
- po = login(self.driver)
- self.assertEqual(po.user_error_hint(),"账号不能为空")
- function.insert_img(self.driver,"user_empty.jpg")
- def test_login4(self):
- '''用户名与密码不匹配'''
- character = random.choice('abcdefghijklmnopqrstuvwxyz')
- username = "zhangsan" + character
- self.user_login_verify(username=username,password="123456")
- po = login(self.driver)
- self.assertEqual(po.pawd_error_hint(),"密码与账号不匹配")
- function.insert_img(self.driver,"user_pwad_error.jpg")
- def test_login5(self):
- '''用户名、密码正确'''
- self.user_login_verify(username='********@qq.com',password='********')
- sleep(2)
- po = login(self.driver)
- self.assertEqual(po.user_login_success(), u'Ztiny')
- function.insert_img(self.driver ,"user_pwd_ture.jpg")
- if __name__ == '__main__':
- unittest.main()
@echo off
echo 启动hub
java -jar .\mztestpro\driver\selenium-server-standalone-2.40.0.jar -role hub -host 192.168.0.102 -port 4444
echo 启动node
java -jar .\mztestpro\driver\selenium-server-standalone-2.40.0.jar -role node -port 5555 -hub http://192.168.0.102:4444/grid/register
复制代码
- # __author__ = 'Ztiny'
- #-*-coding:utf-8-*-
- from HTMLTestRunner import HTMLTestRunner
- from email.mime.text import MIMEText
- import smtplib
- import unittest
- import time
- import os
- # =========================邮件接收者============================
- mailto_list=["********@qq.com"]
- #============= 设置服务器,用户名、口令以及邮箱的后缀===============
- mail_host="smtp.163.com"
- mail_user="******@163.com"
- mail_pass="*******"
- #===========================发送邮件============================
- def send_mail(to_list,file_new):
- '''''
- to_list:发给谁
- sub:主题
- content:内容
- send_mail("aaa@126.com","sub","content")
- '''
- f = open(file_new, 'rb')
- mail_body = f.read()
- f.close()
- me=mail_user
- msg = MIMEText(mail_body,'html','utf-8')
- msg['Subject'] = u'自动化测试报告'
- msg['From'] = me
- msg['To'] = ";".join(to_list)
- try:
- s = smtplib.SMTP()
- s.connect(mail_host,25)
- s.login(mail_user,mail_pass)
- s.sendmail(me, to_list, msg.as_string())
- s.close()
- return True
- except Exception, e:
- print str(e)
- return False
- #==============查找测试报告目录,找到最新生成的测试报告文件==========
- def new_report(testreport):
- lists = os.listdir(testreport)
- lists.sort(key=lambda fn:os.path.getatime(testreport + "\\" + fn))
- file_new = os.path.join(testreport,lists[-1])
- print(file_new)
- return file_new
- if __name__ == '__main__':
- now = time.strftime("%Y-%m-%d %H_%M_%S")
- filename = './bbs/report/' + now +'result.html'
- fp = open(filename,'wb')
- runner = HTMLTestRunner(stream=fp,
- title=u'百度登录自动化测试报告',
- description=u'环境 :window 7 浏览器:firefox')
- discover = unittest.defaultTestLoader.discover('./bbs/test_case',
- pattern='*_sta.py')
- runner.run(discover)
- fp.close()
- file_path = new_report('./bbs/report/')
- if send_mail(mailto_list,file_path):
- print u"发送成功"
- else:
- print u"发送失败"
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |