[英]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
}
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
任务并且在测试它时很多..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.