The third of web automation framework get database Value vs. interface Value ~ ~

Source: Internet
Author: User

MySQL is used in the database, the framework involves the database, mainly contains two aspects, one is the completion of each case, insert case-related information and data; A web interface data check, you need to get a row of a column value from SQL and the interface a value to compare.

Description: A large part of the Web test is to compare the data on the interface is accurate, such as some simple data statistics, data basic information, but also data conversion data (0: Normal, 1: inactive); But when considering web data reconciliation, there are two main scenarios, one is to initialize a batch of data, Then the interface data is compared with the initialization data, the second is to take the relevant information data in the database, and then make a comparison with the interface; Considering that the first scenario support is not too good, the Web page data check is a second program, there are two ways of implementation, One is to take a SQL data set and then assemble the interface elements into a data set comparison, the other is to take a section of SQL data collection of a row of a column and the interface of an element comparison.

Web data Check this piece, a tip, what good ideas, good suggestions Welcome to discuss ~ ~ Thank you

Here is the example code (the example only involves data comparison):

#coding =utf-8
‘‘‘
Created on July 4, 2014

@author: A sigh
‘‘‘
Import MySQLdb
Import Sys
Import OS
From selenium import Webdriver
Import time
# reload (SYS)
# reload (SYS). setdefaultencoding (' GBK ')

# def PRINTSTR (**kwargs):
# return KWARGS[0][3]

#初始化查询sql
sql = "SELECT * FROM TestData"

‘‘‘
CREATE TABLE ' TestData ' (
' ID ' double,
' Name ' varchar (600)
);
Insert INTO ' testdata ' (' id ', ' name ') VALUES (' 1 ', ' testname ');
Insert INTO ' testdata ' (' id ', ' name ') VALUES (' 3 ', ' test_ Baidu Translator ');
‘‘‘
#初始化数据库连接参数
Try
Conn =mysqldb.connect (host= "192.168.0.153", port=3306,user= "root", passwd= "mysql123", db= "test", charset= "UTF8")
Print "Link succeeded"
Except Mysqldb.error, E:
Print "Link Failed"

#定义取列
def printstr (R,c,*args):
return Args[0][c]
#封装一个查询方法, incoming parameter SQL, row r, column C
def findData (str,r,c):
cursor = Conn.cursor ()
Cursor.execute (str)
Return Printstr (R,c,cursor.fetchall () [R])
Conn.close ()

def alldata (str):
cursor = Conn.cursor ()
Cursor.execute (str)
For I in Cursor.fetchall ():
Print I
Return "Output alldata complete"
Conn.close ()

#获取web界面某个项值
Browser = Webdriver. Firefox ()
Browser.get ("http://www.baidu.com/")
Time.sleep (3)
browser.find_element_by_id ("Kw1"). Send_keys ("test")
Time.sleep (2)
browser.find_element_by_id ("SU1"). Click ()
Time.sleep (1)

#获取某个值, conversion type
Print "%s"%str (Browser.find_element_by_xpath ("/html/body/div/div[6]/div/div[2]/div/h3/a"). Text)

#数据对比,

Print str (browser.find_element_by_xpath ("/html/body/div/div[6]/div/div[2]/div/h3/a"). Text) = = str (findData (SQL, ))

Time.sleep (1)
Browser.close ()
Browser.quit ()


The third of web automation framework get database Value vs. interface Value ~ ~

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.