Module: Async::WebDriver::Scope::Navigation

Included in:
Async::WebDriver::Session
Defined in:
lib/async/webdriver/scope/navigation.rb

Overview

Helpers for navigating the browser.

⚠️ Important: Navigation operations (and events that trigger navigation) may result in race conditions if not properly synchronized. Consult the “Navigation Timing” Guide in the documentation for more details.

Instance Method Summary collapse

Instance Method Details

#current_pathObject

Get the path component of the current URL.



32
33
34
# File 'lib/async/webdriver/scope/navigation.rb', line 32

def current_path
	URI.parse(current_url).path
end

#current_urlObject

Get the current URL.



26
27
28
# File 'lib/async/webdriver/scope/navigation.rb', line 26

def current_url
	session.get("url")
end

Navigate back in the browser history.



37
38
39
# File 'lib/async/webdriver/scope/navigation.rb', line 37

def navigate_back
	session.post("back")
end

Navigate forward in the browser history.



42
43
44
# File 'lib/async/webdriver/scope/navigation.rb', line 42

def navigate_forward
	session.post("forward")
end

Navigate to the given URL.



18
19
20
# File 'lib/async/webdriver/scope/navigation.rb', line 18

def navigate_to(url)
	session.post("url", {url: url})
end

#refreshObject

Refresh the current page.



47
48
49
# File 'lib/async/webdriver/scope/navigation.rb', line 47

def refresh
	session.post("refresh")
end

#wait_for_navigation(timeout: 10.0, &block) ⇒ Object

Wait for navigation to complete with custom conditions.

This method helps avoid race conditions by polling the browser state until your specified conditions are met.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/async/webdriver/scope/navigation.rb', line 58

def wait_for_navigation(timeout: 10.0, &block)
	clock = Clock.start
	duration = [timeout / 100.0, 0.005].max
	
	while true
		current_url = session.current_url
		ready_state = session.execute("return document.readyState;")
		
		if block.arity > 1
			break if yield(current_url, ready_state)
		else
			break if ready_state == "complete" && yield(current_url)
		end
		
		if clock.total > timeout
			raise TimeoutError, "Timed out waiting for navigation to complete (current_url: #{current_url}, ready_state: #{ready_state})"
		end
		
		Console.debug(self, "Waiting for navigation...", ready_state: ready_state, location: current_url, elapsed: clock.total)
		sleep(duration)
	end
end