简体   繁体   English

等待量角器加载在新选项卡中打开的数据

[英]Wait for protractor to load data opened in new tab

I have a scenario where when I click on a element in website A it opens in Website B in a new tab.我有一个场景,当我单击网站 A 中的元素时,它会在网站 B 中的新选项卡中打开。 I am able to navigate to the new tab that is Website B but have to use browser.sleep() which my client wants me to avoid using.我能够导航到网站 B 的新选项卡,但必须使用我的客户希望我避免使用的 browser.sleep()。 I tried using promises like after element(Website A).click().then(function(){getallwindowshandle, then move to the window of my choice});我尝试使用像 after element(Website A).click().then(function(){getallwindowshandle, then move to the window of my choice}); 这样的承诺; but here too I had to use browser.sleep()但在这里我也不得不使用 browser.sleep()

Can someone please help me how can I overcome browser.sleep() when navigating to new window with protractor.有人可以帮助我在使用量角器导航到新窗口时如何克服 browser.sleep() 。

Best practice is using protractor.ExpectedConditions for wait until a target element is visible or enabled or so on.最佳实践是使用protractor.ExpectedConditions等待目标元素可见或启用等。 Using protractor's expected conditions you can make protractor to wait until next tab is ready to proceed.使用量角器的预期条件,您可以让量角器等待下一个选项卡准备好继续。 You can follow below code:您可以按照以下代码:

Code代码

   EC=protractor.ExpectedConditions;

   targetEleOnNewTab=element(locator)//find out an element on new tab
   //keep this line of code after switching to new tab before performing any 
   //operation on new tab
   browser.wait(EC.visibilityOf(targetEleOnNewTab),15000,'Ele Not Visible');

Although this question is a little bit older I'd like mention how I solved this problem:虽然这个问题有点老了,但我想提一下我是如何解决这个问题的:

var handles;
const targetNumberOfTabs = 2; // Or whatever
try {
     await browser.wait(
       async function() {
          handles = await browser.getAllWindowHandles();
          if(handles.length >= targetNumberOfTabs) {
                  return true;
           }
           else {
                  return false;
           }
        }
       ,30000, 'The number of tabs should be equal or greater ' + targetNumberOfTabs);
}
catch(err) {
       console.log(err);
       // Whatever you might want to do on error
}

Basically this code makes the test flow wait till the number of expected tabs has been reached for a maximum of 30 seconds.基本上这段代码使测试流程等待,直到达到预期的选项卡数量最多 30 秒。

Requirements: You need to know how many tabs you expect to be there and store them in targetNumberOfTabs .要求:您需要知道您希望有多少个选项卡并将它们存储在targetNumberOfTabs And your environment needs to support async / await .并且您的环境需要支持async / await

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM