美文网首页我爱编程
Selenium+Python环境搭建流程

Selenium+Python环境搭建流程

作者: 不撸代码不舒服斯基 | 来源:发表于2018-08-08 09:50 被阅读0次

1.环境准备
1.1安装Python3(百度自查windows或mac的安装方法)
安装成功后,在终端输入python3 -V
1.2安装selenium
终端输入sudo easy_install selenium
1.3官网下载pycharm
[https://www.jetbrains.com/pycharm/download/#section=mac]
选择Professional版本的
1.4激活pycharm,推荐使用授权服务器激活
选择License server激活,然后填入http://im.js.cn:8888http://idea.java.sx/,最后点击Activate激活即可
2.selenium脚本的编写
2.1pycharm目录如图

目录.png
代码.png
例子:test_login.py
import unittest
from selenium import webdriver
import time
from write_read_cookie.skip_login import save_cookie

class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
# print('开始测试,打开浏览器')
cls.driver = webdriver.Chrome()
cls.url="http://dev.dingnuo.ai:3000/login"

@classmethod
def tearDownClass(cls):
    # print('结束测试,关闭浏览器')
    cls.driver.close()
    cls.driver.quit()

def setUp(self):
    if self.driver.current_url not in self.url:
        self.driver.get(self.url)
        time.sleep(2)

def login(self, username, password):
    self.driver.find_element_by_id('login-username').clear()
    self.driver.find_element_by_id('login-username').send_keys(username)
    self.driver.find_element_by_id('login-password').clear()
    self.driver.find_element_by_id('login-password').send_keys(password)
    self.driver.find_element_by_link_text(u'登录').click()
    time.sleep(5)
    return self.driver.current_url

# 用户名,密码正确
def test_login_success(self):
    current_url = self.login('jindaqian', '123456')
    self.assertEqual('http://dev.dingnuo.ai:3000/questionList', current_url)
   ##将登录后的cookie保存到本地文件中,下次其他页面操作,通过cookie跳过登录 
   save_cookie(self.driver)


# 正确用户名,密码错误
def test_login_pwd_error(self):
    current_url = self.login('jindaqian', '12345')
    self.assertEqual('http://dev.dingnuo.ai:3000/login', current_url)
    self.driver.get_screenshot_as_file("screenshot/login_pwd_error.jpg")

# 用户名错误,密码正确
def test_login_uname_error(self):
    current_url = self.login('jdq', '123456')
    self.assertEqual('http://dev.dingnuo.ai:3000/login', current_url)
    self.driver.get_screenshot_as_file("screenshot/login_uname_error.jpg")

# 用户名密码都是空的情况下直接点击登录
def test_login_uname_pwd_empty(self):
    current_url = self.login('','')
    self.assertEqual('http://dev.dingnuo.ai:3000/login', current_url)
    self.driver.get_screenshot_as_file("screenshot/login_uname_pwd_empty.jpg")

if name == 'main':
unittest.main()
例子:run_all_case.py
import unittest
import os

import HTMLTestRunner

def allcase():
current_path = '/工作文件/workspace/dingnuo/case'
testsuite = unittest.TestSuite()
discover = unittest.defaultTestLoader.discover(current_path,pattern='test*.py',top_level_dir=None)
# discover方法筛选出来的用例,循环添加到测试套件中
for test_suite in discover:
for test_case in test_suite:
print(test_case)
#添加测试用例到testcase中
testsuite.addTest(test_case)
return testsuite

if name=='main':
fp=open('report.html',"wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title='问答社区管理后台测试报告',
description='用例执行情况:')
runner.run(allcase())
fp.close()
运行结果

1533692992105.jpg

相关文章

网友评论

    本文标题:Selenium+Python环境搭建流程

    本文链接:https://www.haomeiwen.com/subject/yrktbftx.html