Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/rust/tests/common.rs
2885 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
use assert_cmd::assert::AssertResult;
19
use assert_cmd::Command;
20
use is_executable::is_executable;
21
use selenium_manager::files::path_to_string;
22
use selenium_manager::logger::JsonOutput;
23
use selenium_manager::shell;
24
use selenium_manager::shell::run_shell_command_by_os;
25
use std::borrow::BorrowMut;
26
use std::env::consts::OS;
27
use std::path::{Path, PathBuf};
28
29
#[allow(dead_code)]
30
pub fn get_selenium_manager() -> Command {
31
let path = PathBuf::from(env!("CARGO_BIN_EXE_selenium-manager"));
32
33
if path.exists() {
34
return Command::new(path);
35
}
36
37
if cfg!(windows) {
38
let exe_path = path.with_extension("exe");
39
if exe_path.exists() {
40
return Command::new(exe_path);
41
}
42
}
43
44
panic!("Binary not found {}", path_to_string(&path));
45
}
46
47
#[allow(dead_code)]
48
pub fn assert_driver(cmd: &mut Command) {
49
let stdout = get_stdout(cmd);
50
51
let json: JsonOutput = serde_json::from_str(&stdout).unwrap();
52
let driver_path = Path::new(&json.result.driver_path);
53
assert!(driver_path.exists());
54
assert!(is_executable(driver_path));
55
}
56
57
#[allow(dead_code)]
58
pub fn assert_browser(cmd: &mut Command) {
59
let stdout = &cmd.unwrap().stdout;
60
let output = std::str::from_utf8(stdout).unwrap();
61
let json: JsonOutput = serde_json::from_str(output).unwrap();
62
let browser_path = Path::new(&json.result.browser_path);
63
assert!(browser_path.exists());
64
assert!(is_executable(browser_path));
65
}
66
67
#[allow(dead_code)]
68
pub fn get_driver_path(cmd: &mut Command) -> String {
69
let stdout = &cmd.unwrap().stdout;
70
let output = std::str::from_utf8(stdout).unwrap();
71
let json: JsonOutput = serde_json::from_str(output).unwrap();
72
path_to_string(Path::new(&json.result.driver_path))
73
}
74
75
#[allow(dead_code)]
76
pub fn exec_driver(cmd: &mut Command) -> String {
77
let cmd_mut = cmd.borrow_mut();
78
let driver_path = get_driver_path(cmd_mut);
79
let driver_version_command = shell::Command::new_single(format!("{} --version", &driver_path));
80
let output = run_shell_command_by_os(OS, driver_version_command).unwrap();
81
println!("**** EXEC DRIVER: {}", output);
82
output
83
}
84
85
#[allow(dead_code)]
86
pub fn get_stdout(cmd: &mut Command) -> String {
87
let stdout = &cmd.unwrap().stdout;
88
let output = std::str::from_utf8(stdout).unwrap();
89
println!("{}", output);
90
output.to_string()
91
}
92
93
#[allow(dead_code)]
94
pub fn get_stderr(cmd: &mut Command) -> String {
95
let stderr = &cmd.unwrap().stderr;
96
let err_output = std::str::from_utf8(stderr).unwrap();
97
println!("stderr: {}", err_output);
98
err_output.to_string()
99
}
100
101
#[allow(dead_code)]
102
pub fn assert_output(
103
cmd: &mut Command,
104
assert_result: AssertResult,
105
expected_output: Vec<&str>,
106
error_code: i32,
107
) {
108
if assert_result.is_ok() {
109
let stdout = &cmd.unwrap().stdout;
110
let output = std::str::from_utf8(stdout).unwrap();
111
expected_output
112
.iter()
113
.for_each(|o| assert!(output.contains(o)));
114
} else {
115
assert!(assert_result
116
.err()
117
.unwrap()
118
.to_string()
119
.contains(&error_code.to_string()));
120
}
121
}
122
123