我正在使用 cypress 来测试我们的 Web 应用程序,在某些页面中,有不同的端点请求被执行多次,例如 GET /A、GET /B、 GET /A。
为了等待所有请求完成并保证页面已完全加载,赛普拉斯的最佳实践是什么。
我不想使用很多cy.wait()命令来等待所有请求被处理。(每个页面有很多不同的请求组)
我有一个赛普拉斯框架,我正在其中测试用户页面(他们可以在其中与页面交互,下载文件,更新他们的个人资料等),以及该用户页面的管理页面(单独登录,实际上可以作为管理员,修改用户页面中的内容)。因此,我有三个配置文件:
cypress.config-admin.ts,其中我有管理页面的 baseUrl(如www.mypage.admin.com)。我在文件中传递 --config-file 标记package.json以进行无头测试。cypress.config-user.ts我有用户页面的 baseUrl ( www.mypage.com)。我在文件中传递 --config-file 标记package.json以进行无头测试。cypress-config.ts,我用它通过赛普拉斯仪表板手动运行测试。有时我需要在同一个测试用例中使用两个 URL(以管理员身份执行某些操作,以用户身份进行验证)。
我的问题是,当我使用 时Cypress.config().baseUrl,它只会显示 上的值cypress.config.ts。我已经尝试了多种方法,但还没有找到在admin或user配置文件上调用 baseUrl 的方法。
https://docs.cypress.io/api/cypress-api/config似乎建议我可以传递配置文件的名称,但我无法做到这一点=/
有任何想法吗?
我想我们都知道 cypress 和软断言已经被讨论得很厉害,并且有各种实现软解决方案的解决方案。一段时间以来,我一直在使用以下代码,基于旧的堆栈问题赛普拉斯是否支持软断言?:
\nlet isSoftAssertion = false;\nlet errors = [];\n\nchai.softExpect = function ( ...args ) {\n isSoftAssertion = true;\n return chai.expect(...args);\n},\nchai.softAssert = function ( ...args ) {\n isSoftAssertion = true;\n return chai.assert(...args);\n}\n\nconst origAssert = chai.Assertion.prototype.assert;\nchai.Assertion.prototype.assert = function (...args) {\n if ( isSoftAssertion ) {\n try {\n origAssert.call(this, ...args)\n } catch ( error ) {\n errors.push(error);\n }\n isSoftAssertion = false;\n } else {\n\n origAssert.call(this, ...args)\n }\n};\n\n// monkey-patch `Cypress.log` so that the last `cy.then()` isn\'t logged to command log\nconst …Run Code Online (Sandbox Code Playgroud) 我正在尝试覆盖 Cypress.io 中的现有命令。我正在寻找log()路由响应status和路由url来扩展内置route()的功能。不幸的是,我The route undefined had a undefined status code. 在console. 请注意,我暂时使用浏览器console。最终,我将使用log()内置方法。这是我到目前为止所尝试过的:
cypress/support/commands.js
Cypress.Commands.overwrite('route', (originalFn, response) => {
console.log(`The route ${response.url} had a ${response.status} status code.`);
return originalFn(response);
});
Run Code Online (Sandbox Code Playgroud)
更新:
我现在得到了路线,但我仍然没有得到response或status。这是我当前的代码:
Cypress.Commands.overwrite('route', (originalFn, url, response) => {
console.log(`The route ${url} had ${response} status code`);
return originalFn(url, response);
});
Run Code Online (Sandbox Code Playgroud) 我有一个方法可以获取元素的 href 属性hrefAppTheme并检查它是否是 string-array 的值之一appThemes:
describe('...',() => {
it('should ...', () => {
(...)
let defaultAppTheme = '';
onMenuPage.hrefAppTheme.invoke('attr', 'href')
.then(hrefVal => {
if (typeof hrefVal !== 'undefined') {
expect(hrefVal).to.be.oneOf(onMenuPage.appThemes);
defaultAppTheme = hrefVal;
//referencing the variable 'defaultAppTheme' further below...
}
});
Run Code Online (Sandbox Code Playgroud)
可以安全地假设this.hrefAppTheme.invoke('attr', 'href')总是返回用户主题之一(字符串),因为上面的代码工作可靠。
由于事实证明我需要在几个不同的地方使用该逻辑,因此我想将其提取到一个方法中并将其放入页面对象类中。这就是我所拥有的:
export default class MenuPage {
(...)
getUserTheme(): string {
let userTheme = '';
cy.then(() => {
this.hrefAppTheme.invoke('attr', 'href')
.then(resVal => {
if (typeof resVal !== 'undefined') {
userTheme = resVal; …Run Code Online (Sandbox Code Playgroud) 使用cy.request()链接 URL“http://www.alitots.co.uk/”失败并出现以下错误:
请求失败且没有响应
我们尝试向此 URL 发出 http 请求,但请求失败且没有响应。
我们在网络级别收到此错误:
错误:getaddrinfo ENOTFOUND www.alitots.co.uk我们发送的请求是:
方法:GET
URL:http://www.alitots.co.uk/
我该如何处理这个问题并继续检查其他链接?
这是完整的代码:
cy.get("a:not([href*='mailto:'])").each(($link, index) => {
const href=$link.prop('href')
cy.log(href)
if (href) {
cy.request({
url: href,
failOnStatusCode:false,
followRedirect:false,
failOnNetworkError:false
})
.then((response)=>{
if(response.status >= 400 || response.status == '(failed)') {
cy.log(` *** link ${index +1} is Broken Link ***`)
//brokenLinks++
} else {
cy.log(` *** link ${index+1} is Active Link ***`)
}
})
}
})
Run Code Online (Sandbox Code Playgroud) 我正在尝试对演示站点执行鼠标悬停操作,但在执行鼠标悬停时遇到问题。
我想检查悬停时的颜色或文本,但我没有这样做。
网站: https: //demo.nopcommerce.com/
步骤:登录应用程序选择注册,考虑定位器并执行鼠标悬停。
我的代码:
cy.visit('https://demo.nopcommerce.com/')
cy.get('.ico-register')
.trigger('mouseover')
.should('have.css', 'cursor', 'pointer')
.wait(1000) // Adjust the delay time as needed
.should('have.css', 'color', 'rgb(74, 178, 241)')
Run Code Online (Sandbox Code Playgroud)
错误 :
断言预期 <a.ico-register> 具有值为 rgb(74, 178, 241) 的 CSS 属性 color,但值为 rgb(119, 119, 119) AssertionError 预期 '<a.ico-register>' 为CSS 属性“color”的值为“rgb(74, 178, 241)”,但值为“rgb(119, 119, 119)”
注意:1:rgb(119,119,119)这是悬停之前的颜色,它显示的颜色与悬停之后相同
2:在此,当我尝试手动将鼠标悬停在注册选项卡上时,测试用例正在通过
cypress ×7
javascript ×2
typescript ×2
built-in ×1
chai ×1
hover ×1
invoke ×1
methods ×1
node.js ×1
pageobjects ×1
request ×1