我正在嘗試使用 CSV 檔案填寫 HTML 表單。對于他們,我首先將 CSV 檔案轉換為 JSON,然后嘗試使用 JSON 檔案填寫表單。但是,我唯一得到的是錯誤:cy.type() 只能接受字串或數字。你傳入:未定義
我已經檢查了針對同一問題給出的答案,但找不到對我有幫助的答案。我究竟做錯了什么?
在這里,我留下了我首先將檔案從 CSV 傳遞到 JSON 的代碼,然后將其記錄在控制臺中,最后通過表單傳遞它:
<!-- language: js -->
import {parse} from "papaparse"
describe('Convert CSV file in JSON file', function(){
let allData
before(()=>{
cy.readFile('C:/data/cypress/downloads/generatedBy_react-csv.csv')
.then(str =>{
cy.writeFile('C:/data/cypress/fixtures/testDataFromCSV.json', parse(str, {header:true}))
})
cy.fixture('testDataFromCSV.json')
.as('dataJson')
.then(dataJson => {
allData = dataJson
})
})
it('Log the data from the CSV to the JSON file', function(){
allData.data.forEach(data =>{
cy.log(data.firstName)
cy.log(data.lastName)
cy.log(data.email)
cy.log(data.age)
cy.log(data.address)
cy.log(data.vehicle)
cy.log(data.phoneNo)
})
})
it('Use JSON Data in hte HTML form', function(){
allData.data.forEach(data =>{
cy.visit('http://localhost/2iTesting/form.html')
cy.fixture("testDataFromCSV.json")
.then((dataJson)=>{
allData = JSON.stringify(dataJson)
cy.get('[cy-data="firstname"]')
.type(data.firstName)
cy.get('[cy-data="lastname"]')
.type(data.lastName)
cy.get('[cy-data="email"]')
.type(data.email);
cy.get('[cy-data="address"]')
.type(data.address);
cy.get('[cy-data="vehicle"]')
.type(data.vehicle);
cy.get('[cy-data="submit"]')
.click()
cy.wait(1500)
})
})
})
})
uj5u.com熱心網友回復:
只需洗掉該stringify步驟。
我認為您打算這樣做parse,但沒有必要,因為賽普拉斯已經為您做到了。任何帶有.json擴展名的檔案都作為物件傳遞到測驗中。
cy.fixture("testDataFromCSV.json")
.then((dataJson) => {
// allData = JSON.stringify(dataJson) // no need to do this, just use dataJson
cy.get('[cy-data="firstname"]')
.type(dataJson.firstName) // NOTE you have data.firstName
... // which looks like a cut-and-paste error
// from the test above
寫入決議結果的資料部分
papaparse 庫為您提供了一個包含資料、錯誤陣列和元資料的包裝物件。
理想情況下,您希望在撰寫 JSON 之前檢查錯誤,但至少需要撰寫.data決議結果的屬性。
cy.readFile('./cypress/downloads/generatedBy_react-csv.csv').then(str =>{
const result = parse(str, {header:true})
if (result.errors.length) {
// handle errors
}
cy.writeFile('./cypress/fixtures/testDataFromCSV.json', result.data)
})
uj5u.com熱心網友回復:
問題是您的 then 塊的范圍。在此夾具的 then 塊內。cy.fixture('testDataFromCSV.json').as('dataJson').then(dataJson => { allData = dataJson})
allData = dataJson
但是在 then 塊之外alldata=let alldata或者只是未定義。要訪問分配的變數,您需要使用cy.wrap().
cy.fixture('testDataFromCSV.json').as('dataJson')
.then( (dataJson) => {
cy.wrap(dataJson).as(allData)
}
然后訪問您的包裝物件,您只需執行以下操作。
it('Log the data from the CSV to the JSON file', () =>{
cy.get('@allData')
.then( (allData) => {
Object.Values(allData.data).forEach( (value) =>{ //this might be wrong as I'm unsure the data structure of your object
cy.log(value)
}
}
it('Use JSON Data in the HTML form', () =>{
cy.get('@allData')
.then( (dataJson) =>{
allData = JSON.stringify(dataJson)
cy.get('[cy-data="firstname"]')
.type(allData.data.firstName) //and so on from here
}
最后要注意的一件事是,cy.wait(1500)在第二個 it 塊的末尾有一個。我建議cy.intercept().as('api')通過單擊提交來查看您獲得的任何 API 回應。那么它只是cy.wait('api')一個最佳實踐。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/481837.html
