Cypress基礎入門

前言

Cypress 由一個免費的、開源的、本地安裝的 Test Runner 和一個用於記錄測試的 Dashboard 服務組成.
Cypress 不受與 Selenium 相同的限制.
cypress可以編寫更快、更容易和更可靠的測試。
cypress能夠測試任何在瀏覽器中運行的東西。css

安裝

  1. 桌面版安裝:
    下載地址: http://download.cypress.io/desktopnode

  2. 命令行安裝
    首先查看本身的npm版本是不是新版本,若是不是最新則進行升級python

npm -v
npm install npm -g

npm install cypress --save-dev

安裝可能會出現速度慢的問題,能夠試一下下面的方法npm

方法1:cnpm命令代替npm

首先安裝cnpm命令瀏覽器

npm install --global cnpm

方法2:使用國內淘寶連接下載

npm install cypress --registry=http://registry.npm.taobao.org

方法3:配置下載連接

npm config set registry http://registry.npm.taobao.org

檢查配置是否成功測試

npm config get registry

啓動

node_modules\.bin\cypress open

啓動cypress若是報文件找不到的錯誤:Can't start server EEXIST: file already exists, mkdir 'C:**\node_modules.bin\cypress'
能夠將命令改成全局啓動
url

node_modules\.bin\cypress open --global

第一個測試用例

建立一個.js的測試文件命令行

describe('個人第一個測試', () => {
    it('測試用例', () => {
        expect(true).to.equal(true);
    }
    )
}
)

打開編寫的測試用例並運行
3d

一個真實的測試用例

describe('個人第一個測試',function(){
    it('百度測試用例:',function(){
        cy.visit('http://www.baidu.com') //訪問url
        cy.title().should('contain','百度一下,你就知道')   //驗證頁面 title 是否正確
        cy.get('#kw').type('python')       //根據 css 定位搜索輸入框
        .should('have.value','python')  //驗證關鍵字自動是否展現正確
        cy.get('#su').click()   //根據 css 定位搜索按鈕並點擊
        cy.url().should('include','wd=python')     //驗證目標url 是否正確包含關鍵字
        cy.title().should('contain','python_百度搜索')  //驗證頁面 title 是否正確
        cy.get('[id="1"]').should('contain','python')    // 驗證第一個結果中是否包含TesterHome
        cy.screenshot()
    })
})

相關文章
相關標籤/搜索