最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)Selenium自動化Page模式

 更新時間:2019年07月14日 10:38:39   作者:Alvin_Xu  
這篇文章主要介紹了Python實現(xiàn)Selenium自動化Page模式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Selenium是當(dāng)前主流的web自動化工具,提供了多種瀏覽器的支持(Chrome,Firefox, IE等等),當(dāng)然大家也可以用自己喜歡的語言(Java,C#,Python等)來寫用例,很容易上手。當(dāng)大家寫完第一個自動化用例的時候肯定感覺”哇...好牛x“,但是大家用余光掃了一下代碼后,內(nèi)心也許是崩潰的,因為太亂了!像這樣:

__author__ = 'xua'

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

class TCRepeatLogin(unittest.TestCase):
  def setUp(self):

    #webdriver
    self.driver = webdriver.Chrome(r'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe')
    self.driver.implicitly_wait(30)
    self.base_url = "http://10.222.30.145:9000/"

  def test_(self):
    driver = self.driver
    driver.get(self.base_url)

    #enter username and password
    driver.find_element_by_id("username").clear()
    driver.find_element_by_id("username").send_keys("sbxadmin")
    driver.find_element_by_id("password").clear()
    driver.find_element_by_id("password").send_keys("IGTtest1"+Keys.RETURN)

    #find dialog and check
    dialogTitle = driver.find_element(By.XPATH,'//html/body/div[7]/div/div/div[1]/h3')
    self.assertEqual("Sign in",dialogTitle.text)

    #find cancel button and click
    cancelBtn = driver.find_element(By.XPATH,'//html/body/div[7]/div/div/div[3]/button[2]')
    cancelBtn.click()

  def tearDown(self):
    self.driver.close()

if __name__ == "__main__":
  unittest.main()

從幾點來分析下上邊的代碼:

1. 易讀性:非常難理解。這么多find element?這難道也是test case?

2. 可擴展性:都是一個個孤立的test case,無擴展性可言

3. 可復(fù)用性:無公共方法,很難提到復(fù)用

4. 可維護性:一旦頁面元素修改,則需要相應(yīng)修改所有相關(guān)用例,effort大

基于以上的問題,Python為我們提供了Page模式來管理測試,它大概是這樣子的:(TestCase中的虛線箭頭應(yīng)該是指向各個page,家里電腦沒裝修改軟件,就不改了:))

關(guān)于Page模式:

1. 抽象出來一個BasePage基類,它包含一個指向Selenium.webdriver的屬性

2. 每一個webpage都繼承自BasePage基類,通過driver來獲取本頁面的元素,每個頁面的操作都抽象為一個個方法

3. TestCase繼承自unittest.Testcase類,并依賴相應(yīng)的Page類來實現(xiàn)相應(yīng)的test case步驟

利用Page模式實現(xiàn)上邊的用例,代碼如下:

BasePage.py:

__author__ = 'xua'

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


#super class
class BasePage(object):
  def __init__(self, driver):
    self.driver = driver


class LoginPage(BasePage):
  
  #page element identifier
  usename = (By.ID,'username')
  password = (By.ID, 'password')
  dialogTitle = (By.XPATH,'//html/body/div[7]/div/div/div[1]/h3')
  cancelButton = (By.XPATH,'//html/body/div[7]/div/div/div[3]/button[2]')

  #Get username textbox and input username
  def set_username(self,username):
    name = self.driver.find_element(*LoginPage.usename)
    name.send_keys(username)
  
  #Get password textbox and input password, then hit return
  def set_password(self, password):
    pwd = self.driver.find_element(*LoginPage.password)
    pwd.send_keys(password + Keys.RETURN)

  #Get pop up dialog title
  def get_DiaglogTitle(self):
    digTitle = self.driver.find_element(*LoginPage.dialogTitle)
    return digTitle.text

  #Get "cancel" button and then click
  def click_cancel(self):
    cancelbtn = self.driver.find_element(*LoginPage.cancelButton)
    cancelbtn.click()

Test_Login.py:

__author__ = 'xua'

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import unittest
import time
import BasePage

class Test_Login(unittest.TestCase):

  #Setup
  def setUp(self):
    self.driver = webdriver.Chrome(r'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe')
    self.driver.implicitly_wait(30)
    self.base_url = "http://10.222.30.145:9000/"
  #tearDown
  def tearDown(self):
    self.driver.close()

  def test_Login(self):
    #Step1: open base site
    self.driver.get(self.base_url)
    #Step2: Open Login page
    login_page = BasePage.LoginPage(self.driver)
    #Step3: Enter username
    login_page.set_username("sbXadmin")
    #Step4: Enter password
    login_page.set_password("IGTtest1")
    #Checkpoint1: Check popup dialog title
    self.assertEqual(login_page.get_DiaglogTitle(),"Sign in")
    #Step5: Cancel dialog
    login_page.click_cancel()


if __name__ == "__main__":
  unittest.main()

Ok, 那么我們回頭來看,Page模式是否解決了上邊的四個方面的問題:

1. 易讀性: 現(xiàn)在單看test_login方法,確實有點test case的樣子了,每一步都很明了

2. 可擴展性:由于把每個page的元素操作都集成到一個page類中,所以增刪改查都和方便

3. 可復(fù)用性: page的基本操作都變成了一個個的方法,在不同的test case中可以重復(fù)使用

4. 可維護性:如果頁面修改,只需修改相應(yīng)page類中的方法即可,無需修改每個test case

總結(jié):

Page模式給我們提供了一個很好的頁面和用例實現(xiàn)的分離機制,降低了耦合,提高了內(nèi)聚,可以使我們在web自動化中做到游刃有余。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

高唐县| 南平市| 垦利县| 土默特左旗| 花莲县| 易门县| 西青区| 斗六市| 邳州市| 日喀则市| 东阿县| 建德市| 惠水县| 成武县| 岐山县| 图木舒克市| 清流县| 丰镇市| 安泽县| 江达县| 安丘市| 隆德县| 宣化县| 斗六市| 汝城县| 沾化县| 陈巴尔虎旗| 梁山县| 高雄市| 南平市| 闵行区| 栖霞市| 饶河县| 保靖县| 陆川县| 余干县| 金阳县| 黄梅县| 稷山县| 南阳市| 贵港市|