zoukankan      html  css  js  c++  java
  • 自动化测试基础篇--Selenium中数据参数化之TXT

    摘自https://www.cnblogs.com/sanzangTst/p/7722594.html

    一、搜索参数化

    在TXT文件中保存需要搜索的内容:
    测试代码:
    复制代码
     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # @File    : txt.py
     4 # @Software: PyCharm
     5 from selenium import webdriver
     6 import time
     7 
     8 url = 'https://www.baidu.com'
     9 browser = webdriver.Firefox()
    10 browser.get(url)
    11 # 打开test文件
    12 file = open('C:\Users\Administrator\Desktop\test.txt')
    13 # 逐行读取整个文件内容
    14 lines = file.readlines()
    15 
    16 for i in lines:
    17     browser.find_element_by_id('kw').send_keys(i)
    18     browser.find_element_by_id('su').click()
    19     time.sleep(3)
    20     browser.close()
    21 file.close()
    22 browser.quit()
    复制代码

    二、登录参数化

      

      测试代码:

    复制代码
     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # @File    : txts.py
     4 # @Software: PyCharm
     5 from selenium import webdriver
     6 import time
     7 
     8 url = 'http://www.yeah.net/'
     9 browser = webdriver.Firefox()
    10 browser.get(url)
    11 
    12 file = open('C:\Users\Administrator\Desktop\test.txt')
    13 line = file.readline()
    14 (username, password) = line.strip('
    ').split(',')
    15 
    16 browser.switch_to.frame('x-URS-iframe')
    17 browser.find_element_by_name('email').send_keys(username)
    18 browser.find_element_by_name('password').send_keys(password)
    19 browser.find_element_by_id('dologin').click()
    20 time.sleep(5)
    21 
    22 browser.switch_to.default_content()
    23 # 简单判断登录是否成功
    24 name = browser.find_element_by_id("spnUid").text
    25 print(name)
    26 if name == 'sanzang520@yeah.net':
    27     print(u'登录成功')
    28 else:
    29     print(u'登录失败')
    30 browser.find_element_by_link_text('退出').click()
    31 browser.quit()
    复制代码
  • 相关阅读:
    个税
    MC9S08中断机制
    各大公司面试笔试
    uc/OSII 任务切换
    Source Insight 使用
    充70送70
    兔年大年30
    pip更新后报No module named pip
    MsSql Md5
    iOS UIImage扩展方法(category):放大、旋转、合并UIImage、增加渐变层、添加阴影、调节透明度、保存到相册
  • 原文地址:https://www.cnblogs.com/yuer20180726/p/10789413.html
Copyright © 2011-2022 走看看