我刚开始学习JavaScript并且一直在使用赛普拉斯来自动化一些回归测试.我当前编写的测试旨在验证链接的文本和页眉和页脚中的href.
我遇到的问题是这些测试需要在各种环境中运行,我似乎无法访问cypress.json中设置的baseUrl属性,以便在我的断言中设置域.
在随后的脚本中是行cy.get("a").should("have.attr", "href", baseUrl + footerLink.link):
it.only("translates the content info section", () => {
cy.wrap(orbitData).each(service => {
cy.visit(service.name);
cy.get("#orb-contentinfo > div > ul > li").each(($li, index) => {
let footerLink = service.links[index]
cy.wrap($li).should("have.text", footerLink.linkText)
.within(($li) => {
cy.get("a").should("have.attr", "href", baseUrl + footerLink.link)
});
});
});
});Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了很多东西,我有点不好意思将它们全部列出来,我是新手,所以他们可能是疯了,绝对只是猜测; 其中包括Cypress.env('CYPRESS_baseUrl')和Cypress.baseUrl.每次它都回来了undefined.
或者,如果我以完全错误的方式攻击这一点,那么任何有关更好方法的指导都将受到赞赏.感谢任何帮助或指导,我将不胜感激.
我在 Cypress support/index.js 中有一个函数,用于获取 和 的尺寸cy.document outerWidth,outerHeight然后返回它们以供将来在测试中使用。我的问题是,当测试运行并将这些值与其他值进行比较时,断言说这些值是NaN。我通过控制台记录断言时的值进行检查,它是空的,所以我一定做错了什么,我只是不确定是什么。我的功能如下,非常感谢您的帮助,谢谢。
function getViewport() {
var viewport = {}
cy.document().then((doc) => {
let width = Cypress.$(doc).outerWidth()
let height = Cypress.$(doc).outerHeight()
viewport['bottom'] = height
viewport['height'] = height
viewport['left'] = 0
viewport['right'] = width
viewport['top'] = 0
viewport['width'] = width
viewport['x'] = 0
viewport['y'] = 0
}).then(() => {
return viewport
})
return viewport
}
Run Code Online (Sandbox Code Playgroud)
调用的代码getViewport()是
export const getRect = (obj) => {
var rect
if (obj == 'viewport') …Run Code Online (Sandbox Code Playgroud) 我一直在尝试创建自己的自定义 chai 断言(基于 Cypress 配方模板:https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/extending-cypress__chai-assertions/cypress /support/index.js)。
我在下面的代码中发现,当它运行时,我最终会得到一个恒定的循环WRAP,如果我this.obj与element它交换,则会产生一个恒定的流GET。我似乎从未取得过比getRect(first).then((actual)
如果有人能帮助我,我将非常感激。
赛普拉斯/集成/test.js
describe('testing custom chai', () => {
it('uses a custom chai helper', () => {
cy.visit('https://www.bbc.co.uk/news');
cy.get('#orb-modules > header').should('be.leftAligned', '#orb-header');
});
});
Run Code Online (Sandbox Code Playgroud)
赛普拉斯/支持/index.js
function getRect(selector) {
if (selector === '&document') {
return cy.document().then(doc => doc.documentElement.getBoundingClientRect());
} if (typeof selector === 'string') {
return cy.get(selector).then($elem => $elem[0].getBoundingClientRect());
}
return cy.wrap(selector).then(elem => Cypress.$(elem)[0].getBoundingClientRect());
}
function getRects(first, second) {
return getRect(first).then((actual) => {
getRect(second).then(expected …Run Code Online (Sandbox Code Playgroud)