Skip to content

Commit

Permalink
tests: Send message via websocket to know when the textarea is focus…
Browse files Browse the repository at this point in the history
…ed and ready for input

* CI: Remove function to maximize browser

* tests: Send message via websocket to know when the textarea is focused and ready for input
  • Loading branch information
pentamassiv authored Sep 8, 2024
1 parent 8ddf90d commit 49e587a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 69 deletions.
11 changes: 6 additions & 5 deletions tests/common/browser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Launch Firefox in kiosk mode (full screen and can't be closed with F11)
//
// n.b. static items do not call [`Drop`] on program termination, so this won't
// be deallocated. this is fine, as the OS can deallocate the terminated program
// faster than we can free memory but tools like valgrind might report "memory
// leaks" as it isn't obvious this is intentional. Launch Firefox on Linux and
// Windows and launch Safari on macOS
// leaks" as it isn't obvious this is intentional.
pub static BROWSER_INSTANCE: std::sync::LazyLock<Option<std::process::Child>> =
std::sync::LazyLock::new(|| {
// Construct the URL
Expand All @@ -16,19 +17,19 @@ pub static BROWSER_INSTANCE: std::sync::LazyLock<Option<std::process::Child>> =
std::process::Command::new("cmd")
.args(["/C", "start", "firefox", "--kiosk", &url])
.spawn()
.expect("Failed to start Firefox on Windows")
.expect("Failed to start Firefox")
} else if cfg!(target_os = "macos") {
// On macOS, use the "open" command to run Firefox
std::process::Command::new("open")
.args(["-a", "Firefox", "--args", "--kiosk", &url])
.spawn()
.expect("Failed to start Firefox on macOS")
.expect("Failed to start Firefox")
} else {
// On Linux, use the "firefox" command
std::process::Command::new("firefox")
.args(["--kiosk", &url])
.spawn()
.expect("Failed to start Firefox on Linux")
.expect("Failed to start Firefox")
};
Some(child)
});
5 changes: 5 additions & 0 deletions tests/common/browser_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use tungstenite::Message;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BrowserEvent {
ReadyForText,
Text(String),
KeyDown(String),
KeyUp(String),
Expand Down Expand Up @@ -52,6 +53,10 @@ impl TryFrom<Message> for BrowserEvent {
#[test]
fn deserialize_browser_events() {
let messages = vec![
(
Message::Text("ReadyForText".to_string()),
BrowserEvent::ReadyForText,
),
(
Message::Text("Text(\"Testing\")".to_string()),
BrowserEvent::Text("Testing".to_string()),
Expand Down
24 changes: 6 additions & 18 deletions tests/common/enigo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,6 @@ impl EnigoTest {
Self { enigo, websocket }
}

// Maximize Firefox by pressing keys or moving the mouse
pub fn maximize_browser(&mut self) {
if cfg!(target_os = "macos") {
// self.key(Key::Control, Press).unwrap();
// self.key(Key::Meta, Press).unwrap();
self.key(Key::Unicode('f'), Click).unwrap();
// self.key(Key::Meta, Release).unwrap();
// self.key(Key::Control, Release).unwrap();
} else {
self.key(Key::F11, Click).unwrap();
self.move_mouse(200, 200, Abs).unwrap();
self.button(Button::Left, Click).unwrap();
};

// Wait for full screen animation
std::thread::sleep(std::time::Duration::from_millis(3000));
}

fn websocket() -> tungstenite::WebSocket<TcpStream> {
let listener = TcpListener::bind("127.0.0.1:26541").unwrap();
println!("TcpListener was created");
Expand Down Expand Up @@ -99,6 +81,12 @@ impl Keyboard for EnigoTest {
// This does not work for all text or the library does not work properly
fn fast_text(&mut self, text: &str) -> enigo::InputResult<Option<()>> {
self.send_message("ClearText");
println!("Attempt to clear the text");
assert_eq!(
BrowserEvent::ReadyForText,
self.read_message(),
"Failed to get ready for the text"
);
let res = self.enigo.text(text);
std::thread::sleep(std::time::Duration::from_millis(INPUT_DELAY)); // Wait for input to have an effect
self.send_message("GetText");
Expand Down
74 changes: 30 additions & 44 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,34 @@

<body>
<h1>Conducted tests</h1>
<form action="/action_page.php">
<input type="checkbox" id="KeyDown" name="KeyDown">
<label for="KeyDown"> KeyDown</label><br>
<input type="checkbox" id="KeyUp" name="KeyUp">
<label for="KeyUp"> KeyUp</label><br>
<input type="checkbox" id="MouseDown" name="MouseDown">
<label for="MouseDown"> MouseDown</label><br>
<input type="checkbox" id="MouseUp" name="MouseUp">
<label for="MouseUp"> MouseUp</label><br>
<input type="checkbox" id="MouseMove" name="MouseMove">
<label for="MouseMove"> MouseMove</label><br>
<input type="checkbox" id="MouseScroll" name="MouseScroll">
<label for="MouseScroll"> MouseScroll</label><br>
</form>
<form id="textForm">
<label for="text">Enter text:</label>
<input type="text" id="text" name="text" />
<button type="submit">Submit</button>
</form>
<input type="checkbox" id="KeyDown" name="KeyDown">
<label for="KeyDown"> KeyDown</label><br>
<input type="checkbox" id="KeyUp" name="KeyUp">
<label for="KeyUp"> KeyUp</label><br>
<input type="checkbox" id="MouseDown" name="MouseDown">
<label for="MouseDown"> MouseDown</label><br>
<input type="checkbox" id="MouseUp" name="MouseUp">
<label for="MouseUp"> MouseUp</label><br>
<input type="checkbox" id="MouseMove" name="MouseMove">
<label for="MouseMove"> MouseMove</label><br>
<input type="checkbox" id="MouseScroll" name="MouseScroll">
<label for="MouseScroll"> MouseScroll</label><br>
<textarea id="text" name="text" rows="20" cols="50"></textarea><br>

<p id="output1">Test did not start. Do you have JavaScript enabled?</p>
<script>
// Focus on the textarea when the page loads
window.onload = () => {
textArea.focus();
};

// Prevent other elements from gaining focus
document.addEventListener('focusin', (event) => {
if (event.target !== textArea) {
event.preventDefault();
textArea.focus();
}
});

const ws = new WebSocket('ws://localhost:26541');

// Helper function to handle events
Expand All @@ -42,34 +48,15 @@ <h1>Conducted tests</h1>
ws.send(message);
};

document.addEventListener('open', (event) => {
handleEvent('Open', event);
document.getElementById('output1').innerHTML = 'Test was started. Do not close the page.';
});
document.addEventListener('close', (event) => {
handleEvent('Close', event);
document.getElementById('output1').innerHTML = 'Test concluded. Close this page.';
});
document.addEventListener('open', (event) => handleEvent('Open', event));
document.addEventListener('close', (event) => handleEvent('Close', event));
document.addEventListener('keydown', (event) => handleEvent('KeyDown', `(\"${event.key}\")`));
document.addEventListener('keyup', (event) => handleEvent('KeyUp', `(\"${event.key}\")`));
document.addEventListener('mousedown', (event) => handleEvent('MouseDown', `(${event.button})`));
document.addEventListener('mouseup', (event) => handleEvent('MouseUp', `(${event.button})`));
document.addEventListener('mousemove', (event) => handleEvent('MouseMove', `((${event.movementX},${event.movementY}),(${event.screenX},${event.screenY}))`));
document.addEventListener('wheel', (event) => handleEvent('MouseScroll', `(${event.deltaX},${event.deltaY})`));

// Handle form submission
document.getElementById('textForm').addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting the traditional way

const text = document.getElementById('text').value;

// Send the input text via WebSocket
ws.send(`Text(${text})`);

// Clear the input field
document.getElementById('text').value = '';
});

// Handle incoming WebSocket messages
ws.addEventListener('message', (event) => {
console.log('Received message:', event.data);
Expand All @@ -78,6 +65,8 @@ <h1>Conducted tests</h1>
if (event.data === 'ClearText') {
document.getElementById('text').value = '';
document.getElementById('text').focus();
// Send the input text via WebSocket
ws.send(`ReadyForText`);
}

// Server asks for the form's content
Expand All @@ -88,9 +77,6 @@ <h1>Conducted tests</h1>
ws.send(`Text(\"${text}\")`);
}
});



</script>
</body>

Expand Down
2 changes: 0 additions & 2 deletions tests/integration_browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use common::enigo_test::EnigoTest as Enigo;
fn integration_browser_events() {
let mut enigo = Enigo::new(&Settings::default());

// enigo.maximize_browser();

enigo.text("TestText❤️").unwrap(); // Fails on Windows (Message is empty???)
enigo.key(Key::F1, Click).unwrap();
enigo.key(Key::Control, Click).unwrap();
Expand Down

0 comments on commit 49e587a

Please sign in to comment.