Initial Headless Functions v2
This commit is contained in:
parent
615c5c9edd
commit
a41f765211
4 changed files with 90 additions and 36 deletions
|
|
@ -3,6 +3,7 @@ use futures::StreamExt;
|
|||
use chromiumoxide::browser::{Browser, BrowserConfig};
|
||||
use chromiumoxide::cdp::browser_protocol::page::NavigateParams;
|
||||
use chromiumoxide::cdp::browser_protocol::input::InsertTextParams;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
use crate::state::{SharedState, HeadlessPhase};
|
||||
use crate::config::read_config;
|
||||
|
|
@ -110,27 +111,33 @@ impl PageHandle {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn run_test(ip: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("[Headless] Starte Test für {ip}");
|
||||
pub async fn run_test(ip: &str, log: broadcast::Sender<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let _ = log.send(format!("[Headless] Starte Test für {ip}"));
|
||||
|
||||
// config lesen, um Pfad zu Chrome zu bekommen
|
||||
let exe_dir = std::env::current_exe()?.parent().unwrap().to_path_buf();
|
||||
let cfg = read_config(&exe_dir)?;
|
||||
let chrome_exe = Path::new(&cfg.chrome.path);
|
||||
let cfg = crate::config::read_config(&exe_dir)?;
|
||||
let chrome_exe = std::path::Path::new(&cfg.chrome.path);
|
||||
|
||||
let _ = log.send(format!("[Headless] Chrome: {}", chrome_exe.display()));
|
||||
let manager = HeadlessManager::new(chrome_exe, &cfg.chrome.args).await?;
|
||||
|
||||
let page = manager.new_page().await?;
|
||||
let url_https = format!("https://{ip}/");
|
||||
let url_http = format!("http://{ip}/");
|
||||
|
||||
// Zielseite öffnen
|
||||
let url = format!("https://{ip}/");
|
||||
page.goto(&url).await?;
|
||||
let _ = log.send(format!("[Headless] goto {url_https}"));
|
||||
if page.goto(&url_https).await.is_err() {
|
||||
let _ = log.send(format!("[Headless] HTTPS fehlgeschlagen, versuche HTTP"));
|
||||
let _ = log.send(format!("[Headless] goto {url_http}"));
|
||||
page.goto(&url_http).await?;
|
||||
}
|
||||
|
||||
// kurz warten & Text auslesen
|
||||
let _ = log.send("[Headless] warte auf <h1> …".to_string());
|
||||
let _ = page.wait_for_selector("h1", 5000).await?;
|
||||
if let Some(h1) = page.get_text("h1").await? {
|
||||
println!("[Headless] H1: {}", h1);
|
||||
} else {
|
||||
println!("[Headless] Keine H1 gefunden");
|
||||
|
||||
match page.get_text("h1").await? {
|
||||
Some(h1) => { let _ = log.send(format!("[Headless] H1: {h1}")); }
|
||||
None => { let _ = log.send("[Headless] Keine H1 gefunden".to_string()); }
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue