0

有没有人知道如何在我的硒代码中的这些操作之间等待,因为当我运行它时,我每次都会得到不同的结果,因为浏览器每次以不同的速度运行。有没有办法在每次 moveByOffSet 之间让它等待几秒钟?

 Actions builder2 = new Actions(driver);
    Action moveByOffset = builder2.moveByOffset(100, 200)
            .click()
            .moveByOffset(-150, 0)
            .click()
            .moveByOffset(0, -150)
            .click()
            .moveByOffset(150, 0)
            .click()
            .moveByOffset(0, 150)
            .contextClick()              
            .build();
    moveByOffset.perform();
4

1 回答 1

0

您可以从 Actions 类继承并覆盖 click() 方法,包括在其主体内睡眠。

public class MyActions extends Actions {

    public MyActions(WebDriver driver) {
        super(driver);
    }

    @Override
    public Actions click() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            //do something
        }
        return super.click();
    }
}

然后只需在测试中使用新类。

Actions actions = new MyActions(driver);

请记住,将睡眠调用放在测试代码中并不是一个好习惯。考虑使用 WebDriverWait 类提供的功能。

于 2013-06-10T14:38:34.830 回答