柏树结束的引导模式

发布于 2025-02-04 04:09:37 字数 4221 浏览 2 评论 0原文

试图关闭模式时,它们不会关闭。

我有几种从不同元素调用的自举模式。要显示它们,我执行以下操作:

cy.get('#units_modal')。click(click()

,并且毫无问题地显示模式。

问题是关闭模态时。我想在hook 之后关闭中的对话框,当该的所有测试部分都完成时,我正在说:

describe('Checking vehicles.', function(){
    before(()=>{
        cy.get('#unitsCommandHeaderMenu').click()
        cy.get('#units_modal').should('be.visible')
    })

    after(()=>{
        cy.get('#units_modal')    
        .then(($dialog)=>{
            cy.wrap($dialog).find('button').find('span').contains("×").click()
        });
    })

    it('first test', ()=> {
        cy.log('first test')
    })
    it('second test', ()=> {
        cy.log('second test')
    })
})

从视觉工具中,当我将指针放在点击事件我看到关闭按钮正在正确选择,应该单击它,但模态不会关闭。

甚至尝试使用:关闭Cypress中的角度材料对话框

这是我的HTML代码。

<!-- buttons -->
<ul class="modal">
<li id="first" data-toggle="modal" data-target="#units_modal">
    <span>first</span>
</li>
<li id="second" data-toggle="modal" data-target="#second_modal">
    <span>second</span>
</li>
<li id="third" data-toggle="modal" data-target="#third_modal">
    <span>third</span>
</li>
</ul>


<!-- modals -->

<div class="modal fade" id="units_modal" tabindex="-1" role="dialog" aria-labelledby="firstModal" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <!-- header -->
        </div>
        <div class="modal-body toFill">
            <!-- body -->
        </div>
        <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-modal-update"><span class="modal-update-btn-spinner fa fa-refresh"></span><span class="modal-update-btn-text">Update</span></button>
                    <button type="button" class="btn btn-secondary btn-modal-add"><span class="fa fa-plus"></span><span class="modal-update-btn-text">Add</span></button>
                <button type="button" class="btn btn-secondary btn-modal-close" data-dismiss="modal">Close</button>
        </div>
        </div>
    </div>
</div>

<div class="modal fade" id="second_modal" tabindex="-1" role="dialog" aria-labelledby="secondModal" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <!-- header -->
            </div>
            <div class="modal-body toFill">
                <!-- body -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary btn-modal-close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="third_modal" tabindex="-1" role="dialog" aria-labelledby="thirdModal" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <!-- header -->
            </div>
            <div class="modal-body toFill">
                <!-- body -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-thirdary btn-modal-close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

When trying to close the modals they don't close.

I have several bootstrap modals that are called from different elements. To display them I do the following:

cy.get('#units_modal').click()

And the modal is displayed without problem.

The problem is when closing the modal. I want to close the dialog in the after hook when all the tests of that describe section have finished, I am putting:

describe('Checking vehicles.', function(){
    before(()=>{
        cy.get('#unitsCommandHeaderMenu').click()
        cy.get('#units_modal').should('be.visible')
    })

    after(()=>{
        cy.get('#units_modal')    
        .then(($dialog)=>{
            cy.wrap($dialog).find('button').find('span').contains("×").click()
        });
    })

    it('first test', ()=> {
        cy.log('first test')
    })
    it('second test', ()=> {
        cy.log('second test')
    })
})

From the visual tool, when I put the pointer on the click event I see that the close button is being selected correctly and it should be clicking on it, but the modal does not close.

enter image description here
Even try the solution suggested by: Closing an Angular Material Dialog in Cypress

This is my html code.

<!-- buttons -->
<ul class="modal">
<li id="first" data-toggle="modal" data-target="#units_modal">
    <span>first</span>
</li>
<li id="second" data-toggle="modal" data-target="#second_modal">
    <span>second</span>
</li>
<li id="third" data-toggle="modal" data-target="#third_modal">
    <span>third</span>
</li>
</ul>


<!-- modals -->

<div class="modal fade" id="units_modal" tabindex="-1" role="dialog" aria-labelledby="firstModal" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <!-- header -->
        </div>
        <div class="modal-body toFill">
            <!-- body -->
        </div>
        <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-modal-update"><span class="modal-update-btn-spinner fa fa-refresh"></span><span class="modal-update-btn-text">Update</span></button>
                    <button type="button" class="btn btn-secondary btn-modal-add"><span class="fa fa-plus"></span><span class="modal-update-btn-text">Add</span></button>
                <button type="button" class="btn btn-secondary btn-modal-close" data-dismiss="modal">Close</button>
        </div>
        </div>
    </div>
</div>

<div class="modal fade" id="second_modal" tabindex="-1" role="dialog" aria-labelledby="secondModal" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <!-- header -->
            </div>
            <div class="modal-body toFill">
                <!-- body -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary btn-modal-close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="third_modal" tabindex="-1" role="dialog" aria-labelledby="thirdModal" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <!-- header -->
            </div>
            <div class="modal-body toFill">
                <!-- body -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-thirdary btn-modal-close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-02-11 04:09:37

我认为问题是他们在按钮上的“ X”不是您从键盘上获得的“ X”。

我在下面显示了这两个,因此您可以比较它们。

it('opens and closes the bootstrap modal', () => {
  cy.visit('https://getbootstrap.com/docs/4.0/components/modal/');

  cy.contains('Launch demo modal').click()

  cy.get('#exampleModalLive')  
    .should('be.visible')
    .find('.modal-header')
    .find('button[data-dismiss="modal"]')
    // .should('contain', 'x')             // ❌ fails  (char 120)
    .should('contain', '×')                // ✅ passes (char 215)
    .click()

  cy.get('#exampleModalLive')  
    .should('not.be.visible')
})

但是使差异的行似乎是

实际上,我只是检查了 - 您的“ x”正确,

如果您在测试中不仅有cy.log(),就不会发生问题。


可见性检查似乎使测试代码等待淡入淡出的动画完成(在顶部元素上,请参见class =“模态淡出show”)。

I think the problem is the "x" they have on the button is not the one you get from the keyboard.

I've shown both below so you can compare them.

it('opens and closes the bootstrap modal', () => {
  cy.visit('https://getbootstrap.com/docs/4.0/components/modal/');

  cy.contains('Launch demo modal').click()

  cy.get('#exampleModalLive')  
    .should('be.visible')
    .find('.modal-header')
    .find('button[data-dismiss="modal"]')
    // .should('contain', 'x')             // ❌ fails  (char 120)
    .should('contain', '×')                // ✅ passes (char 215)
    .click()

  cy.get('#exampleModalLive')  
    .should('not.be.visible')
})

Actually, I just checked - you have the right "x", but the line that makes the difference seems to be

.should('be.visible')

after finding the modal (not checked exhaustively)

Maybe if you have more than just a cy.log() in the tests, the problem would not occur.


The visibility checks seem to make the test code wait for the fade animations to complete (see class="modal fade show" on the top element).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文