Share a copy of the Webdriver Automation script

Source: Internet
Author: User

UI Automation for back-end system searches is primarily more consistent than expected for page query results (that is, database query results)
search.py

#-*-Coding:utf8-*-ImportHtmltestrunnerImportTimeImportUnitTestImportPublic fromSeleniumImportWebdriver class Search(unittest. TestCase):     def setUp(self):Self.driver = Webdriver. Firefox () public. Public (). Login_success (Self.driver) Self.driver.find_element_by_link_text ("App list"). 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"    #根据名称进行查询, enter multiple sets of test data     def test_search_by_name(self):Self.search_by_name ("CC") Self.search_by_name ("Sports") Self.search_by_name ("!") Self.search_by_name ("*") Self.search_by_name ("0")#根据包名进行查询, enter multiple sets of test data     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")#根据开发者进行查询, enter multiple sets of test data     def test_search_by_developer(self):Self.search_by_developer ("Baidu") Self.search_by_developer ("XXX") Self.search_by_developer ("*") Self.search_by_developer ("0")#根据标签进行查询, enter multiple sets of test data     def test_search_by_tag(self):Self.search_by_tag ("Pinyin") Self.search_by_tag ("Google") Self.search_by_tag ("163") Self.search_by_tag ("*") Self.search_by_tag ("0")#下拉菜单选择分类后, display the data under the corresponding category     def test_search_by_category(self):All_options = Self.category.find_elements_by_tag_name ("option") forOptioninchAll_options:option.click () Time.sleep (2) actual_search_count = Int (self.count_text.text[3: Len (Self.count_text.text)-1])ifOption.text = ="All": 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)#下拉菜单选择来源后, displaying data from the corresponding source     def test_search_by_source(self):All_options = Self.source.find_elements_by_tag_name ("option") forOptioninchAll_options:option.click () Time.sleep (2) actual_search_count = Int (self.count_text.text[3: Len (Self.count_text.text)-1])ifOption.text = ="All": Search_sql ="SELECT COUNT (*) from app where Status!=-1 and status!=4;"            Else:ifOption.text = ="Local upload": Source_value =0                elifOption.text = ="Pea pod": Source_value =1                elifOption.text = ="App Bao": Source_value =2                elifOption.text = ="Developer Center": Source_value =3                elifOption.text = ="Nine Tour": Source_value =4Search_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)# #下拉菜单选择状态后, display the data in the corresponding State     def test_search_by_status(self):All_options = Self.status.find_elements_by_tag_name ("option") forOptioninchAll_options:option.click () Time.sleep (2) actual_search_count = Int (self.count_text.text[3: Len (Self.count_text.text)-1])ifOption.text = ="All": Search_sql ="SELECT COUNT (*) from app where Status!=-1 and status!=4;"            Else:ifOption.text = ="Pending Review": Status_value =2                elifOption.text = ="Already on the shelf": Status_value =1                elifOption.text = ="off Shelf": Status_value =0                elifOption.text = ="Refuse to shelves": Status_value =-2                elifOption.text = ="off Shelf": Status_value =0                elifOption.text = ="temporary": Status_value =3                elifOption.text = ="pre-release": Status_value =5Search_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 the 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-*-ImportTimeImportPymysql 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 () cu Ror.close () Conn.close ()returnResultexceptException asExreturnSTR (ex)

Share a copy of the Webdriver Automation script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.