Lithium-WebDriver

 view release on metacpan or  search on metacpan

lib/Lithium/WebDriver.pm  view on Meta::CPAN

		$self->_session_id($res);
		return $res->{value};
	} else {
		$self->_parse_error($res->content);
		return undef;
	}
}

sub _post
{
	my ($self, $base, $path, $obj) = @_;
	$obj ||= {};
	my $uri = $self->_get_uri($base, $path);
	if (!%$obj){
		debug "Posting to: $uri, no payload";
	} else {
		debug "Posting to: $uri\npayload is:";
		dump $obj;
	}
	$self->_window_id_list
		if $base ne 'host' && $self->{window_tracking} ne 'noop';
	my $res = $self->{LWP}->post($uri, Content => encode_json($obj));
	$self->{last_res} = $res;
	if ($res->code == 204) {
		return 1;
	} elsif ($res->is_success){
		$res = decode_json $res->content;
		if ($res->{status} != 0) {
			return _error_status($res->{status});
		}
		$self->_session_id($res);
		return $res->{value};
	} else {
		$self->_parse_error($res->content);
		return undef;
	}
}

########################### Window Functions ##################################

sub _window_id_list
{
	my ($self) = @_;
	# Pull a list of opaque windows each post, maintaining the order
	# in which they were opened
	my $new_windows    = $self->window_handles;
	if ($new_windows) {
		for my $new_window (@{$new_windows}) {
			my $match = 0;
			for my $old_window (@{$self->{window_list}}) {
				$match = 1 if $old_window eq $new_window;
				last if $match;
			}
			next if $match;
			push @{$self->{window_list}}, $new_window;
		}
	}
	return $self->{window_list};
}

sub update_windows
{
	my ($self) = @_;
	$self->_window_id_list();
}

sub _wait_for_page
{
	my ($self, %params) = @_;
	debug "Waiting for page to load"
		.", checking readystate and url";
	$params{timeout} ||= 3;
	debug "Page load timeout set to: $params{timeout} (ms)";
	my $url   = $self->url;
	debug "Current url: $url";
	my $readiness = $self->run(js => "return document.readyState;");
	debug "Document state: $readiness";
	my $title = $self->title;
	debug "Assumed new page, no longer in iframe.";
	$self->{in_frame} = undef;
	local $SIG{ALRM} = sub { die 1; };
	alarm $params{timeout};
	eval {
		while (!$readiness || $readiness !~ m/complete|interactive/ || $url =~ m/about\:blank/ ) {
			$readiness = $self->run(js => "return document.readyState;") || '';
			debug "Document ready state: $readiness";
			$url = $self->url || '';
			debug "Url: $url";
			$title = $self->title || '';
			debug "title is: $title";
			sleep 0.1;
		}
		alarm 0;
		1;
	} or do {
		alarm 0;
		debug "Timed out waiting for current window to load";
		return 0;
	};
	return 1;
}

sub _window_looper
{
	my ($self) = @_;
	my $window_list_new = $self->windows;
	$self->{current_window} = $self->window;
	for my $new_window (@$window_list_new) {
		my $match = 0;
		if (!$self->{window_titles}{$new_window}) {
			debug "Missing window title for ID: $new_window";
			my $title;
			if ($new_window eq $self->{current_window}) {
				$title = $self->title ;
			} else {
				debug "Switching to new window and getting title";
				my $original_window = $self->{current_window};
				$self->_window_id_list
					if $self->{window_tracking} eq 'noop';
				$self->_post(path => "/window", { name => $new_window});
				$title = $self->title;



( run in 0.785 second using v1.01-cache-2.11-cpan-39bf76dae61 )