Cypress web自動化17-fixture加載json文件數據


前言

面試時間經常被問到:你的測試數據放哪?有沒有做到測試數據和代碼的分類?
Cypress 使用cypress/fixture 目錄存放 json 文件數據, cy.fixture() 加載測試數據。
官方文檔參考https://docs.cypress.io/api/commands/fixture.html#Syntax

fixture 使用

在 cypress/fixture 目錄寫個 login.json 文件,內容如下

{
  "username": "admin",
  "password": "123456"
}

使用 cy.fixture("filename.json") 讀取 json 文件數據.
先不訪問網頁,使用 cy.log() 查看數據是否讀取到

/**
 * Created by dell on 2020/5/14.
 * 作者:上海-悠悠 交流QQ群:939110556
 */


describe('登陸web網站案例', function() {
    beforeEach(() => {
          //cy.visit('http://xxxx:8080/zentao/user-login.html');
          cy.fixture('login.json').as('login')
        })

    it("登陸案例", function ()
    {
        cy.log("讀取login.json文件賬號:"+this.login.username)
        cy.log("讀取login.json文件密碼:"+this.login.password)
    })
    })

運行結果

登錄案例

接下來把賬號和密碼放 login.json 文件,登錄的網頁的時候使用 fixture 讀取數據

{
  "username": "admin",
  "password": "Yoyo123456",
  "include": "/zentao/my/",
  "text": "我的地盤",
  "zentaosid": "exist"
}

登錄 web 網站按鈕參考前面這篇https://www.cnblogs.com/yoyoketang/p/12873091.html

/**
 * Created by dell on 2020/5/14.
 * 作者:上海-悠悠 交流QQ群:939110556
 */


describe('登陸web網站案例', function() {
    beforeEach(() => {
          cy.visit('http://ip:8080/zentao/user-login.html');
          cy.fixture('login.json').as('login')
        })

    it("登陸案例", function ()
    {
        cy.log("讀取login.json文件賬號:"+this.login.username)
        cy.log("讀取login.json文件密碼:"+this.login.password)
        // let 定義變量
        let username = this.login.username
        let password = this.login.password
        let include = this.login.include
        let text = this.login.text
        let zentaosid = this.login.zentaosid
        //輸入用戶名
        cy.get('#account').type(username)
              .should('have.value', username)
        // 輸入密碼
        cy.get('[name="password"]').type(password)
              .should('have.value', password)
        // 提交表單
        cy.get('#submit').click()
        // 判斷頁面跳轉到 /zentao/my/
        cy.url().should('include', include)
        // and '歡迎您:admin' in page
        cy.get('body').should('contain', text)
        // 判斷存在cookie值 'zentaosid'
        cy.getCookie('zentaosid').should(zentaosid)
    })
    })

運行結果

QQ交流群:939110556


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM