在 Selenium 中计时的最佳方式 [英] Best way to time something in Selenium

查看:38
本文介绍了在 Selenium 中计时的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写一些 Selenium 测试,并且我主要尝试使用验证而不是断言,因为我正在检查的东西不是很依赖,所以如果一件小事没有,我不想中止不行.我要关注的一件事是某些 Drupal 页面是否需要永远加载.最好的方法是什么?

I'm writing some Selenium tests in Java, and I'm mostly trying to use verifications instead of assertions because the things I'm checking aren't very dependent so I don't want to abort if one little thing doesn't work. One of the things I'd like to keep an eye on is whether certain Drupal pages are taking forever to load. What's the best way to do that?

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

Little example of the pattern I'm using.

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),并且如果在第一个之后没有显示所需的文本,则打印一个语句?这看起来很笨拙.我想做的只是像

Should I have two "waitForPagetoLoad" statements (say, 10000 and 100000) and if the desired text doesn't show up after the first one, print a statement? That seems clumsy. What I'd like to do is just a line like

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

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

And then keep going to check whether the text was present or not.

推荐答案

waitForPageToLoad 将等到下一页加载完毕.所以你可以做一个开始/结束计时器并做你的if:

waitForPageToLoad will wait until the next page is loaded. So you can just do a start/end timer and do your if:

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

这篇关于在 Selenium 中计时的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆