Skip to content

Commit 11aedcf

Browse files
committed
Run tests with chrome driver
The one test that is not executed with the same assertions is the javascript throw, because the error information returned by chrome driver is too different from gecko. Did a quick fix to the ignored youtube tests to avoid the anchor elements without an href attribute.
1 parent ba5dbbf commit 11aedcf

File tree

3 files changed

+113
-27
lines changed

3 files changed

+113
-27
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ rust:
33
- stable
44
- beta
55
- nightly
6+
# chrome sandbox fails to run without this
7+
sudo: required
68
addons:
79
# Force the latest firefox, otherwise we get and old ESR version
810
# that we dont even support
911
firefox: latest
12+
chrome: stable
1013
apt:
1114
packages:
1215
- firefox
16+
- google-chrome-stable
1317
- xvfb
1418
before_script:
1519
# start xvfb and firefox to run the tests
@@ -21,5 +25,10 @@ before_script:
2125
- bin/download_geckodriver
2226
- export PATH=$PATH:$HOME/.cargo/bin:$PWD/bin
2327
- which geckodriver
28+
# install chromedriver
29+
- bin/download_chromedriver
30+
- export CHROME_BIN=/usr/bin/google-chrome-stable
31+
- export CHROME_HEADLESS=1
32+
- which chromedriver
2433
script:
2534
- RUST_LOG="webdriver=trace" cargo test --verbose

bin/download_chromedriver

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
set -ex
3+
4+
script_dir=$( cd $(dirname ${BASH_SOURCE[0]}); pwd )
5+
6+
version=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
7+
8+
curl -sSL \
9+
http://chromedriver.storage.googleapis.com/$version/chromedriver_linux64.zip \
10+
-o ${script_dir}/chromedriver-linux64.zip
11+
unzip -d ${script_dir} ${script_dir}/chromedriver-linux64.zip
12+
chmod +x ${script_dir}/chromedriver

tests/webdriver_client_integration.rs

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,32 @@ use std::env;
1111
use std::sync::{Once, ONCE_INIT};
1212
use std::thread::sleep;
1313
use std::time::Duration;
14-
use webdriver_client::{Driver, HttpDriverBuilder};
14+
use webdriver_client::{Driver, HttpDriverBuilder, DriverSession};
1515
use webdriver_client::firefox::GeckoDriver;
16+
use webdriver_client::chrome::ChromeDriver;
1617
use webdriver_client::messages::ExecuteCmd;
1718

1819
#[test]
19-
fn test_file() {
20+
fn test_file_geckodriver() {
2021
ensure_logging_init();
21-
2222
// TODO: Perhaps calculate path from PATH environment variable.
2323
let gecko = GeckoDriver::build()
2424
.firefox_binary("/usr/bin/firefox")
2525
.spawn().unwrap();
2626
let sess = gecko.session().unwrap();
27+
test_file(sess);
28+
}
29+
30+
#[test]
31+
fn test_file_chromedriver() {
32+
ensure_logging_init();
33+
let chrome = ChromeDriver::build()
34+
.spawn().unwrap();
35+
let sess = chrome.session().unwrap();
36+
test_file(sess);
37+
}
2738

39+
fn test_file(sess: DriverSession) {
2840
let test_url = get_integration_test_url();
2941
sess.go(&test_url).unwrap();
3042
let url = sess.get_current_url().unwrap();
@@ -55,22 +67,6 @@ fn test_file() {
5567
assert_eq!(exec_int, 4);
5668
}
5769

58-
{
59-
// Test execute handling an exception
60-
let exec_res = sess.execute(ExecuteCmd {
61-
script: "throw 'SomeException';".to_owned(),
62-
args: vec![],
63-
});
64-
assert!(exec_res.is_err());
65-
let err = exec_res.err().unwrap();
66-
let err = match err {
67-
webdriver_client::Error::WebDriverError(e) => e,
68-
_ => panic!("Unexpected error variant: {:#?}", err),
69-
};
70-
assert_eq!(err.error, "javascript error");
71-
assert_eq!(err.message, "SomeException");
72-
}
73-
7470
{
7571
// Test execute async
7672
let exec_json = sess.execute_async(ExecuteCmd {
@@ -86,14 +82,68 @@ fn test_file() {
8682
}
8783

8884
#[test]
89-
fn test_http_driver() {
85+
fn test_exception_handling_geckodriver() {
86+
ensure_logging_init();
87+
// TODO: Perhaps calculate path from PATH environment variable.
88+
let gecko = GeckoDriver::build()
89+
.firefox_binary("/usr/bin/firefox")
90+
.spawn().unwrap();
91+
let sess = gecko.session().unwrap();
92+
93+
let exec_res = sess.execute(ExecuteCmd {
94+
script: "throw 'SomeException';".to_owned(),
95+
args: vec![],
96+
});
97+
assert!(exec_res.is_err());
98+
let err = exec_res.err().unwrap();
99+
let err = match err {
100+
webdriver_client::Error::WebDriverError(e) => e,
101+
_ => panic!("Unexpected error variant: {:#?}", err),
102+
};
103+
assert_eq!(err.error, "javascript error");
104+
assert_eq!(err.message, "SomeException");
105+
}
106+
107+
/// This test is kept separate from the gecko variant because the error messages are different
108+
/// in chrome and so are the other stacktrace information
109+
#[test]
110+
fn test_exception_handling_chromedriver() {
90111
ensure_logging_init();
112+
let chrome = ChromeDriver::build()
113+
.spawn().unwrap();
114+
let sess = chrome.session().unwrap();
115+
116+
let exec_res = sess.execute(ExecuteCmd {
117+
script: "throw 'SomeException';".to_owned(),
118+
args: vec![],
119+
});
120+
assert!(exec_res.is_err());
121+
let err = exec_res.err().unwrap();
122+
match err {
123+
webdriver_client::Error::WebDriverError(_) => (),
124+
_ => panic!("Unexpected error variant: {:#?}", err),
125+
}
126+
}
91127

128+
#[test]
129+
fn test_http_driver_geckodriver() {
130+
ensure_logging_init();
92131
// TODO: Perhaps calculate path from PATH environment variable.
93132
let gecko = GeckoDriver::build()
94133
.firefox_binary("/usr/bin/firefox")
95134
.spawn().unwrap();
135+
test_http_driver(gecko.url());
136+
}
137+
138+
#[test]
139+
fn test_http_driver_chromedriver() {
140+
ensure_logging_init();
141+
let chrome = ChromeDriver::build()
142+
.spawn().unwrap();
143+
test_http_driver(chrome.url());
144+
}
96145

146+
fn test_http_driver(url: &str) {
97147
// Hackily sleep a bit until geckodriver is ready, otherwise our session
98148
// will fail to connect.
99149
// If this is unreliable, we could try:
@@ -102,7 +152,7 @@ fn test_http_driver() {
102152
sleep(Duration::from_millis(1000));
103153

104154
let http_driver = HttpDriverBuilder::default()
105-
.url(gecko.url())
155+
.url(url)
106156
.build().unwrap();
107157
let sess = http_driver.session().unwrap();
108158

@@ -137,21 +187,36 @@ fn get_integration_test_url() -> String {
137187
format!("file://{crate}/tests/integration_test.html", crate = crate_root)
138188
}
139189

190+
/// This depends on an external page not under our control, we
191+
/// should migrate to using local files.
140192
mod youtube_integration_test {
141-
use webdriver_client::Driver;
193+
use webdriver_client::{Driver, DriverSession};
142194
use webdriver_client::firefox::GeckoDriver;
195+
use webdriver_client::chrome::ChromeDriver;
143196
use webdriver_client::messages::LocationStrategy;
144197

145-
/// This depends on an external page not under our control, we
146-
/// should migrate to using local files.
147198
#[test]
148199
#[ignore]
149-
fn test() {
200+
fn test_geckodriver() {
150201
let gecko = GeckoDriver::build()
151202
.kill_on_drop(true)
152203
.spawn()
153204
.unwrap();
154-
let mut sess = gecko.session().unwrap();
205+
let sess = gecko.session().unwrap();
206+
test(sess);
207+
}
208+
209+
#[test]
210+
#[ignore]
211+
fn test_chromedriver() {
212+
let chrome = ChromeDriver::build()
213+
.spawn()
214+
.unwrap();
215+
let sess = chrome.session().unwrap();
216+
test(sess);
217+
}
218+
219+
fn test(mut sess: DriverSession) {
155220
sess.go("https://www.youtube.com/watch?v=dQw4w9WgXcQ").unwrap();
156221
sess.get_current_url().unwrap();
157222
sess.back().unwrap();
@@ -160,7 +225,7 @@ mod youtube_integration_test {
160225
sess.get_page_source().unwrap();
161226

162227
{
163-
let el = sess.find_element("a", LocationStrategy::Css).unwrap();
228+
let el = sess.find_element("a[href]", LocationStrategy::Css).unwrap();
164229
el.attribute("href").unwrap();
165230
el.css_value("color").unwrap();
166231
el.text().unwrap();

0 commit comments

Comments
 (0)