gpt4 book ai didi

javascript - 无法在 puppeteer-jest 测试框架中解析本地 JSON 数据?

转载 作者:行者123 更新时间:2023-11-30 19:19:54 27 4
gpt4 key购买 nike

<分区>

我需要使用 Jest+Puppeteer 测试产品。我在页面对象的不同位置从前端定位定位器,我需要将该 JSON 导入页面对象。但是当我尝试使用 JSON.parse() 进行解析时,我收到:

SyntaxError: Unexpected token.

所有研究 Stack Overflow 中的所有问题,但没有任何帮助。

页面对象:

import { waitForNavigation, click, type, pressEnter} from './base';
import { listsPageLocators } from '../constants/locators/listPage.json';

async function clickSearchByCompanyName(page){
click(page, listsPageLocators.searchByCompanyNameBtn);
waitForNavigation(page);
}

JSON:

{
"findNewProspectsBtn" : {
"locator" : "li.search-leads",
"name" : "Find New Prospects Button"
},
"searchByCompanyNameBtn" : {
"locator" : "li.company-name > a",
"name" : "Search By Company Name Button"
},
"leadEnrichmentBtn" : {
"locator" : "li.lead-enrichment",
"name" : "Lead Enrichment Button"
},
"importUrlsBtn" : {
"locator" : "li.import-urls",
"name" : "Import URLs Button"
},
"listsNameFld" : {
"locator" : "span.name",
"name": "List Name Field"
},
"searchIndustryOrCompanyField" : {
"locator" : "input.select2-search__field",
"name" : "Search Industry Or Company Field"
},
"advancedSearchBtn" : {
"locator" : "a[ng-click='goToAdvancedSearch()']",
"name" : "Advanced Search Button"
}
}

具有点击方法的基页对象:

async function click(page, JsonObject, text) {
console.log(JsonObject);
const locatorData = JSON.parse(JsonObject);
if (typeof text !== 'undefined') {
const el = await selectContains(page).getElement(`${locatorData.locator}:contains(${text})`);
el.click();
console.log(`Click on ${locatorData.name} which contains ${text}`);
} else {
await page.click(locatorData.locator);
console.log(`Click on ${locatorData.name}`);
}
}

错误:

● 单个公司页面测试:› C297679 - 从列表中打开公司页面

SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)

54 | async function click(page, JsonObject, text) {
55 | console.log(JsonObject);
> 56 | const locatorData = JSON.parse(JsonObject);
| ^
57 | if (typeof text !== 'undefined') {
58 | const el = await selectContains(page).getElement(`${locatorData.locator}:contains(${text})`);
59 | el.click();

at parse (pages/base.js:56:30)
at Object.clickViewList (pages/add-company-by-name-popup.js:29:5)
at Object.clickViewList (company-page-e2e.js:29:35)

我希望可以从不同的 JSON 导入定位器并在页面对象中实现它。但实际上,我收到一个错误。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com