我正在尝试实现以下功能
在阻止之前:调用Cy.visit(“ / login”)并调用一个函数,该函数将触发REST API并处理REST API响应并设置本地存储。
仅在设置本地存储后,单击“我的帐户”链接
这是我正在尝试的源代码。
import * as subscriberHelpers from '../../../helpers/subscriberHelpers';
import * as localStorage from '../../../helpers/localStorage';
describe('testCode', () => {
before((done) => {
cy.visit('/login', {
timeout: 10000,
onLoad: () => {
localStorage.write("CD-Environment", Cypress.env('defaultEnvironment'));
localStorage.write("CD-Language", "en-US");
localStorage.write("CD-SystemId", "85788485-e411-48a9-b478-610c1285dc1a");
}
})
subscriberHelpers.createSubscriber().then(()=>{
done();
})
})
it('sClick on my account link', () => {
cy.get('.c-header-listItem > .c-link').contains("My Account").click();
})
})
这是createSubscriber函数的代码
export function createSubscriber() {
let URL = `SOME URL`;
let body = {
Some Body
}
return new Promise((resolve, reject) => {
request.subscriberServiceRequest(URL, body).then((response) => {
if (response.status === 200 && ("SessionId" in response.body)) {
localStorage.write("CD-SessionId", response.body.SessionId);
localStorage.write("CD-SubscriberId", response.body.Subscriber.Id);
resolve();
}
else if (response.status === 200 && ("Fault" in response.body)) {
reject(response.body.Fault.Message);
}
})
})
}
这是订户服务请求功能的代码
export function subscriberServiceRequest(url, body, headers = null) {
let defaultHeaders = { "CD-SystemId": "85788485-e411-48a9-b478-610c1285dc1a" }
if (headers != null) {
defaultHeaders = addHeaders(defaultHeaders, headers);
}
return new Cypress.Promise((resolve, reject) => {
cy.request({
url: url,
method: 'POST',
body: body,
headers: defaultHeaders
}).then((response) => {
resolve(response);
});
})
}
当我尝试执行代码时,我在赛普拉斯中遇到以下错误
但是UI中存在的元素
问题:
答案 0 :(得分:0)
要回答第一个问题:
为什么我遇到错误
.contains()
专门搜索元素,但不包括被调用的标签。换句话说,someElement.contains("My Account")
将不匹配someElement
。
您应该拥有的是这个
cy.get('.c-header-listItem').contains("My Account").click();
或者简单地:
cy.contains("My Account").click();