我有一个动态生成的下拉列表,其正常结构如下:
<select>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
etc.
</select>
Run Code Online (Sandbox Code Playgroud)
这是一种分页,我不知道表中有多少项目,也不想根据项目计数计算下拉列表中有多少个选项。
我想通过 Cypress 实现的目标就是始终选择最后一个选项。
我知道我可以按名称或值选择任何选项,例如cy.get('select').select('10')
还有索引:cy.get('select').select(0)
但我不知道最后一个索引。
我也尝试过select().last(),但这不起作用,因为select不能为空。
我想创建执行以下操作的 Cypress 脚本:
1. 可以工作,但不理想
当我通过访问启动脚本时,我确实设法让它工作
cy.visit('https://localhost:3000/login')
Run Code Online (Sandbox Code Playgroud)
但后来我有两个问题:
A. 其余代码需要位于 origin 函数中:
cy.origin('https://localhost:3000/login', () => {
cy.visit('https://localhost:3000')
// test script
})
Run Code Online (Sandbox Code Playgroud)
B、更重要的是:
2. 不工作,理想
所以我想通过访问以正确的方式启动脚本
cy.visit('https://localhost:3000/')
Run Code Online (Sandbox Code Playgroud)
然后点击Log In按钮
cy.contains('Log In')
.click()
Run Code Online (Sandbox Code Playgroud)
这是棘手的部分 - 一旦用户单击按钮,Log InURL https://localhost:3000/login(状态代码302)会将用户重定向到
https://staging.XXX.com/login?redirect_uri=https%3A%2F%2Flocalhost%3A3000%2Freturn_from_login&client_id=CLIENT_ID&scope=%7B%22full%22%3A+true%7D
Run Code Online (Sandbox Code Playgroud)
一旦我让脚本以这种方式工作,我就会遇到两个问题:
A。Chrome 要求我允许重定向
b. 一旦允许,我将被重定向到 Cypress 之外,因此左侧边栏完全消失
有人知道如何解决这个问题吗?
整个代码是
describe('User Session + Start a Discussion', () => {
const username = "username";
const password = "password";
before(() => { …Run Code Online (Sandbox Code Playgroud) 我在 Cypress 测试中使用了 softAssertions。我能够使用softAssert()方法来验证元素中的文本,但我试图弄清楚如何使用softAssert()方法来断言元素可见。在 cypress 中,我使用.should('be.visible')很简单,但我似乎无法使其与softAssert()方法一起使用。我试图断言的元素是输入字段、表格和按钮。我在下面举了一个简单的例子。
我就是这样做的:
const jsonAssertion = require("soft-assert")
Cypress.Commands.add('softAssert', (actual, expected, message) => {
jsonAssertion.softAssert(actual, expected, message)
if (jsonAssertion.jsonDiffArray.length) {
jsonAssertion.jsonDiffArray.forEach(diff => {
const log = Cypress.log({
name: 'Soft assertion error',
displayName: 'softAssert',
message: diff.error.message
})
})
}
});
Cypress.Commands.add('softContains', (actual, expected, message) => {
jsonAssertion.softContains(actual, expected, message)
if (jsonAssertion.jsonDiffArray.length) {
jsonAssertion.jsonDiffArray.forEach(diff => {
const log = Cypress.log({
name: 'Soft assertion error',
displayName: 'softContains',
message: diff.error.message
})
})
} …Run Code Online (Sandbox Code Playgroud) 如果我有一个包含文本“xyzzy”的元素,但我想对照所有这些进行检查:“par”或“xyzzy”或“arc”,我们该怎么做?
我无法用这个得到它:
.should('have.text','Ab demo 1').and('have.text','ab demo 2')
Run Code Online (Sandbox Code Playgroud)
我们可以用“或”代替“和”吗?
我在这个网站上看到过其他类似的问题,但没有一个对我来说有令人满意的解决方案。我没有任何 webpack.config.js 文件,因为我们从 Angular 获取默认配置。请参阅下面相应的图片,以便更好地了解我的问题。
我的项目中的其他配置:
包.json
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true,
"json": {
"enabled": true
},
"stepDefinitions": "**/cypress/e2e/**/*.js",
"step_definitions": "**/cypress/e2e/**/*.js"
},
Run Code Online (Sandbox Code Playgroud)
赛普拉斯.config.ts
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./cypress/plugins/index.js')(on, config)
},
specPattern: '**/e2e/**/*.feature',
"supportFile": false,
chromeWebSecurity: false
},
})
Run Code Online (Sandbox Code Playgroud)
更新 1: 您好@Wirtuald,感谢您回复我。我开始在一个相当复杂的角度项目中遇到这个问题。因此,我从头开始创建了一个基本项目,但仍然遇到同样的问题。接下来,我向大家介绍一下这个新项目的全部信息:
我没有“插件”文件夹
package.json 上的版本:
"devDependencies": {
"@badeball/cypress-cucumber-preprocessor": "^11.2.0", …Run Code Online (Sandbox Code Playgroud)cucumber webpack angular cypress cypress-cucumber-preprocessor
我希望能够通过修改 的值在任何 URL 上运行我的 Cypress 脚本,baseUrl但该命令不会更改它。
"cypress open --env version=development --config baseUrl=https://google.com"
Run Code Online (Sandbox Code Playgroud)
我也尝试过 env 变量,但这也不起作用:
"cypress:open:dev": "cypress open --env version=development,baseUrl=https://google.com"
Run Code Online (Sandbox Code Playgroud)
配置文件:
export default defineConfig({
e2e: {
async setupNodeEvents(on, config) {
const version = config.env.version || 'development'
const configFile = await import(path.join(
config.projectRoot,
'cypress/config',
`${version}.json`
));
const credentialsFile = await import(path.join(
config.projectRoot,
'cypress/config',
'credentials.json'
));
config = {
...config, // take config defined in this file
...configFile // merge/override from the external file
}
config.env = {
...config.env, // …Run Code Online (Sandbox Code Playgroud) cypress ×6
angular ×1
automation ×1
cucumber ×1
cypress-cucumber-preprocessor ×1
node.js ×1
npm ×1
or-operator ×1
package.json ×1
select ×1
webpack ×1