简体   繁体   English

如何在屏幕上显示警报消息之前单击Web元素?

[英]How to click on a web element until alert message is visible in screen?

In my application there is a Click to add email link , on clicking on the same link multiple times , alert message comes that you have reached the maximum limit. 在我的应用程序中有一个单击添加电子邮件链接,多次单击同一链接,警报消息到达您已达到最大限制。

How should I implement using protractor with typescript. 我应该如何使用带有打字稿的量角器来实现。 I used for loop, but it didn't work 我用于循环,但它没有用

for (let i:number=1; i<5; i++){   
    if (EmailLinkpageElement.isDisplayed())
    {   
        // click on the link
    }
    else
    {
        // verify the text message
    }
}

Please suggest what to do as I am trying but couldn't reach to any solution. 请建议我正在尝试做什么,但无法达成任何解决方案。

Generally, you want to click an element for unknown amount times until alert is present. 通常,您希望单击未知量的元素,直到出现警报。 So, maybe you can go with this approach: 所以,也许你可以采用这种方法:

This function takes an ElementFinder object which is clicked repeatively until either a given timeout is reached OR the alert box is present. 此函数接受一个ElementFinder对象,该对象被重复单击,直到达到给定的timeout或存在警告框。

const clickElementUntilAlertPresent = async (el: ElementFinder, timeout: number = 10 * 1000): Promise<boolean>=> {
    const clickDelay = 100
    let done = false

    // start interval for clicking element repeatively
    const interval = setInterval(() => {

        // IF the function is done, clear interval,
        if(done) {
            console.log("stop clicking element now", el.locator.toString())
            clearInterval(interval)
            return
        }

        console.log("clicking element", el.locator.toString())
        el.click()
    }, clickDelay)


    // wait until alert box is present. After timeout, returns false. 
    const result = await browser.wait<boolean>(browser.ExpectedConditions.alertIsPresent(), timeout)

    // Result is now true if alert is present, or false, if timeout reached.
    // Important here, to set done to true. We want to stop clicking interval!
    done = true

    return result
}

Note 注意

You should learn about Promise class. 你应该学习Promise课程。 For example, 例如,

if (EmailLinkpageElement.isDisplayed()) { }

Will always be true, since this returns a Promise object, and an object is evaluated as truethy in javascript. 永远都是真的,因为这会返回一个Promise对象,并且一个对象在javascript中被评估为truethy

If you are not familiar with Promise class, then your first priority now should be to learn them! 如果您不熟悉Promise课程,那么您现在的首要任务应该是学习它们! You need them for async tasks and in testing it's a lot.. 你需要它们用于async任务并且在测试它时很多..

Here's one ressource: Promise 这是一个资源: 承诺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 serenity-js 断言 web 元素在屏幕上可见? - How to assert that web element is visible on the screen using serenity-js? WebdriverIO 6:如何单击仅在悬停后可见的元素 - WebdriverIO 6: how to click on an element that is visible only after hovering 我如何在Ionic 3 Angular应用程序中禁用按钮单击,直到取消祝酒消息? - How can I disable the button click in my Ionic 3 Angular app until the toast message gets dismissed? 如何在角度6中打印数组元素的值作为警报 - how to print value of array element as alert in angular 6 如果 MatSelctionList,如何获取屏幕上可见的项目数? - How to get how many items are visible on screen if MatSelctionList? 注销用户并显示警报消息 - Logout user with alert message TypeScript显示警报消息 - TypeScript display alert message 如果超出可见屏幕,如何找到我的下拉 div? - How to find my dropdown div if exceeds out of visible screen? 如何使用 Jasmine 单击按钮元素? - How to click button element with Jasmine? 无法单击表单内的按钮(它说带有定位器的元素不可见) - Unable to click on a Button which is inside a Form(Its says element with locator is not visible)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM