Selenium2+python自動化21-TXT資料參數化

來源:互聯網
上載者:User

標籤:click   實現   span   百度搜   自動化   excel   testcase   [1]   for   

前言

     在17篇我們講了excel資料的參數化,有人問了txt資料的參數化該怎麼辦呢,下面小編為你帶你txt資料參數化的講解

一、以百度搜尋為例,自動搜尋五次不同的關鍵字。輸入的資料不同從而引起輸出結果的變化。

     測試指令碼:

#coding=utf-8
from selenium import webdriver
import unittest, time, os
class Login(unittest.TestCase):
def test_login(self):
source = open("D:\\test\\txt.txt", "r")
values = source.readlines()
source.close()
# 執行迴圈
for hzy in values :
driver=webdriver.Firefox()
driver.get("http://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys(hzy)
driver.find_element_by_id("su").click()
time.sleep(2)
driver.close()

    txt檔案:

   

    open方法以唯讀方式(r)開啟本地txt.txt檔案,readlines方法是逐行讀取整個檔案內容。

    通過for迴圈,hzy可以每次擷取到檔案中一行資料,在定位到百度輸入框後,將資料傳入send_keys(hzy)。這樣通過迴圈調用,直到檔案的中的所有內容全被讀取。

二、登入參數化
   現在按照上面的思路,對自動化指令碼中使用者、名密碼進行參數化,通過 python 文檔我們發現 python讀取檔案的方式有:整個檔案讀取、逐行讀取、固定位元組讀取。

   並沒有找到一次讀取兩條資料的好方法。

   建立兩個檔案,分別存放使用者名稱密碼。

  

   測試指令碼:

   #coding=utf-8
   from selenium import webdriver
   from selenium.common.exceptions import NoSuchElementException
   import unittest, time, os
   class Login(unittest.TestCase):
         def test_login(self):
         source = open("D:\\test\\un.txt", "r") #使用者名稱檔案
         un = source.readline() #讀取使用者名稱
         source.close()
         source2 = open("D:\\test\\pw.txt", "r") #密碼檔案
         pw = source2.readline() #讀取密碼
         source2.close()
         driver=webdriver.Firefox()
         driver.get("http://www.baidu.com/")
         driver.maximize_window()
         driver.find_element_by_id("txtusername").clear()
         driver.find_element_by_id("txtusername").send_keys(un)
         driver.find_element_by_id("txtpassword").clear()
         driver.find_element_by_id("txtpassword").send_keys(pw)
         driver.find_element_by_id("userlogin").click()
         time.sleep(2)
         try:
             t = driver.find_element_by_xpath("//form/div[4]/div/div[1]/div[1]/div/a/img")
         except NoSuchElementException:
             assert 0 , u"登入失敗,找不到左上方LOG"
         driver.close()

   本來想用百度的例子的,無奈有驗證碼,麻煩,所以用了公司內網測試環境。

   分別開啟txt檔案,通過un和pw來接收使用者賬戶和密碼資訊,將接收的資料通過send_keys(XX)轉入到執行程式中。

  雖然目的達到了這,但這樣的實現有很多問題:

  1、使用者名稱密碼分別在不同的檔案裡,修改使用者名稱和密碼比較麻煩。
  2、un.txt 和 pw.txt 檔案中只能儲存一個使用者密碼,無能很好的迴圈讀取。

  多個賬戶和密碼的txt檔案,小編還未能找到解決的方法,歡迎大家給出意見和想法。

  多賬戶和密碼建議還是用excel參數化去解決。

 

 

Selenium2+python自動化21-TXT資料參數化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.