使用iFrame打开新窗口后,测试用例运行速度非常慢

甚至更新的信息:我现在对于为什么这个测试很慢这一点感到困惑。 我已经消除了对javascript的需求,如下所述,我添加了报告以确定延迟的位置。 这些是以“logger”开头的行。 我添加了这些行后面的时间戳,以便您可以看到延迟的位置。 这是代码的一部分,它看起来像混乱/需要很长时间(每次10分钟):

wait.until(ExpectedConditions.elementToBeClickable(By.id("ID"))); logger.log(LogStatus.INFO, "Waituntill is done and element is clickable"); /** logged as successfull at 17:45:51 */ driver.findElement(By.xpath("//div[@class='CLASS']")).click(); logger.log(LogStatus.INFO, "menu visible"); /** logged as successfull at 17:45:52 */ driver.findElement(By.xpath("//*[@class='CLASS' and text()='TEXT']")).click(); logger.log(LogStatus.INFO, "menu item is available and clickable."); /** logged as successfull at 17:55:47 */ 

奇怪的是,如果你按照测试过程,最后一步(点击)会立即显示,因为这会打开一个新窗口。 这不是10分钟后。 似乎在这一步之后内置了10个延迟。但是在我的代码中没有这样的东西。 上面的代码后跟:

 Set  handles =driver.getWindowHandles(); Iterator it = handles.iterator(); //iterate through your windows while (it.hasNext()){ String parent = it.next(); String newwin = it.next(); driver.switchTo().window(newwin); driver.switchTo().defaultContent(); logger.log(LogStatus.INFO, "timestamp1 defaultcontent"); /** logged as successfull at 17:55:47 */ WebElement frame=driver.findElement(By.xpath("/html/body/iframe")); logger.log(LogStatus.INFO, "timestamp2 find xpath for iframe"); /** logged as successfull at 17:55:47 */ driver.switchTo().frame(frame); logger.log(LogStatus.INFO, "timestamp3 switch to iframe"); /** logged as successfull at 17:55:47 */ assertEquals("PageTitle", driver.getTitle()); logger.log(LogStatus.INFO, "timestamp4 confirm switch to iframe succesfull"); /** logged as successfull at 17:55:47 */ 

新信息:我研究了我的代码,看起来我对延迟发生的地方不正确。 现在看起来它发生在我改变元素的样式以便它显示而另一个元素变得可点击的地方。 我现在使用以下代码来更改样式,但这可能是问题所在。 是否有更简单或更好的方法来改变元素的风格?

 JavascriptExecutor js = (JavascriptExecutor) driver; WebElement element = driver.findElement(By.xpath("//div[@class='button_ribbon_dropdownsection']")); js.executeScript("arguments[0].setAttribute('style', 'left: 1520px; top: 40px; display: block;')",element); 

ORIGINAL POST:我有一个测试用例,点击一个打开弹出窗口的链接。 在此弹出窗口中是一个iFrame,其中包含我要断言和填充的字段。 但是,运行此案例大约需要10分钟,我不知道为什么。 这是我的代码:

 //this is the link I click that opens the new window driver.findElement(By.xpath("//*[@class='value' and text()='text']")).click(); //I have inserted this section to switch to the correct window (the popup). If I don't inlcude this the focus remains on the window where I clicked the link that opens the pop-up. Set  handles =driver.getWindowHandles(); Iterator it = handles.iterator(); //iterate through your windows while (it.hasNext()){ String parent = it.next(); String newwin = it.next(); driver.switchTo().window(newwin); //this is where I switch to the iFrame in the new window in order to reach the desired elements WebElement testframe=driver.findElement(By.xpath("/html/body/iframe")); driver.switchTo().frame(testframe); /** this section may take up to 10-15 minutes */ assertEquals("pagetitle here", driver.getTitle()); assertEquals("value", driver.findElement(By.id("id")).getText()); driver.findElement(By.id("id")).clear(); driver.findElement(By.id("id")).sendKeys("number"); 

这段代码确实运行,因此代码中没有错误(我可以找到),但它只需要很长时间,我无法弄清楚原因。

我知道通过Xpath访问iFrame并不是最好的方法,但它是商业产品,iframe没有id或名称,所以我无法以这种方式导航到它。

我正在使用Eclipse与Selenium webdriver和Java。

对不起,没有答案,而是建议(我发布此评论,但stackoverflow不会让我):

通常我会在事情缓慢的时候检查3件事:

  • Selenium版本:它们会定期出现一些缓慢的错误,也许你很幸运有这个问题的版本;
  • Selenium版本与浏览器类型和版本。 可能对您使用的selenium版本的浏览器版本支持不是最佳的。
  • 尝试使用缩减/禁用的javascript运行浏览器,如果可能的话。 可能是因为一些javascript不能很好地发挥作用,使用selenium。

如果不是上述情况,我也会对你怀疑的每个声明进行计时,看看哪个声明实际上很慢或者它们都是同样慢的(例如,有时候selenium的sendKeys在IE上有时会出现很长的突发错误)。 并在此基础上启用调试日志,如此处所述。 希望能够为您提供有关正在发生的事情的更多信息。

想到的一件事是你的第二个xpath:

 //*[@class='CLASS' and text()='TEXT'] 

“// *”部分可能非常慢。 您可以尝试指定元素名称或路径,例如

 //div[@class='CLASS' and text()='TEXT'] 

要么

 /select/sub/tree/to/search//*[@class='CLASS' and text()='TEXT'] 

限制搜索?

感谢Kiril的建议。 我将此作为答案发布,因为评论字段对于此回复并不是很大。

我正在努力实现你的建议,但我刚才尝试的两件事似乎都没有用。 我使用完整的xpath找到了项目:

 /html/body/form/div[4]/div[2]/span[6]/div[2]/div[2]/table/tbody/tr[3]/td[2] 

正如我认为在这个特定的表中查找对我尝试的搜索的限制是足够的:

 driver.findElement(By.xpath("/html/body/form/div[4]/div[2]/span[6]/div[2]/div[2]/table/tbody//*[@class='value' and text()='text']")).click(); 

但是,这会导致NoSuchElementExcepttion

然后,我尝试根据您的第一个建议更具体,并尝试:

 driver.findElement(By.xpath("//td[@class='value' and text()='text']")).click(); 

这确实有效,但没有为测试的速度做任何事情,它仍然需要10分钟才能继续。