Unable to switch window in fantoccini - rust

I have opened a page https://www.jiocloud.com and trying to automate to login.
client.goto("https://www.jiocloud.com/").await?;
// jio click on login.
let ele = client
.wait()
.for_element(Locator::Css(r#"button.btn-login"#))
.await?;
ele.click().await?;
// jio-sign in with google.
let ele = client
.wait()
.for_element(Locator::Css(r#"button.social-btn.btn-google"#))
.await?;
ele.click().await?;
// this sleep was required otherwise windows() returns only one window.
std::thread::sleep(std::time::Duration::from_millis(1000));
let windows = client.windows().await?; // ex: returns vec[window1, window2]
let mut next = client.windows().await?.remove(1);
println!("switch to >> {:?}", next); // ex: switch to >> window2
client.switch_to_window(next);
// still shows me previous window as active.
println!("we are in {:?}", client.window().await?); // ex: window1

I was not waiting for an async call client.switch_to_window(next);

Related

Rust: How to move child (commandchild) to thread to be killed on window exit

I am trying to run a streamlit server behind the scenes of a Tauri application.
The streamlit server is packaged with PyInstaller into a single binary file and works as expected when standalone.
I have a rust main.rs file that needs to run a streamlit binary using a Command (in this case its a tauri::api::process::Command using a new_sidecar).
It spawns the server, but when the app closes, my streamlit server is not disposed off.
On window exit, I want to send a kill command to kill the server instance in the child.
Here is an example of my code:
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use async_std::task;
use std::sync::mpsc::sync_channel;
use std::thread;
use std::time::Duration;
use tauri::api::process::{Command, CommandEvent};
use tauri::{Manager, WindowEvent};
fn main() {
let (tx_kill, rx_kill) = sync_channel(1);
tauri::Builder::default()
.setup(|app| {
println!("App Setup Start");
let t = Command::new_sidecar("streamlit").expect("failed to create sidecar");
let (mut rx, child) = t.spawn().expect("Failed to spawn server");
let splashscreen_window = app.get_window("splashscreen").unwrap();
let main_window = app.get_window("main").unwrap();
// Listen for server port then refresh main window
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(output) = event {
if output.contains("Network URL:") {
let tokens: Vec<&str> = output.split(":").collect();
let port = tokens.last().unwrap();
println!("Connect to port {}", port);
main_window.eval(&format!(
"window.location.replace('http://localhost:{}')",
port
));
task::sleep(Duration::from_secs(2)).await;
splashscreen_window.close().unwrap();
main_window.show().unwrap();
}
}
}
});
// Listen for kill command
thread::spawn(move || loop {
let event = rx_kill.recv();
if event.unwrap() == -1 {
child.kill().expect("Failed to close API");
}
});
Ok(())
})
.on_window_event(move |event| match event.event() {
WindowEvent::Destroyed => {
println!("Window destroyed");
tx_kill.send(-1).expect("Failed to send close signal");
}
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running application");
}
But I am getting an error, when I try to kill the child instance:
thread::spawn(move || loop {
let event = rx_kill.recv();
if event.unwrap() == -1 {
child.kill().expect("Failed to close API");
}
});
use of moved value: `child`
move occurs because `child` has type `CommandChild`, which does not implement the `Copy` trait
Any ideas?

multiple websocket servers in rust

I referred this and also tried tungstenite library. But I was able to run only one server at a time, it captured whole thread.
I tried running multiple servers on different thread but that never listen anything and just exit the program.
Is there anyway that I can run multiple WebSocket servers on different ports, and create, destroy a server in runtime?
Edit: If I run a server on main thread and another one on other thread, it works, looks like I'd have to keep main thread busy somehow.. but is there any better way?
here's some example code:
it uses:
use std::net::TcpListener;
use std::thread::spawn;
use tungstenite::accept;
this is the normal code that blocks the main thread
let server = TcpListener::bind("127.0.0.1:9002").expect("err: ");
for stream in server.incoming() {
spawn(move || {
let mut websocket = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read_message().unwrap();
println!("{}", msg);
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
websocket.write_message(msg).unwrap();
}
}
});
}
here's the code with thread:
spawn(|| {
let server = TcpListener::bind("127.0.0.1:9001").expect("err: ");
for stream in server.incoming() {
spawn(move || {
let mut websocket = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read_message().unwrap();
println!("{}", msg);
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
websocket.write_message(msg).unwrap();
}
}
});
}
});
but the above code needs the main thread to run somehow, I'm indeed able to run multiple servers on different threads but need something to occupy main thread.
Rust programs terminate when the end of main() is reached. What you need to do is wait until your secondary threads have finished.
std::thread::spawn returns a JoinHandle, which has a join method which does exactly that - it waits (blocks) until the thread that the handle refers to finishes, and returns an error if the thread panicked.
So, to keep your program alive as long as any threads are running, you need to collect all of these handles, and join() them one by one. Unlike a busy-loop, this will not waste CPU resources unnecessarily.
use std::net::TcpListener;
use std::thread::spawn;
use tungstenite::accept;
fn main() {
let mut handles = vec![];
// Spawn 3 identical servers on ports 9001, 9002, 9003
for i in 1..=3 {
let handle = spawn(move || {
let server = TcpListener::bind(("127.0.0.1", 9000 + i)).expect("err: ");
for stream in server.incoming() {
spawn(move || {
let mut websocket = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read_message().unwrap();
println!("{}", msg);
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
websocket.write_message(msg).unwrap();
}
}
});
}
});
handles.push(handle);
}
// Wait for each thread to finish before exiting
for handle in handles {
if let Err(e) = handle.join() {
eprintln!("{:?}", e)
}
}
}
When you do all the work in a thread (or threads) and the main thread has nothing to do, usually it is set to wait (join) that thread.
This has the additional advantage that if your secondary thread finishes or panics, then your program will also finish. Or you can wrap the whole create-thread/join-thread in a loop and make it more resilient:
fn main() {
loop {
let th = std::thread::spawn(|| {
// Do the real work here
std::thread::sleep(std::time::Duration::from_secs(1));
panic!("oh!");
});
if let Err(e) = th.join() {
eprintln!("Thread panic: {:?}", e)
}
}
}
Link to playground, I've changed to the loop into a for _ in ..3 because playgrond does not like infinite loops.

how to implement checking for a timer on a webpage

I'm still learning, but I've been trying to build a faucet bot for 5minutebitcoin.com,
using selenium-webdriver in nodejs. And I've been trying to figure out how to implement a check, for the 5 minute timer. So the script would check to see if the timer is present, if it is then sleep for the remaining countdown. And if ther's no timer left on the countdown then proceed, with the script. So far I have
const { Builder, By, Key, until } = require('selenium-webdriver');
let chrome = require('selenium-webdriver/chrome');
let address = 'btcAddress';
let balanceUrl = 'http://5minutebitcoin.com/check-balance/?the99btcbfaddress=';
let claimBtcUrl = 'http://5minutebitcoin.com/';
async function main() {
while (true) {
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(new chrome.Options().headless())
.build();
let getBalance = await driver.get(balanceUrl + address);
driver.manage().setTimeouts({implicit: 5000});
balanceText = await driver.findElement(By.css('div.row.info'));
console.log(await balanceText.getText());
try {
let minuteTimerText = await driver.findElement(By.css('timer'));
// assert(minuteTimerText = true);
return true
}
catch(err) {
console.log("\n Timer not ready: ");
sleep(5)
}
let claimBtc = await driver.get(claimBtcUrl);
driver.manage().setTimeouts({implicit: 5000});
submit = await driver.findElement(By.name('claim_coins')).click()
console.log("\n Submitting to faucet using: " + address + "\n\n");
await driver.quit();
}
};
main()
At the moment it does:
Unpaid address balance: 72705 Satoshis
Address seniority: 9 days
Seniority bonus: 5% on all direct payouts
Time until next seniority level: 5 days
Submits per 24 hours: 15 / 48
Timer not ready:
Submitting to faucet using:
And loops.....
It's supposed to do:
Get the address balance and log.
Then check to see if the timer is active.
And if the timer is still active wait till the timer runs out.
solve the captcha
Then click claim coins.
And repeat every 5 minutes.
I'll be using tesseract-ocr to solve the captcha

How can I restart the watcher when it was failed?

everyone
I am using rust to code a program to notify a directory
1.Start a watcher
2.loop hit the case and then do sth.
because i use the Ok(DebouncedEvent::Create(p)), so i first remove the directory(is being watched),and then creat it, but the watcher fails to keeping watching
so i think fs maybe be not atomicity, so i sleep 3s, but it fails again
and then i try to delete the files but not the directory but it fails again
start a watcher
// Create a channel to receive the events.
let (tx, rx) = channel();
// Create a watcher
let mut watcher: RecommendedWatcher = try!(Watcher::new(tx.clone(), Duration::from_secs(policy_interval as u64)));
// Path to be monitored
try!(watcher.watch(metrics_repo_path.as_path(), RecursiveMode::Recursive));
my loop
loop
{ // Step 2: Start monitoring metrics repository
match rx.recv()
{
Ok(DebouncedEvent::Create(p)) =>
{
eprintln!("OK OK, loop start");
if let Some(ext) = p.extension()
{
if num_log_files == num_instances
{ // We have all logs for this epoch
remove_dir_contents(metrics_repo_path.to_str()).unwrap();
thread::sleep(Duration::from_millis(3000));
// // Remove old rates files
// // TODO: remove only files in the repo
// let _ = Command::new("rm")
// .arg("-r")
// .arg(metrics_repo_path.to_str().unwrap())
// .output()
// .expect("Failed to remove log files.");
// // Create a new rates folder
// let _ = Command::new("mkdir")
// .arg(metrics_repo_path.to_str().unwrap())
// .output()
// .expect("Failed to create new rates folder.");
}
else
{ // No re-configuration was issued
epochs_since_reconfiguration += 1;
}
// Clear epoch information
epoch_files.remove(epoch);
}
}
}
},
Err(e) => panic!("Monitoring error: {:?}", e),
_ => {}
}
}
the fn
fn remove_dir_contents<P: AsRef<Path>>(path: P) -> io::Result<()> {
for entry in fs::read_dir(path)? {
fs::remove_file(entry?.path())?;
}
Ok(())
}
i also try just restart the watcher but fails
like this
remove_dir_contents(metrics_repo_path.to_str()).unwrap();
thread::sleep(Duration::from_millis(3000));
try!(watcher.watch(metrics_repo_path.as_path(), RecursiveMode::Recursive));
In my opinoin my code will go to the condition
Ok(DebouncedEvent::Create(p))
**but it fails go to it even though new file was created in the watched file **
Any help is greatly appreciated.

Grabbing data from mpsc::channel without having it lock if there is no data waiting

I'm writing a small game in Rust to learn about multithreading. I got code that contains two loops, one with the logic, one with the rendering, like this:
let (t1_entity_in, t1_entity_out) = mpsc::channel(); // ommited type definitions
let (t1_event_in, t1_event_out) = mpsc::channel();
let entity = Entity::new(20,20);
std::thread::spawn(move || {
let window = Window::new(1280,720);
loop {
// waits until parent send data
let entity = t1_entity_out.recv().unwrap();
window.draw(entity);
window.flip();
let events = window.get_events();
// parent starts working
}
});
'event_loop: loop {
// do stuff to the entity
t1_entity_in.send(entity.clone());
// thread 1 starts workinng
// waits until thread 1 sends data
let events = t1_event_out.recv().unwrap(); // [1]
// thread 1 sent data, continues.
for event in events {
if event.type == event::QUIT {
break 'event_loop;
}
}
}
This code works, but it is pretty much behaving the same way as single thread would. Behavior I want is that at line marked [1], if there is a event iterator waiting, get it, but if there isn't just give me a None and keep going. How do I do that?
I think you need try_recv():
let events = match t1_event_out.try_recv() {
Ok(events) => events,
Err(TryRecvError::Empty) => continue,
Err(TryRecvError::Disconnected) => break,
};

Resources