小编Ond*_* M.的帖子

Cypress.io:将表单输入的值读取并存储到 JS 变量(或常量)中

我在一个简单的页面上有一个 HTML 表单(非常简单,没有像 Ajax 这样的棘手部分,...)。我尝试读取任何输入的默认值(type="text",同样没有技巧)并将其存储在常量中以备后用(断言)。

HTML 看起来像:

<form method="post" ...>
  <input type="text" id="xyz" name="xyz" value="123">
</form>
Run Code Online (Sandbox Code Playgroud)

Cypress 测试,不起作用,看起来像:

describe('General tests', function(){

  context('General', function() {

    it('can read the input and store in const', function () {

        cy.visit('http://localhost/settings')
        const xyz = Cypress.$('#xyz').val()
        cy.log(xyz)

    })

  })
})
Run Code Online (Sandbox Code Playgroud)

这不起作用。但是经过几个小时的播放(偶然地,这在更复杂的测试套件 = 文件中对我有用)。我意识到,如果先前的测试( = it() )访问与上次访问的 URL 相同的 URL,则此构造按预期工作。比它像奇迹一样工作。

有效的赛普拉斯测试看起来像:

describe('General tests', function(){

  context('General', function() {

    it('makes magic and allows the next test to work', function () {

        cy.visit('http://localhost/settings')

    })

    it('can read the input and store …
Run Code Online (Sandbox Code Playgroud)

forms automated-tests cypress

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

automated-tests ×1

cypress ×1

forms ×1