2017-10-19 56 views
0

一條新的路徑,我使用Cypress測試我的Web應用程序。測試重定向到與賽普拉斯

這個片段當前工作,並會提交一個新的東西:

describe('The Create Page',() => { 
    it('successfully creates a thing',() => { 
    cy.visit('/create') 
    cy.get('input[name=description]').type('Hello World') 
    cy.get('button#submit') 
     .click() 

    // After POST'ing this data via AJAX, the web app then 
    // receives the id of the new thing that was just created 
    // and redirects the user to /newthing/:id 
    // How do I test that this redirection worked? 
    }) 
}) 

就像註解說明,我不知道如何測試重定向是否將新的路線的作品。我可以在瀏覽器模擬中看到它的重定向工作,我只是想爲它添加一個測試。

謝謝!

回答

1

你需要做的是斷言,網址是在期望的狀態。

有賽普拉斯很多命令,可以用來做出關於網址的斷言。具體來說,cy.url()cy.location()cy.hash()可滿足您的要求。可能是您的使用情況下,最好的例子是這樣的:

cy.location('pathname').should('eq', '/newthing/:id') 
+0

所以......這個服務器我的web應用程序是POST'ing到可能需要過長...時間響應(如超過10秒)......我仍然得到這個錯誤:賽普拉斯錯誤:超時重試:預計'/創造'等於'/新事物/:身份證'...我可以改變多久柏樹會繼續嘗試的東西?另外,thx的幫助,真的很感激它! – SeanPlusPlus

+1

是的。你可以閱讀這些定時重試的細節[這裏](https://on.cypress.io/introduction-to-cypress#Applying-Timeouts)。 從本質上講,直到你的說法相符賽普拉斯將不斷檢查位置的路徑('/ newthing /:id'),或者直到超時。默認的超時時間是4000ms。 'cy.location( '路徑',{超時:10000})你可以通過傳遞超時選項'cy.location()'像這樣增加這個時候應該( '情商', '/ newthing /:身份證'。 )' –

+0

賓果!超時工作。 Thx soooo !!! – SeanPlusPlus