iT邦幫忙

1

cypress的問題

  • 分享至 

  • twitterImage

這是CYPRESS官方的範例檔

請問route取得的'comments/*'是什麼意思,
我知道是url,但我從頭到尾都沒有看到這個網址被執行。
我也不曉得,點了那個按鈕後,為什麼要呼叫路由。
我的認知會覺得是不要要換頁。
也看了官方文件了,但還是看不太懂,請大家幫忙,謝謝。

  it('.as() - 給路由添加別名以供以後使用', () => {
    cy.server()
    cy.route('GET', 'comments/*').as('getComment')
    cy.get('.network-btn').click()
    cy.wait('@getComment').its('status').should('eq', 200)
  })

完整的文檔:

context('Aliasing', () => {
  beforeEach(() => {
    cy.visit('https://example.cypress.io/commands/aliasing')
  })      

cy.get('.as-table')
  .find('tbody>tr').first()
  .find('td').first()
  .find('button').as('firstBtn')

// To reference the alias we created, we place an
// @ in front of its name
cy.get('@firstBtn').click()

cy.get('@firstBtn')
  .should('have.class', 'btn-success')
  .and('contain', 'Changed')

// Alias the route to wait for its response

cy.server()
cy.route('GET', 'comments/*').as('getComment')

// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.network-btn').click()

// https://on.cypress.io/wait
cy.wait('@getComment').its('status').should('eq', 200)

})

找到了,在開發者工具下面有一個network/headers
https://ithelp.ithome.com.tw/upload/images/20200925/20122648eWLNf83lDr.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
fillano
iT邦超人 1 級 ‧ 2020-09-24 19:44:55
最佳解答

https://docs.cypress.io/api/commands/route.html#Syntax

Route的文件說:目前只支援攔截XMLHttpRequest...可以從這裡推敲,範例這一段就是在驗證按下某按鈕後的ajax動作。

fillano iT邦超人 1 級 ‧ 2020-09-24 19:52:51 檢舉

話說看到標題,我還在想:有人來問微控制器的問題了嗎XD

jbuduoo iT邦新手 4 級 ‧ 2020-09-25 09:08:37 檢舉

謝謝

2

我不知道你是否懂所謂的ajax。
跟ajax的定義很像。

再多一個提示。
href = route

route 不一定是 href

不知道這樣說明是否能懂?
要不然用比較白話簡單的說法。
那段程式碼就是背景執行的東西。你當然看不到網址。
但你開debug模式一定可以看到它。

fillano iT邦超人 1 級 ‧ 2020-09-25 08:12:16 檢舉

文件開頭就有「注意」:

Note: cy.route() assumes you are already familiar with core concepts such as network requests

jbuduoo iT邦新手 4 級 ‧ 2020-09-25 09:08:28 檢舉

謝謝

我要發表回答

立即登入回答