Home > database >  Selenium WebDriver: Actions.Pause() vs. Thread.Sleep()
Selenium WebDriver: Actions.Pause() vs. Thread.Sleep()

Time:09-16

While not ideal, there are instances where a hard wait is the only thing that works. In my quest to avoid Thread.Sleep(), I found Actions.Pause() which appears to have the same effect. Apart from chaining it with other actions, is there any other technical difference (or advantage)?

Thread.Sleep():

Thread.Sleep(1000);

Actions.Pause():

Actions.Pause(TimeSpan.FromSeconds(1)).Build().Perform();

EDIT:

Not looking into method chaining which the other thread does not answer. Interested on the actual difference of the 2 statements as is.

CodePudding user response:

The internal implementation of

actions.pause()

is

public Actions pause(long pause) {
    if (this.isBuildingActions()) {
        this.action.addAction(new PauseAction(pause));
    }

    return this.tick(new Pause(this.defaultMouse, Duration.ofMillis(pause)));
}

where the pause action is actually involved by new PauseAction(pause)
The internal implementation of it is refenced to:

public class PauseAction implements Action, IsInteraction {
    private final long pause;

    public PauseAction(long pause) {
        this.pause = pause;
    }

    public void perform() {
        try {
            Thread.sleep(this.pause);
        } catch (InterruptedException var2) {
        }

    }

    public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
        return Collections.singletonList(new Pause(keyboard, Duration.ofMillis(this.pause)));
    }
}

So, as you can see internally it utilizes...

Thread.sleep(this.pause);
  • Related