最好的方式来在硒时间

最好的方式来在硒时间

问题描述:

我正在写一些Selenium测试在Java中,我主要是试图使用验证,而不是断言,因为我检查的东西不是非常依赖,所以我不想如果一件小事不起作用就放弃。我想要关注的一件事是某些Drupal页面是否需要永久加载。什么是最好的方式来做到这一点?最好的方式来在硒时间

我正在使用的模式的小例子。

selenium.open("/m"); 
selenium.click("link=Android"); 
selenium.waitForPageToLoad("100000"); 
if (selenium.isTextPresent("Epocrates")) { 
     System.out.println("  Epocrates confirmed"); 
} else { 
     System.out.println("Epocrates failed"); 
} 

我应该有两个“waitForPagetoLoad”的语句(比如,10000和100000),如果第一个后所需的文本显示不出来,打印一份声明?这似乎很笨拙。我想要做的只是一条线,就像

if (timeToLoad>10000) System.out.println("Epocrates was slow"); 

然后继续检查文本是否存在。

waitForPageToLoad将等到下一页加载。所以,你可以做一个开始/结束计时,做你的,如果:

long start = System.currentTimeMillis(); 
selenium.waitForPageToLoad("100000"); 
long timeToLoad= (System.currentTimeMillis()-start); 
if (timeToLoad>10000) System.out.println("Epocrates was slow"); 
+0

感谢。我想知道是否有更多的Selenium本地方式来做到这一点,但我可以将它捆绑到我自己的函数中,并得到相同的结果。 – eom 2010-08-25 19:19:36

在加载页面后加载文本吗?我的意思是,动态插入文本?否则,文本应该在页面加载后立即出现。

selenium.isTextPresent 

不等。它只检查当前可用的页面。

等待中硒东西的最好方法如下:

 Reporter.log("Waiting for element '" + locator + "' to appear."); 
    new Wait() 
    { 
     public boolean until() 
     { 
      return selenium.isElementPresent(locator); 
     } 
    }.wait("The element '" + locator + "' did not appear within " 
      + timeout + " ms.", timeout); 

服务员是硒的一部分,您只必须导入它。

此处还有一个您可以使用的框架。它是开源的,主要处理所有事情,并且可以轻松扩展。

https://sourceforge.net/projects/webauthfw/

使用得很好,给美国信贷嘿嘿。 :)

干杯, Gergely。

+0

如果你想出一种方法来围绕使用OOP的测试方法进行等待,这将会很有用。你上面写的方式对我来说看起来不是很有用。只要您调用的方法是异常安全的,那么使用包装器方法(使用System.currentTimeMillis())来调用定时方法会更好。 – djangofan 2015-01-12 15:52:12

+0

@djangofan我相信4年后,肯定会有东西包装它。 ;) – Hannibal 2015-01-13 16:38:37

Selenium integration test,我做到了,像这样,采用纳米时间和转换为双获得秒:

long endTime = System.nanoTime(); 
long duration = (endTime - startTime); 
Reporter.log("Duration was: " + ((double)duration/1000000000.0) + " seconds.", true); 
assertTrue(duration >=0 || duration <= 1000, "Test that duration of default implicit 
    timeout is less than 1 second, or nearly 0.");