Skip to content

Commit

Permalink
Add Tab.get_script_source and enable / disable debugger methods (rust…
Browse files Browse the repository at this point in the history
  • Loading branch information
atroche committed Mar 21, 2019
1 parent 603bad7 commit 625c59f
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased](https://github.com/atroche/rust-headless-chrome/compare/v0.1.4...HEAD)
### Added

* Tab.get_script_source, Tab.enable_debugger, Tab.disable_debugger

### Removed
### Changed

Expand Down
21 changes: 21 additions & 0 deletions src/browser/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,27 @@ impl<'a> Tab {
})?;
Ok(())
}

/// Enables Debugger
pub fn enable_debugger(&self) -> Result<(), Error> {
self.call_method(protocol::debugger::methods::Enable {})?;
Ok(())
}

/// Disables Debugger
pub fn disable_debugger(&self) -> Result<(), Error> {
self.call_method(protocol::debugger::methods::Disable {})?;
Ok(())
}

/// Returns source for the script with given id.
///
/// Debugger must be enabled.
pub fn get_script_source(&self, script_id: &str) -> Result<String, Error> {
Ok(self
.call_method(protocol::debugger::methods::GetScriptSource { script_id })?
.script_source)
}
}

impl Drop for Tab {
Expand Down
41 changes: 41 additions & 0 deletions src/protocol/debugger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub mod methods {
use crate::protocol::Method;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct GetScriptSource<'a> {
pub script_id: &'a str,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetScriptSourceReturnObject {
pub script_source: String,
}
impl<'a> Method for GetScriptSource<'a> {
const NAME: &'static str = "Debugger.getScriptSource";
type ReturnObject = GetScriptSourceReturnObject;
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Enable {}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EnableReturnObject {}
impl Method for Enable {
const NAME: &'static str = "Debugger.enable";
type ReturnObject = EnableReturnObject;
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Disable {}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DisableReturnObject {}
impl Method for Disable {
const NAME: &'static str = "Debugger.disable";
type ReturnObject = DisableReturnObject;
}
}
1 change: 1 addition & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde_json::Value;
use std::fmt::Debug;

pub mod browser;
pub mod debugger;
pub mod dom;
pub mod input;
pub mod network;
Expand Down
54 changes: 53 additions & 1 deletion tests/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use std::sync::{atomic, Arc};
use std::thread::JoinHandle;
use std::time::Duration;
use std::{fs, io};

pub struct Server {
server: Arc<tiny_http::Server>,
Expand Down Expand Up @@ -52,6 +52,11 @@ impl Server {
Self::new(responder)
}

#[allow(dead_code)]
pub fn url(&self) -> String {
format!("http://127.0.0.1:{}", self.port())
}

pub fn port(&self) -> u16 {
self.server.server_addr().port()
}
Expand All @@ -70,3 +75,50 @@ impl Drop for Server {
self.exit().unwrap()
}
}

fn basic_http_response<'a>(
body: &'a str,
content_type: &'static str,
) -> tiny_http::Response<&'a [u8]> {
tiny_http::Response::new(
200.into(),
vec![tiny_http::Header::from_bytes(&b"Content-Type"[..], content_type.as_bytes()).unwrap()],
body.as_bytes(),
Some(body.len()),
None,
)
}

#[allow(dead_code)]
fn not_found_response() -> tiny_http::Response<io::Empty> {
tiny_http::Response::new_empty(404.into())
}

#[allow(dead_code)]
pub fn file_server(path: &'static str) -> Server {
Server::new(move |request: tiny_http::Request| {
let url = if request.url() == "/" {
"/index.html"
} else {
request.url()
};

let file_path = format!("{}{}", path, url);

if let Ok(file_contents) = fs::read_to_string(file_path) {
let content_type = if url.ends_with(".js") {
"application/javascript"
} else if url.ends_with(".css") {
"text/css"
} else {
"text/html"
};

let response = basic_http_response(&file_contents, content_type);
request.respond(response)
} else {
let response = not_found_response();
request.respond(response)
}
})
}
48 changes: 48 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use headless_chrome::{
browser::default_executable, browser::tab::Tab, protocol::page::ScreenshotFormat, Browser,
LaunchOptionsBuilder,
};
use std::thread::sleep;
use std::time::Duration;

mod logging;
mod server;
Expand Down Expand Up @@ -358,3 +360,49 @@ fn incognito_contexts() -> Result<(), failure::Error> {
);
Ok(())
}

#[test]
fn get_script_source() -> Result<(), failure::Error> {
logging::enable_logging();
let server = server::file_server("tests/coverage_fixtures");
let browser = Browser::new(
LaunchOptionsBuilder::default()
.path(Some(default_executable().unwrap()))
.headless(false)
.build()
.unwrap(),
)
.unwrap();

let tab: Arc<Tab> = browser.wait_for_initial_tab()?;

tab.enable_profiler()?;
tab.start_js_coverage()?;

tab.navigate_to(&format!(
"{}/basic_page_with_js_scripts.html",
&server.url()
))?;

tab.wait_until_navigated()?;

sleep(Duration::from_millis(100));

let script_coverages = tab.take_precise_js_coverage()?;

tab.enable_debugger()?;

let contents = tab.get_script_source(&script_coverages[0].script_id)?;
assert_eq!(
include_str!("coverage_fixtures/coverage_fixture1.js"),
contents
);

let contents = tab.get_script_source(&script_coverages[1].script_id)?;
assert_eq!(
include_str!("coverage_fixtures/coverage_fixture2.js"),
contents
);

Ok(())
}

0 comments on commit 625c59f

Please sign in to comment.