Data-driven test instances

Source: Internet
Author: User
Tags array definition tagname

There are many types of data-driven forms:

1. How variables are defined

2. How to define arrays and dictionaries

3. How to read files (txt\csv\xml)


1. parameterization by defining variable mode

public.py

Class Login ():
    def user_login (self,driver,username,password):
        driver.find_element_by_xpath ("//input[@ Name= ' email '). Clear ()
        Driver.find_element_by_xpath ("//input[@name = ' email ']"). Send_keys (username)
        Driver.find_element_by_name ("password"). Clear ()
        driver.find_element_by_name ("password"). Send_keys (password )
        driver.find_element_by_id ("Dologin"). Click ()
    def user_logout (self,driver):
        driver.find_element_by _link_text ("Exit"). Click ()
        driver.quit ()

mailtest.py

From selenium import webdriver from public
import Login
class Logintest ():
    def __init__ (self):
        Self.driver=webdriver. Firefox ()
        self.driver.implicitly_wait (self.driver.get) (
        "http://www.126.com")
    def Test_admin_ Login (self):
        username= ' admin '
        password= ' 123 '
        Login (). User_login (Self.driver,username,password)
        Self.driver.quit ()
    def test_guest_login (self):
        username= ' guest '
        password= ' 321 '
        login ( ). User_login (Self.driver,username,password)
        self.driver.quit ()
logintest (). Test_admin_login ()
Logintest (). Test_guest_login ()

2. Parameterization by means of array definition

From selenium import webdriver
search_text=[' python ', ' Chinese ', ' text '] for
text in Search_text:
    driver= Webdriver. Firefox ()
    driver.implicitly_wait (driver.get) (
    "http://www.baidu.com")
    driver.find_element_by_id ( ' kw '). Send_keys (text)
    driver.find_element_by_id (' su '). Click ()
    driver.quit ()
3, through the way to read the TXT file parameterized

Python provides the following methods for reading a TXT file:

Read (): Read the entire file

ReadLine (): reads a row of data

ReadLines (): reads all rows of data

User_info.txt

Zhangsan,123

lisi,456

wangwu,789

user_info.py

User_file=open (' User_info.txt ', ' R ')
Lines=user_file.readlines ()
user_file.close () for line in
lines:
     username=line.split (', ') [0]
     password=line.split (', ') [1]
     print (Username,password)

4, by reading the CSV file method for parameterization

csv_read.py

Import CSV
date=csv.reader (open (' Info.csv ', ' R '))
#循环输出每一行信息 for
user in date:
     print (user)
# Loop out the second column of data for the
user in date:
     print (user[1])
5. parameterization by reading XML files

Info.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<info>

<base>

<platform>Windows</platform>

<browser>Firefox</browser>

<url>http://www.baidu.com</url>

<login username= "admin" password= "123456"/>

<login username= "Guest" password= "654321"/>

</base>

<test>

<province> Beijing </province>

<province> Guangdong </province>

<city> Shenzhen </city>

<city> Zhuhai </city>

<province> Zhejiang </province>

<city> Hangzhou </city>

</test>

</info>

From xml.dom import minidom
#打开xml文档
dom=minidom.parse (' info.xml ')
#得到文档元素对象
root= Dom.documentelement
Print (root.nodename)
print (root.nodevalue) print (
root.nodetype)
print (root . Element_node)
#获得任意标签名
tagname=root.getelementsbytagname (' browser ')
print (tagname[0].tagname)
#获得标签的属性值
tagname=root.getelementsbytagname (' login ')
username=tagname[0].getattribute (" Username ")
print (username)
Password=tagname[0].getattribute (" password ")
print (password)
Username=tagname[1].getattribute ("username")
print (username)
Password=tagname[1].getattribute (" Password ")
print (password)
#获得标签对之间的数据
provinces=root.getelementsbytagname (' Province ')
Citys=root.getelementsbytagname (' city ')
p2=provinces[1].firstchild.data
C1=citys[0].firstchild.data
c2=citys[1].firstchild.data
Print (p2)
print (c1)
print (C2)



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.