分享一份webdriver自动化脚本

对于后台系统的搜索进行UI自动化,主要是比对页面查询结果是否与预期一致(即数据库查询结果)

search.py

# -*- coding:utf8 -*-
import HTMLTestRunner
import time
import unittest
import public
from selenium import webdriver

class Search(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        public.Public().login_success(self.driver)
        self.driver.find_element_by_link_text("应用列表").click()
        time.sleep(5)
        self.name = self.driver.find_element_by_xpath("//input[@ng-model=‘name‘]")
        self.package = self.driver.find_element_by_xpath("//input[@ng-model=‘package‘]")
        self.developer = self.driver.find_element_by_xpath("//input[@ng-model=‘developer‘]")
        self.tag = self.driver.find_element_by_xpath("//input[@ng-model=‘tag‘]")
        self.category = self.driver.find_element_by_name("classify")
        self.source = self.driver.find_element_by_xpath("//select[@ng-change=‘changeSource()‘]")
        self.status = self.driver.find_element_by_name("status")
        self.search = self.driver.find_element_by_xpath("//button[@ng-click=‘search()‘]")
        self.count_text = self.driver.find_element_by_xpath("//span[@class=‘ng-binding‘]")
        self.app_database = "xxx_com"

    #根据名称进行查询,输入多组测试数据
    def test_search_by_name(self):
        self.search_by_name("cc")
        self.search_by_name("运动")
        self.search_by_name("!")
        self.search_by_name("*")
        self.search_by_name("0")

    #根据包名进行查询,输入多组测试数据
    def test_search_by_package(self):
        self.search_by_package("com.weimeng.mami")
        self.search_by_package("com.com2us.tinypang.momo.freefull.momo.cn.android.common")
        self.search_by_package("CRM.Android.KASS")
        self.search_by_package("*")
        self.search_by_package("0")

    #根据开发者进行查询,输入多组测试数据
    def test_search_by_developer(self):
        self.search_by_developer("百度")
        self.search_by_developer("xxx")
        self.search_by_developer("*")
        self.search_by_developer("0")

    #根据标签进行查询,输入多组测试数据
    def test_search_by_tag(self):
        self.search_by_tag("拼音")
        self.search_by_tag("Google")
        self.search_by_tag("163")
        self.search_by_tag("*")
        self.search_by_tag("0")

    #下拉菜单选择分类后,显示对应分类下的数据
    def test_search_by_category(self):
        all_options = self.category.find_elements_by_tag_name("option")
        for option in all_options:
            option.click()
            time.sleep(2)
            actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
            if option.text == "全部":
                search_sql = "select count(*) from app where status!=-1 and status!=4;"
            else:
                search_sql = "select count(*) from app_category_app as aca,app_category as ac, app as a where ac.name =‘"                             + option.text + "‘ and aca.c_id=ac.id and aca.app_id=a.id and a.status!=-1 and a.status!=4 ;"
            expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
            expect_search_count = int(expect_search_count[0][0])
            self.assertEqual(actual_search_count, expect_search_count)
            time.sleep(2)

    #下拉菜单选择来源后,显示对应来源下的数据
    def test_search_by_source(self):
        all_options = self.source.find_elements_by_tag_name("option")
        for option in all_options:
            option.click()
            time.sleep(2)
            actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
            if option.text == "全部":
                search_sql = "select count(*) from app where status!=-1 and status!=4;"
            else:
                if option.text == "本地上传":
                    source_value = 0
                elif option.text == "豌豆荚":
                    source_value = 1
                elif option.text == "应用宝":
                    source_value = 2
                elif option.text == "开发者中心":
                    source_value = 3
                elif option.text == "九游":
                    source_value = 4
                search_sql = "select count(*) from app where source="+str(source_value)+" and status!=-1 and status!=4 ;"
            expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
            expect_search_count = int(expect_search_count[0][0])
            self.assertEqual(actual_search_count, expect_search_count)
            time.sleep(2)

    ##下拉菜单选择状态后,显示对应状态下的数据
    def test_search_by_status(self):
        all_options = self.status.find_elements_by_tag_name("option")
        for option in all_options:
            option.click()
            time.sleep(2)
            actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
            if option.text == "全部":
                search_sql = "select count(*) from app where status!=-1 and status!=4;"
            else:
                if option.text == "待审核":
                    status_value = 2
                elif option.text == "已上架":
                    status_value = 1
                elif option.text == "已下架":
                    status_value = 0
                elif option.text == "拒绝上架":
                    status_value = -2
                elif option.text == "已下架":
                    status_value = 0
                elif option.text == "临时":
                    status_value = 3
                elif option.text == "预发布":
                    status_value = 5
                search_sql = "select count(*) from app where status="+str(status_value)+" and status!=-1 and status!=4 ;"
            expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
            expect_search_count = int(expect_search_count[0][0])
            self.assertEqual(actual_search_count, expect_search_count)
            time.sleep(2)

    def search_by_name(self, keyword):
        self.name.send_keys(keyword)
        self.search.click()
        time.sleep(2)
        actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
        search_sql = "select count(*) from app where (name like ‘%"+                     keyword+"%‘ or cname like ‘%"+keyword+"%‘) and status!=-1 and status!=4;"
        expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
        expect_search_count = int(expect_search_count[0][0])
        self.assertEqual(actual_search_count, expect_search_count)
        self.name.clear()

    def search_by_package(self, package):
        self.package.send_keys(package)
        self.search.click()
        time.sleep(2)
        actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
        search_sql = "select count(*) from app where package=‘"+package+"‘ and status!=-1 and status!=4;"
        expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
        expect_search_count = int(expect_search_count[0][0])
        self.assertEqual(actual_search_count, expect_search_count)
        self.package.clear()

    def search_by_developer(self, keyword):
        self.developer.send_keys(keyword)
        self.search.click()
        time.sleep(2)
        actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
        search_sql = "select count(*) from app where developer like‘%"+keyword+"%‘ and status!=-1 and status!=4;"
        expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
        expect_search_count = int(expect_search_count[0][0])
        self.assertEqual(actual_search_count, expect_search_count)
        self.developer.clear()

    def search_by_tag(self, keyword):
        self.tag.send_keys(keyword)
        self.search.click()
        time.sleep(2)
        actual_search_count = int(self.count_text.text[3:len(self.count_text.text)-1])
        search_sql = "select count(*) from app,app_tag where app_tag.tag like‘%"                     +keyword+"%‘ and app_tag.app_id=app.id and app.status!=-1 and app.status!=4;"
        expect_search_count = public.Public().search_from_mysql(self.app_database, search_sql)
        expect_search_count = int(expect_search_count[0][0])
        self.assertEqual(actual_search_count, expect_search_count)
        self.tag.clear()

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    test_suite = unittest.TestSuite()
    test_suite.addTest(Search("test_search_by_name"))
    test_suite.addTest(Search("test_search_by_package"))
    test_suite.addTest(Search("test_search_by_developer"))
    test_suite.addTest(Search("test_search_by_tag"))
    test_suite.addTest(Search("test_search_by_category"))
    test_suite.addTest(Search("test_search_by_source"))
    test_suite.addTest(Search("test_search_by_status"))
    filename = "/yangjing/report/search.html"
    fp = open(filename, "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title="app_search_report")
    runner.run(test_suite)

public.py

# -*- coding:utf8 -*-
import time
import pymysql

class Public():

    def __init__(self):
        self.base_url = "http://xxx.com"
        self.login_url = self.base_url + "/xxx/login"
        self.username = "xxx"
        self.password = "xxx"
        self.mysql_user = "xxx"
        self.mysql_password = "xxx"
        self.mysql_host = "xxx"

    def login_success(self, driver):
        driver.get(self.login_url)
        time.sleep(2)
        driver.find_element_by_id("username").send_keys(self.username)
        driver.find_element_by_id("password").send_keys(self.password)
        driver.find_element_by_xpath(
            "//button[@ng-click=‘login()‘]").click()
        time.sleep(2)

    def search_from_mysql(self, database_name, search_sql):
        try:
            conn = pymysql.connect(host=self.mysql_host, port=3306, user=self.mysql_user,
                                   passwd=self.mysql_password, db=database_name, charset=‘utf8‘)
            curor = conn.cursor()
            curor.execute(search_sql)
            result = curor.fetchall()
            curor.close()
            conn.close()
            return result
        except Exception as ex:
            return str(ex)
时间: 2024-10-11 02:50:33

分享一份webdriver自动化脚本的相关文章

Selenium+C#自动化脚本开发学习

Selenium+C#自动化脚本开发知识学习 1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver driver = new FirefoxDriver(); //打开指定的URL地址 driver.Navigate().GoToUrl(@"http://12.99.102.196:9080/corporbank/logon_pro.html"); //关闭浏览器 Driver.quit(); 网银浏览器兼容性测试过程中,关闭浏览器后会有对话框,此问题解决

自动化脚本中运到的一些简单算法

背景简介 在写一个自动化脚本时,要模拟发送网络请求,其中网络请求包含hid.md5.机器是64位还是32位等众多情况,而且这些因子还是不确定的,产品和开发可能会随时更改,这里我们就想通过自动化脚本根据这些因子全排列地自动去生成Case. 这里就怎么写个扩展性较好的生成全排列Case的代码,做下简单分享,具体前因后果请关注另外一篇文章(待发). 问题分析 现在假设就下面这三个case hid md5 64位/32位 123 abc 32 456 cde 64 现在又三个因子,每个因子2种情况,全排

二、运行第一个自动化脚本

1.安装selenium 1.打开命令行,输入命令 pip install selenium,进行安装,安装成功的界面如下图: 验证是否安装成功 打开命令行,输入python 进入python的命令行界面 输入 from selenium import webdriver  没有报错,证明安装成功 2.运行第一个自动化脚本 1.打开pycharm,新建一个项目 输入项目名,test,选择 existing interpreter,就是选择安装python,new environment usin

Linux 多网卡Bond自动化脚本

现状: 目前一台物理机4个网卡,em1-em2-em3-em4,需要实现1.2网卡绑定,3.4网卡绑定,模式为主备.即:一个网卡处于活动状态 ,一个处于备份状态,所有流量都在主链路上处理.当活动网卡down掉时,启用备份的网卡 附:linux有七种网卡绑定模式:0. round robin,1.active-backup,2.load balancing (xor),  3.fault-tolerance (broadcast), 4.lacp,  5.transmit load balanci

Resurrectio-capserjs的自动化脚本录制工具

[根据github上的文档说明整理] Phantom下的任何操作都可以录制 Resurrectio是一个Chrome插件,他可以记录浏览器的操作,并转化成对应的casperjs脚本 Resurrectio使用最简和装饰性的选择器,所以记录的步骤能在布局和设计变化时保持有效 当CasperJS执行JavaScript,录制步骤不限于html交互,js执行的精确步骤也能够被完整记录 Resurrectio也提供了一个方法去生成截屏,在你的测试场景里,并且能够导出注视和截屏在重构后的方法里(意思是这个

我的第一个自动化脚本(python)----百度搜索

这是一个纯小白胡说八道的个人总结,如果有人看到什么不对的,欢迎批评指正 博客园开通了很久,一直不知道该怎么去写,总觉得自己要写的东西都是别人已经写过的,我再去写就是在重复之前人所说,今天去面试和面试姐姐聊到这个问题的时候,我才真正的意识到写博客的意义,其实博客只要是写出来对自己有帮助,管其他人怎么看呢,就算百度搜索出来好多一样的那又怎么样呢,博客是写给自己的,如果能因此帮助别人少走一些弯路又何乐而不为呢 接下来就来我实现这个自动化脚本经历的过程,首先要在火狐浏览器安装selenium IDE:其

老李分享:《Linux Shell脚本攻略》 要点(八)

老李分享:<Linux Shell脚本攻略> 要点(八) 1.打印进程 [[email protected] program_test]# ps -e | head  PID TTY          TIME CMD    1 ?        00:00:03 init    2 ?        00:00:00 kthreadd    3 ?        00:00:00 migration/0    4 ?        00:00:00 ksoftirqd/0    5 ?   

老李分享:《Linux Shell脚本攻略》 要点(二)

老李分享:<Linux Shell脚本攻略> 要点(二) poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478.Linuxshell是测试开发工程师的基本功之一,所以在poptest测试开发课堂上加入了大量的linuxshell的课程,为了学员开发跨平台的测试平台打基础. 1.cat cat -s //多个空白行压缩成一个 cat *.txt | tr -s '\n'   //

老李分享:《Linux Shell脚本攻略》 要点(四)

老李分享:<Linux Shell脚本攻略> 要点(四) 1.IP地址的正则表达式: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} 2.grep用法 //在多级目录中对文本进行递归检索 [[email protected] program_test]# grep "yang" ./ -Rn ./test.txt:6:laoyang./right.txt:1:1 yang man //忽略大小写匹配 [[email protec