Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2e testkit #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ node_modules/

.vscode
.env
.DS_Store
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ serde_with = "3.0.0"
colored_json = "5"
chrono = "0.4.26"
walkdir = "2.3.3"
thirtyfour = "0.32.0"
# core-foundation = {git="https://github.com/servo/core-foundation-rs", rev="9effb788767458ad639ce36229cc07fd3b1dc7ba"}

[dev-dependencies]
httpmock = "0.7"
testing_logger = "0.1.1"

[workspace]
27 changes: 27 additions & 0 deletions browser-test.tk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- metadata:
name: Login To Talstack
description: test login
headless: false
browser: firefox
groups:
- group: Login to Talstack
steps:
- visit: 'app.talstack.com'
- find: .social-button_SocialButton__C6hcE
click: true
- wait: 2000
- find_xpath: '//*[@id="identifierId"]'
type_text: [email protected]
- wait: 2000
- find_xpath: '//*[@id="identifierNext"]'
click: true
- wait: 2000
- find_xpath: '//*[@id="password"]/div[1]/div/div[1]/input'
type_text: "loleremm"
- find_xpath: '//*[@id="passwordNext"]'
click: true
- wait: 10000
- group: Register a user
steps:
- visit: 'app.talstack.com/'

181 changes: 181 additions & 0 deletions src/base_browser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
use std::time::Duration;

use anyhow::Result;
use serde::{Deserialize, Serialize};
use thirtyfour::prelude::*;
use thirtyfour::DesiredCapabilities;

#[derive(Deserialize, Serialize, Debug)]
pub struct TestStep {
visit: Option<String>,
find: Option<String>,
find_xpath: Option<String>,
#[serde(default)]
type_text: Option<String>,
#[serde(default)]
click: Option<bool>,
#[serde(default)]
wait: Option<u64>,
assert: Option<Vec<Assertion>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Assertion {
array: Option<String>,
array_xpath: Option<String>,
empty: Option<String>,
empty_xpath: Option<String>,
string: Option<String>,
string_xpath: Option<String>,
equal: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TestItem {
metadata: Option<Metadata>,
groups: Vec<Group>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
name: Option<String>,
description: Option<String>,
headless: Option<bool>,
browser: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Group {
group: String,
steps: Vec<TestStep>,
}

#[derive(Debug, Default, Serialize)]
pub struct RequestResult {
pub step_name: Option<String>,
pub step_index: u32,
}
pub async fn run_browser(
test_cases: &Vec<TestItem>,
should_log: bool,
) -> Result<Vec<RequestResult>, Box<dyn std::error::Error>> {
let mut driver = None;

// Find the metadata to configure the browser
for (i, item) in test_cases.iter().enumerate() {
if let Some(metadata) = &item.metadata {
log::debug!("running on : {:?}", metadata.browser);

driver = match &metadata.browser {
Some(browser_str) => {
let caps = match browser_str.as_str() {
"firefox" => {
println!("Initializing Firefox");
let mut caps = DesiredCapabilities::firefox();
if metadata.headless.unwrap_or(false) {
caps.set_headless()?;
}
caps
}
_ => {
println!(
"Unrecognized browser '{}', defaulting to Firefox",
browser_str
);
let mut caps = DesiredCapabilities::firefox();
if metadata.headless.unwrap_or(false) {
caps.set_headless()?;
}
caps
}
};

Some(WebDriver::new("http://localhost:4444", caps).await?)
}
None => {
println!("No browser specified, defaulting to Firefox");
let mut caps = DesiredCapabilities::firefox();
if metadata.headless.unwrap_or(false) {
caps.set_headless()?;
}
Some(WebDriver::new("http://localhost:4444", caps).await?)
}
};

break;
}
}

if driver.is_none() {
log::debug!("No driver configuration found in metadata");
}

let driver = driver.unwrap();

let mut all_results = Vec::new();

for test_case in test_cases {
let result = base_browser(test_case, driver.clone()).await;
match result {
Ok(mut res) => {
if should_log {
log::debug!("Test passed: {:?}", res);
}
all_results.append(&mut res);
}
Err(err) => {
if should_log {
log::error!(target: "testkit", "{}", err);
}
return Err(err);
}
}
}

Ok(all_results)
}

pub async fn base_browser(
test_item: &TestItem,
client: WebDriver,
) -> Result<Vec<RequestResult>, Box<dyn std::error::Error>> {
let mut results: Vec<RequestResult> = Vec::new();

for (i, group) in test_item.groups.iter().enumerate() {
println!("Running group: {:?}", group.group);

for (j, step) in group.steps.iter().enumerate() {
if let Some(url) = &step.visit {
client.get(url).await?;
}
if let Some(selector) = &step.find {
let element = client.find(By::Css(selector)).await?;
if step.click.unwrap_or(false) {
element.click().await?;
}
if let Some(text) = &step.type_text {
element.send_keys(text).await?;
}
}
if let Some(xpath) = &step.find_xpath {
let element = client.find(By::XPath(xpath)).await?;
if step.click.unwrap_or(false) {
element.click().await?;
}
if let Some(text) = &step.type_text {
element.send_keys(text).await?;
}
}
if let Some(wait_time) = step.wait {
tokio::time::sleep(Duration::from_millis(wait_time)).await;
}

results.push(RequestResult {
step_name: Some(format!("{} - step {}", group.group, j)),
step_index: i as u32,
});
}
}

client.quit().await?;
Ok(results)
}
9 changes: 8 additions & 1 deletion src/base_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
#[command(name = "testkit")]
#[command(author = "APIToolkit. <[email protected]>")]
#[command(version = "1.0")]
#[command(about = "Manually and Automated testing starting with APIs", long_about = None)]
#[command(about = "Manually and Automated testing starting with APIs and Browser", long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
Expand All @@ -18,6 +18,13 @@ pub struct Cli {
#[derive(Subcommand)]
pub enum Commands {
Test {
/// Run browser tests
#[arg(short = 'i', long)]
api: bool,

#[arg(short = 'b', long)]
browser: bool,

/// Sets the YAML test configuration file
#[arg(short, long)]
file: Option<PathBuf>,
Expand Down
3 changes: 1 addition & 2 deletions src/base_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use reqwest::header::{HeaderMap, HeaderValue};
use rhai::Engine;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::{serde_as, DisplayFromStr};
use serde_yaml::with;
use serde_with::serde_as;
use std::{collections::HashMap, env, env::VarError};
use thiserror::Error;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use base_request::{RequestResult, TestContext};
use libc::c_char;
use std::ffi::{CStr, CString};
use std::ffi::CStr;

pub mod base_cli;
pub mod base_request;
Expand Down
36 changes: 33 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![feature(extend_one)]
pub mod base_browser;
pub mod base_cli;
pub mod base_request;

use anyhow::Ok;
use base_browser::TestItem;
use base_cli::Commands;
use base_request::{RequestResult, TestContext};
use clap::Parser;
Expand Down Expand Up @@ -32,11 +34,18 @@ async fn main() {

match cli_instance.command {
None | Some(Commands::App {}) => {}
Some(Commands::Test { file }) => cli(file).await.unwrap(),
Some(Commands::Test { file, api, browser }) => {
if api {
cli_api(file.clone()).await.unwrap();
}
if browser {
cli_browser(file).await.unwrap();
}
}
}
}

async fn cli(file_op: Option<PathBuf>) -> Result<(), anyhow::Error> {
async fn cli_api(file_op: Option<PathBuf>) -> Result<(), anyhow::Error> {
match file_op {
Some(file) => {
let content = fs::read_to_string(file.clone())?;
Expand Down Expand Up @@ -64,6 +73,27 @@ async fn cli(file_op: Option<PathBuf>) -> Result<(), anyhow::Error> {
}
}

async fn cli_browser(file_op: Option<PathBuf>) -> Result<(), anyhow::Error> {
match file_op {
Some(file) => {
let content = fs::read_to_string(file.clone()).expect("Unable to read file");
let test_cases: Vec<TestItem> =
serde_yaml::from_str(&content).expect("Unable to parse YAML");
let _ = base_browser::run_browser(&test_cases, true).await;
}
None => {
let files = find_tk_yaml_files(Path::new("."));
for file in files {
let content = fs::read_to_string(file.clone()).expect("Unable to read file");
let test_cases: Vec<TestItem> =
serde_yaml::from_str(&content).expect("Unable to parse YAML");
base_browser::run_browser(&test_cases, true).await;
}
}
}
Ok(())
}

fn find_tk_yaml_files(dir: &Path) -> Vec<PathBuf> {
let mut result = Vec::new();
for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
Expand Down