### Install LaVague QA from Source Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/usage Navigates to the lavague-qa package directory and installs it in editable mode using pip. Requires the repository to be cloned. ```bash cd LaVague/lavague-qa pip install -e . ``` -------------------------------- ### Install Optional Gemini Context Source: https://docs.lavague.ai/en/latest/docs/get-started/installation Installs the Gemini context package for La vague, allowing the use of Gemini as a non-default context. This is an example of installing optional packages not included in the core bundle. ```shell pip install lavague.contexts.gemini ``` -------------------------------- ### Full Debug Setup Example Source: https://docs.lavague.ai/en/latest/docs/learn/debug-tools A comprehensive example combining Chrome debug session attachment, agent initialization, and step-by-step execution with visual node highlighting. It sets up the driver, world model, action engine, and agent, then navigates a URL and highlights nodes at different stages. ```python from lavague.core import WorldModel, ActionEngine from lavague.core.agents import WebAgent from lavague.drivers.selenium import SeleniumDriver from selenium.webdriver.chrome.options import Options # add debug port to chrome options chrome_options = Options() chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # include chrome options selenium_driver = SeleniumDriver(options=chrome_options) world_model = WorldModel() action_engine = ActionEngine(selenium_driver) # create the agent, don't run it agent = WebAgent(world_model, action_engine) # prepare for the run and load the URL objective = "Add cat food to my cart" agent.prepare_run() agent.get("https://amazon.com/") # highlight nodes in red selenium_driver.highlight_interactive_nodes(color="red") # run one step and highlight nodes in blue agent.run_step(objective) selenium_driver.highlight_interactive_nodes(color = "blue") # run another step and highlight nodes in green agent.run_step(objective) selenium_driver.highlight_interactive_nodes(color = "green") ``` -------------------------------- ### Install Poetry and Dependencies Source: https://docs.lavague.ai/en/latest/docs/contributing/general These commands are used to set up the development environment for LaVague using Poetry. It first installs Poetry via pipx, then activates the Poetry shell and installs the project with development dependencies. ```bash sudo apt update sudo apt install pipx pipx install poetry poetry shell poetry install --with dev ``` -------------------------------- ### Clone LaVague QA Repository Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/usage Clones the LaVague repository from GitHub to install from the latest source. Requires Git to be installed. ```bash git clone https://github.com/lavague-ai/LaVague.git ``` -------------------------------- ### Install LaVague QA with pip Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/usage Installs the latest release of the lavague-qa package using pip. Ensure you have Python and pip installed. ```bash pip install lavague-qa ``` -------------------------------- ### Install LaVague QA using pip Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/quick-tour Installs the LaVague QA tool using pip. This is the initial step to set up the testing framework. ```bash pip install lavague-qa ``` -------------------------------- ### Install lavague-tests CLI Source: https://docs.lavague.ai/en/latest/docs/get-started/testing Installs the lavague-tests command-line interface tool using pip. ```bash pip install lavague-tests ``` -------------------------------- ### Install LaVague and text-generation libraries Source: https://docs.lavague.ai/en/latest/docs/examples/job-application Installs the LaVague and text-generation Python libraries using pip. These are essential for running the automated job application example. ```bash pip install text-generation lavague ``` -------------------------------- ### Install Fireworks context package Source: https://docs.lavague.ai/en/latest/docs/get-started/quick-tour Installs the Fireworks context package for LaVague, enabling the use of Fireworks AI models. ```bash pip install lavague-contexts-fireworks ``` -------------------------------- ### Install La vague from Source Source: https://docs.lavague.ai/en/latest/docs/get-started/installation Installs La vague from its source code repository. This method is useful for developers who want to modify the library locally or contribute to its development. It performs an editable installation. ```shell pip install -e . ``` -------------------------------- ### Install Non-Default LaVague Integration Source: https://docs.lavague.ai/en/latest/docs/contributing/general This command installs a specific, non-default integration package for LaVague using pip. The example shows how to install the Gemini context integration, allowing you to use specific features not included in the default installation. ```bash pip install -e lavague-integrations/contexts/lavague-contexts-gemini ``` -------------------------------- ### Print World Model General Examples - Python Source: https://docs.lavague.ai/en/latest/docs/module-guides/world-model Retrieves and prints the general examples used by the World Model to provide world knowledge to the LLM. These examples help the model understand context and generate relevant instructions. ```python from lavague.core.world_model import WORLD_MODEL_GENERAL_EXAMPLES print(WORLD_MODEL_GENERAL_EXAMPLES) ``` -------------------------------- ### Example config.yml for HuggingFace tests Source: https://docs.lavague.ai/en/latest/docs/get-started/testing This example demonstrates how to configure tests for HuggingFace's website. It includes navigation and search tests, specifying URLs, prompts, and expectations based on URL, status, and HTML content. ```yaml tasks: - name: HuggingFace navigation url: https://huggingface.co/docs prompt: Go on the quicktour of PEFT expect: - URL is https://huggingface.co/docs/peft/quicktour - Status is success - HTML contains PEFT offers parameter-efficient methods for finetuning large pretrained models - name: HuggingFace search url: https://huggingface.co prompt: Find the-wave-250 dataset expect: - URL is https://huggingface.co/datasets/BigAction/the-wave-250 - Status is success ``` -------------------------------- ### Install LaVague Source: https://docs.lavague.ai/en/latest/docs/examples/hf-papers-retrieval Installs the LaVague library using pip. This is a prerequisite for running the agent. ```shell pip install lavague ``` -------------------------------- ### Pytest setup for Amazon cart testing Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples This Python code sets up the pytest environment for testing Amazon's cart functionality. It includes necessary imports, defines constants, loads scenarios from a feature file, and configures a pytest fixture for the Selenium WebDriver. ```python import pytest from pytest_bdd import scenarios, given, when, then, parsers from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ElementClickInterceptedException import time # Constants BASE_URL = "https://www.amazon.fr/" # Scenarios scenarios("demo_amazon.feature") # Fixtures @pytest.fixture def browser(): driver = webdriver.Chrome() driver.implicitly_wait(10) yield driver driver.quit() ``` -------------------------------- ### Setup Pytest WebDriver Fixture (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Defines a pytest fixture to initialize and manage the Selenium WebDriver instance. It ensures the browser is properly quit after the test session, utilizing Chrome in this example. ```python import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() driver.implicitly_wait(10) yield driver driver.quit() ``` -------------------------------- ### LaVague QA CLI Help Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/usage Displays all available arguments and options for the lavague-qa command-line interface. Use this to understand the tool's capabilities. ```bash lavague-qa --help ``` -------------------------------- ### Install Documentation Tools Source: https://docs.lavague.ai/en/latest/docs/contributing/documentation Installs the necessary Python packages for working with MkDocs, including MkDocs itself, mkdocs-jupyter for notebook integration, and mkdocs-material for the theme. ```shell pip install mkdocs mkdocs-jupyter mkdocs-material ``` -------------------------------- ### Install GeminiContext Package Source: https://docs.lavague.ai/en/latest/docs/integrations/gemini Installs the necessary GeminiContext package for LaVague using pip. This is a prerequisite for using Gemini models with LaVague. ```bash pip install lavague-contexts-gemini ``` -------------------------------- ### Serve Documentation Locally Source: https://docs.lavague.ai/en/latest/docs/contributing/documentation Starts a local development server for the documentation. This allows you to see your changes in real-time as you edit the documentation files. ```shell mkdocs serve ``` -------------------------------- ### Replit.com LaVague Setup Source: https://docs.lavague.ai/en/latest/docs/get-started/troubleshoot Configures the replit.nix file to include necessary dependencies for running LaVague with the Selenium Driver in a Replit environment. ```nix {pkgs}: { deps = [ pkgs.geckodriver pkgs.python38Full pkgs.chromium pkgs.chromedriver ]; } ``` -------------------------------- ### Launch LaVague agent in Gradio Demo mode Source: https://docs.lavague.ai/en/latest/docs/get-started/quick-tour Launches an interactive Gradio interface for using the web agent. It sets up the agent and navigates to a URL before starting the demo. ```python driver = SeleniumDriver(headless=True) action_engine = ActionEngine(driver) world_model = WorldModel() # Create Web Agent agent = WebAgent(world_model, action_engine) # Set URL agent.get("https://huggingface.co/docs") # Launch the agent in the Agent Gradio Demo mode agent.demo("Go on the quicktour of PEFT") ``` -------------------------------- ### Replace World Model Examples Source: https://docs.lavague.ai/en/latest/docs/module-guides/world-model Initializes a new World Model instance, completely replacing the default examples with a custom string of examples. This allows for tailored reasoning and instruction generation. ```python new_examples = "[YOUR_EXAMPLES_HERE]" world_model = WorldModel(examples=new_examples) ``` -------------------------------- ### Install LaVague in a Virtual Environment Source: https://docs.lavague.ai/en/latest/docs/get-started/troubleshoot Demonstrates setting up a Python virtual environment and installing LaVague within it to avoid dependency conflicts and module import errors. ```bash python -m venv my_env source my_env/bin/activate pip install lavague python test.py ``` -------------------------------- ### Start New Profiling Step (Python) Source: https://docs.lavague.ai/en/latest/docs/core/utilities/profiling Initiates a new step in the profiling process. This function does not take any arguments and does not return any value. ```python def start_new_step() : ``` -------------------------------- ### Install lavague-contexts-anthropic Package Source: https://docs.lavague.ai/en/latest/docs/integrations/anthropic This command installs the necessary lavague package for integrating Anthropic models. Ensure you have Python and pip installed. ```bash pip install lavague-contexts-anthropic ``` -------------------------------- ### Local Package Installation (Bash) Source: https://docs.lavague.ai/en/latest/docs/learn/actions This command demonstrates how to install a modified local package for a driver, specifically the Selenium driver, using pip in editable mode. ```bash pip install -e lavague-integrations/drivers/lavague-drivers-selenium ``` -------------------------------- ### Install and Initialize PlaywrightDriver Source: https://docs.lavague.ai/en/latest/docs/module-guides/browser-drivers Provides instructions to install the Playwright driver package and then initialize the PlaywrightDriver, passing it to the ActionEngine. This driver offers an alternative to Selenium for browser automation. ```python pip install lavague.drivers.playwright from lavague.drivers.playwright import PlaywrightDriver playwright_driver = PlaywrightDriver() action_engine = ActionEngine(playwright_driver) ``` -------------------------------- ### Install LaVague and Integrations Source: https://docs.lavague.ai/en/latest/docs/examples/medical_appointment_booking Installs the LaVague library along with necessary integrations for Anthropic and Fireworks API. ```bash pip install lavague pip install llama-index-multi-modal-llms-anthropic pip install llama-index-llms-fireworks ``` -------------------------------- ### End-to-end Example with FireworksContext Source: https://docs.lavague.ai/en/latest/docs/integrations/fireworks Demonstrates initializing FireworksContext, SeleniumDriver, ActionEngine, and WorldModel, then building and running a WebAgent. It shows how to set up the agent and perform a web query. ```python from lavague.core import WorldModel, ActionEngine from lavague.core.agents import WebAgent from lavague.drivers.selenium import SeleniumDriver from lavague.contexts.fireworks import FireworksContext # Initialize Context context = FireworksContext() selenium_driver = SeleniumDriver() # Build Action Engine and World Model from Context action_engine = ActionEngine.from_context(context=context, driver=selenium_driver) world_model = WorldModel() # Build agent & run query agent = WebAgent(world_model, action_engine) agent.get("https://huggingface.co/") agent.run("What is this week's top Space of the week?") ``` -------------------------------- ### Navigate to Homepage (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Navigates the browser to the specified base URL. This function is a prerequisite for most other interaction steps. ```python from selenium.webdriver.remote.webdriver import WebDriver BASE_URL = "http://www.hsbc.com" @given('the user is on the HSBC homepage') def go_to_homepage(browser: WebDriver): browser.get(BASE_URL) ``` -------------------------------- ### Command to Generate Pytest from Feature File Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples This command initiates the pytest framework to execute feature files. It specifies the base URL of the website to test and the path to the feature file. ```bash lavague-qa --url https://laposte.fr/ --feature lavague-qa/features/demo_laposte.feature ``` -------------------------------- ### Install HuggingFace Embeddings for LaVague Source: https://docs.lavague.ai/en/latest/docs/examples/job-application Installs the HuggingFace embeddings library, which is used by LaVague's Action Engine for processing text data with local embedding models. ```shell pip install llama-index-embeddings-huggingface ``` -------------------------------- ### Install Lavague Server Package Source: https://docs.lavague.ai/en/latest/docs/get-started/docs-chrome Installs the necessary `lavague-server` Python package using pip. This package is required to run the LaVague Agent Server that the Chrome extension communicates with. ```bash pip install lavague-server ``` -------------------------------- ### Switch to New Tab (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Waits for 3 seconds and then switches the browser focus to the next available window handle, typically used after a link opens a new tab. ```python from selenium.webdriver.remote.webdriver import WebDriver import time @when('the user navigates to the new tab opened') def switch_tab(browser: WebDriver): time.sleep(3) browser.switch_to.window(browser.window_handles[1]) ``` -------------------------------- ### Get Capability Description Source: https://docs.lavague.ai/en/latest/docs/core/base_driver Prompts the LLM to explain the expected code style, variables, and imports for code generation. ```python def get_capability(self) -> str ``` -------------------------------- ### Python Web Agent Example using Selenium Source: https://docs.lavague.ai/en/latest/index This Python code demonstrates how to build and run a Web Agent using the LaVague framework with a Selenium driver. It initializes the agent, navigates to a URL, and executes a specific objective. Requires an OpenAI API key set as an environment variable. ```python from lavague.core import WorldModel, ActionEngine from lavague.core.agents import WebAgent from lavague.drivers.selenium import SeleniumDriver selenium_driver = SeleniumDriver(headless=False) world_model = WorldModel() action_engine = ActionEngine(selenium_driver) agent = WebAgent(world_model, action_engine) agent.get("https://huggingface.co/docs") agent.run("Go on the quicktour of PEFT") # Launch Gradio Agent Demo agent.demo("Go on the quicktour of PEFT") ``` -------------------------------- ### Setup LLMEvaluator and Navigation Engines (Python) Source: https://docs.lavague.ai/en/latest/docs/module-guides/evaluation Initializes the LLMEvaluator and creates two NavigationEngine instances, one using OpenaiContext and another using GeminiContext, both with SeleniumDriver. Requires OPENAI_API_KEY and GOOGLE_API_KEY environment variables. ```python from lavague.core.evaluator import LLMEvaluator from lavague.contexts.openai import OpenaiContext from lavague.contexts.gemini import GeminiContext from lavague.core.navigation import NavigationEngine import os from lavague.drivers.selenium import SeleniumDriver llm_evaluator = LLMEvaluator() openai_engine = NavigationEngine.from_context(OpenaiContext(), SeleniumDriver()) gemini_engine = NavigationEngine.from_context(GeminiContext(), SeleniumDriver()) ``` -------------------------------- ### End-to-End Example with Anthropic Context Source: https://docs.lavague.ai/en/latest/docs/integrations/anthropic Demonstrates initializing the AnthropicContext, setting up SeleniumDriver, and building the ActionEngine and WorldModel. It then uses a WebAgent to navigate to a URL and run a query. ```python from lavague.core import WorldModel, ActionEngine from lavague.core.agents import WebAgent from lavague.drivers.selenium import SeleniumDriver from lavague.contexts.anthropic import AnthropicContext # Initialize Context context = AnthropicContext() selenium_driver = SeleniumDriver() # Build Action Engine and World Model from Context action_engine = ActionEngine.from_context(context=context, driver=selenium_driver) world_model = WorldModel.from_context(context) # Build agent & run query agent = WebAgent(world_model, action_engine) agent.get("https://huggingface.co/") agent.run("What is this week's top Space of the week?") ``` -------------------------------- ### Enter Password and Login (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Enters a predefined password ('lavaguetest123') into the password field and clicks the login button. Similar to username entry, it ensures the element is ready before interacting. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @when('the user enters lavaguetest123 in the password field') def the_user_enters_lavaguetest123_in_the_password_field(browser: WebDriver): element = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div[3]/main/div[3]/div[3]/div[2]/div/form/div[2]/div[2]/div/input')) ) browser.execute_script('arguments[0].click();', element) element.clear() element.send_keys('lavaguetest123') ``` -------------------------------- ### Enter Username and Login (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Enters a predefined username ('Lavague-test') into the username field and clicks the login button. It handles clearing the field and sending keys, along with waiting for element interactability. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @when('the user enters Lavague-test in the username field') def the_user_enters_lavague_test_in_the_username_field(browser: WebDriver): element = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div[3]/main/div[3]/div[3]/div[2]/div/form/div/div[2]/div/input')) ) browser.execute_script('arguments[0].click();', element) element.clear() element.send_keys('Lavague-test') @when('the user clicks on login under the username and password field') def the_user_clicks_on_login_under_the_username_and_password_field(browser: WebDriver): element = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/div[3]/main/div[3]/div[3]/div[2]/div/form/div[4]/div/button')) ) browser.execute_script('arguments[0].click();', element) ``` -------------------------------- ### Run LaVague Web Agent to get temperature Source: https://docs.lavague.ai/en/latest/docs/learn/debugging This Python code initializes a Selenium driver, World Model, and Action Engine to create a Web Agent. It then navigates to a weather website, runs an objective to get the temperature in Birmingham, UK, and prints the output. It's used to demonstrate initial World Model behavior. ```python selenium_driver = SeleniumDriver(headless=False) world_model = WorldModel() action_engine = ActionEngine(driver=selenium_driver) agent = WebAgent(world_model, action_engine) url = "https://weather.com/" objective = "What is the temperature now in Birmingham UK?" agent.get(url) ret = agent.run(objective) print(ret.output) ``` -------------------------------- ### Navigate to Wikipedia Homepage (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Navigates the WebDriver to the specified base URL, typically the homepage of a website. This is a foundational step for most web automation tests. ```python from selenium.webdriver.remote.webdriver import WebDriver BASE_URL = 'YOUR_BASE_URL' @given('the user is on the Wikipedia homepage') def the_user_is_on_the_wikipedia_homepage(browser: WebDriver): browser.get(BASE_URL) ``` -------------------------------- ### LaVague Web Agent Setup and Execution Source: https://docs.lavague.ai/en/latest/docs/examples/job-application Sets up and runs a LaVague WebAgent. It initializes a SeleniumDriver, ActionEngine with HuggingFace embeddings, and a WorldModel. The agent is then directed to a URL and instructed to fill out a form. ```python from lavague.drivers.selenium import SeleniumDriver from lavague.core import ActionEngine, WorldModel from lavague.core.agents import WebAgent selenium_driver = SeleniumDriver() action_engine = ActionEngine(selenium_driver, embedding=embedding) world_model = WorldModel() agent = WebAgent(world_model, action_engine) url = "https://form.jotform.com/241472287797370" objective = "Fill out this form. Do not provide a cover letter" agent.get(url) agent.run(objective, user_data=user_data, display=True) ``` -------------------------------- ### LaVague Action Definition Example (YAML) Source: https://docs.lavague.ai/en/latest/docs/learn/actions This snippet shows the expected YAML output format for defining a 'click' action with its arguments, as provided to the Action Engine LLM. ```yaml - actions: - action: # Thus, we believe this is the correct element to be interacted with: args: xpath: "/html/body/div[2]/div[2]/div[3]/span/div/div/div/div[3]/div[1]/button[2]" value: "" # Then we can click on the button name: "click" ``` -------------------------------- ### Generate Tests with LaVague QA Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/usage Runs LaVague QA with a specified URL and a Gherkin feature file to generate Python tests. Requires OPENAI_API_KEY to be set. ```bash lavague-qa --url https://example.com --feature example.feature ``` -------------------------------- ### Using LaVague Agent with SeleniumDriver Source: https://docs.lavague.ai/en/latest/docs/module-guides/browser-drivers Shows a complete example of using the LaVague WebAgent after initializing the SeleniumDriver and ActionEngine. It includes setting up the WorldModel, creating the agent, navigating to a URL, and running a task. ```python from lavague.core import WorldModel, ActionEngine from lavague.core.agents import WebAgent world_model = WorldModel() agent = WebAgent(WorldModel(), action_engine) agent.get("https://huggingface.co/docs") result = agent.run("Go on the quicktour of PEFT") ``` -------------------------------- ### Get Initialization Code Source: https://docs.lavague.ai/en/latest/docs/core/base_driver Extracts the necessary code for initializing the driver, intended to be placed at the beginning of a script. ```python def code_for_init(self) -> str ``` -------------------------------- ### Initialize SeleniumDriver and ActionEngine Source: https://docs.lavague.ai/en/latest/docs/module-guides/browser-drivers Demonstrates how to initialize the SeleniumDriver, which is the default and most feature-complete driver, and then pass it to the ActionEngine for web automation tasks. It assumes 'lavague' is already installed. ```python from lavague.drivers.selenium import SeleniumDriver driver = SeleniumDriver() action_engine = ActionEngine(selenium_driver) ``` -------------------------------- ### Navigate to Login Page (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Locates and clicks a specific element on the page, likely a login link or button, to navigate the user to the login interface. It uses WebDriverWait for reliable element interaction. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @when('the user navigates to the login page') def the_user_navigates_to_the_login_page(browser: WebDriver): element = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div/header/div[2]/nav/div/div[4]/div/ul/li[2]/a')) ) browser.execute_script('arguments[0].click();', element) ``` -------------------------------- ### Run LaVague QA with Custom Context Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/usage Executes LaVague QA using a custom context file, allowing for the definition of specific LLMs and token counters. Requires the `--context` flag pointing to a Python file. ```bash lavague-qa --context ./my_contexts/custom_context_gemini.py ``` -------------------------------- ### Default Initialization Code Source: https://docs.lavague.ai/en/latest/docs/core/base_driver Provides the default code for initializing the driver, including necessary imports, suitable for pasting at the start of an output script. ```python def default_init_code(self) -> Any ``` -------------------------------- ### Get Possible Interactions Source: https://docs.lavague.ai/en/latest/docs/core/base_driver Returns a dictionary of interactable elements, mapped by their XPath, with options to filter by viewport and foreground visibility. ```python def get_possible_interactions(self, in_viewport=True, foreground_only=True) -> Dict[str, Set[InteractionType]] ``` -------------------------------- ### Generate Pytest Command Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples A command-line instruction to execute BDD tests using the lavague-qa tool. It specifies the URL of the website to be tested (Wikipedia) and the path to the feature file containing the test scenarios. This command initiates the test run. ```bash lavague-qa --url https://en.wikipedia.org/ --feature lavague-qa/features/demo_wikipedia.feature ``` -------------------------------- ### Selenium WebDriver Exceptions (SessionNotCreatedException) Source: https://docs.lavague.ai/en/latest/docs/get-started/troubleshoot An example of a Selenium error indicating Chrome failed to start, often due to issues with the DevToolsActivePort, typically when using non-headless mode in incompatible environments. ```text selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally. (session not created: DevToolsActivePort file doesn't exist) (The process started from chrome location /home/user/.cache/selenium/chrome/linux64/126.0.6478.55/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) ``` -------------------------------- ### Accept Cookies (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Locates and clicks the 'Tout accepter' button to accept cookies, using WebDriverWait for element visibility and clickable status. Includes error handling for click interception. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ElementClickInterceptedException import pytest @when('the user clicks on "Tout accepter" to accept cookies') def accept_cookies(browser: WebDriver): accept_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div/div/div[3]/button[2]")) ) try: browser.execute_script("arguments[0].click();", accept_button) except ElementClickInterceptedException: pytest.fail("Failed to accept cookies") ``` -------------------------------- ### Navigate to Homepage (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/quick-tour Navigates the browser to the base URL of the website. This is typically the first step in any automated test to ensure a clean starting state. ```python from pytest_bdd import given, when, parsers from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time BASE_URL = 'http://localhost:8000' # Assuming BASE_URL is defined elsewhere or globally @given('the user is on the homepage') def user_on_homepage(browser): browser.get(BASE_URL) ``` -------------------------------- ### Initialize GeminiContext and Build Agent Source: https://docs.lavague.ai/en/latest/docs/integrations/gemini Demonstrates how to import and initialize the GeminiContext, then use it to build an ActionEngine and WorldModel. Finally, it shows how to create a WebAgent and run a query against a website. ```python from lavague.core import WorldModel, ActionEngine from lavague.core.agents import WebAgent from lavague.drivers.selenium import SeleniumDriver from lavague.contexts.gemini import GeminiContext # Initialize Context context = GeminiContext() selenium_driver = SeleniumDriver() # Build Action Engine and World Model from Context action_engine = ActionEngine.from_context(context=context, driver=selenium_driver) world_model = WorldModel.from_context(context) # Build agent & run query agent = WebAgent(world_model, action_engine) agent.get("https://huggingface.co/") agent.run("What is this week's top Space of the week?") ``` -------------------------------- ### Click Global Banking Link (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Locates and clicks the 'Global Banking and Markets' link using XPath. It utilizes WebDriverWait and handles potential ElementClickInterceptedException. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ElementClickInterceptedException import pytest @when('the user clicks on "Global Banking and Markets"') def click_global_banking(browser: WebDriver): global_banking_link = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/main/div[2]/div/div/div/div/div[4]/div/div/div/div/div/div/div[2]/div/a")) ) try: browser.execute_script("arguments[0].click();", global_banking_link) except ElementClickInterceptedException: pytest.fail("Failed to click on Global Banking and Markets") ``` -------------------------------- ### Accept All Cookies (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Locates and clicks the 'Accept all cookies' button using WebDriverWait for reliability. It also includes a try-except block to catch and report ElementClickInterceptedException. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ElementClickInterceptedException import pytest @when('the user clicks on "Accept all cookies"') def accept_all_cookies(browser: WebDriver): accept_all_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div/div[2]/button[2]")) ) try: browser.execute_script("arguments[0].click();", accept_all_button) except ElementClickInterceptedException: pytest.fail("Failed to accept all cookies") ``` -------------------------------- ### Dispatching Instructions with Action Engine (Python) Source: https://docs.lavague.ai/en/latest/docs/module-guides/action-engine Demonstrates how to initialize an Action Engine with a Selenium driver and dispatch a natural language instruction for execution. This involves setting up the driver, creating the Action Engine instance, and calling the `dispatch_instruction` method with the target engine name and instruction. ```python from lavague.drivers.selenium import SeleniumDriver from lavague.core import ActionEngine selenium_driver = SeleniumDriver(headless=False, url="https://huggingface.co/") action_engine = ActionEngine( driver=selenium_driver, ) # Dispatch an instruction to the Navigation Engine engine_name = "Navigation Engine" instruction = "Click on the Models button in the top menu" # Execute the instruction and get the output if applicable output = action_engine.dispatch_instruction(engine_name, instruction) ``` -------------------------------- ### Generate Pytest command for HSBC test (Bash) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Provides a command-line instruction to generate pytest tests for a specific feature file on the HSBC website. It includes flags for full LLM usage and specifying the target URL and feature file. ```bash lavague-qa --full-llm --url https://hsbc.fr/ --feature lavague-qa/features/demo_hsbc.feature ``` -------------------------------- ### Click Continue Button (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Finds and clicks the 'Je comprends, continuons' button, ensuring it's clickable before executing the action. Includes error handling for click interception. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ElementClickInterceptedException import pytest @when('the user clicks on "Je comprends, continuons"') def click_continue(browser: WebDriver): continue_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div[4]/div/div/div/a[2]")) ) try: browser.execute_script("arguments[0].click();", continue_button) except ElementClickInterceptedException: pytest.fail("Failed to click on Je comprends, continuons") ``` -------------------------------- ### Verify Successful Login (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Asserts that the login was successful by checking for a specific element (e.g., a user profile indicator) and verifying its text content matches the expected username. This confirms the redirection to the main page post-login. ```python from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @then('the login is successful and the user is redirected to the main page') def the_login_is_successful_and_the_user_is_redirected_to_the_main_page(browser: WebDriver): user_element = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.XPATH, "//li[@id='pt-userpage']//span")) ) assert user_element.text.strip() == "Lavague-test" ``` -------------------------------- ### Get Instruction from World Model - Python Source: https://docs.lavague.ai/en/latest/docs/module-guides/world-model Demonstrates the core functionality of calling the `get_instruction` method on the World Model instance with the prepared inputs. It then prints the output, which includes the next engine and instruction. ```python world_model_output = world_model.get_instruction( objective, current_state, past, obs ) print(world_model_output) ``` -------------------------------- ### HSBC Website Navigation Test (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Automates navigation and interaction with the HSBC website, including accepting cookies, clicking on specific banking services, and verifying redirection to an 'About us' page. It uses multiple steps to simulate user actions. ```gherkin Feature: HSBC navigation Scenario: Multi tab navigation Given the user is on the HSBC homepage When the user clicks on "Tout accepter" to accept cookies And the user clicks on "Global Banking and Markets" And the user clicks on "Je comprends, continuons" And the user navigates to the new tab opened And the user clicks on "Accept all cookies" And the user clicks on "About us" Then the user should be on the "About us" page of the "Global Banking and Markets" services of HSBC ``` -------------------------------- ### Python Pytest BDD Steps for Web Navigation and Cart Actions Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples This code defines pytest-bdd steps for interacting with a web application. It covers navigating to the homepage, accepting cookies, searching for products, adding items to a cart, and verifying cart status. It uses Selenium WebDriver with WebDriverWait for handling dynamic elements. ```python import pytest from pytest_bdd import scenarios, given, when, then, parsers from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import ElementClickInterceptedException import time # Constants BASE_URL = 'https://www.laposte.fr/' # Scenarios scenarios('demo_laposte.feature') # Fixtures @pytest.fixture def browser(): driver = webdriver.Chrome() driver.implicitly_wait(10) yield driver driver.quit() @given("the user is on the homepage") def user_on_homepage(browser): browser.get(BASE_URL) @when('the user clicks on "Accepter" to accept cookies') def accept_cookies(browser): accept_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable( (By.XPATH, "/html/body/div/span/form/div[2]/span/span/input") ) ) accept_button.click() @when( parsers.parse('the user enters "{search_term}" into the search bar and press Enter') ) def enter_search_term(browser, search_term): search_input = WebDriverWait(browser, 10).until( EC.presence_of_element_located( (By.XPATH, "/html/body/div/header/div/div/div[2]/div/form/div[3]/div/input") ) ) search_input.send_keys(search_term) search_input.submit() @when("the user clicks on the first product in the search results") def click_first_product(browser): first_product_link = WebDriverWait(browser, 10).until( EC.element_to_be_clickable( ( By.XPATH, "/html/body/div/div/div/div/div/span/div/div[2]/div/div/span/div/div/div[2]/div/h2/a", ) ) ) first_product_link.click() @when('the user clicks on the "Ajouter au panier" button') def add_to_cart(browser): add_to_cart_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable( ( By.XPATH, "/html/body/div[2]/div/div[4]/div/div[5]/div[4]/div[4]/div/div/div/div/div/div/div/div/div[2]/div/form/div/div/div[23]/div/span/span/span/input", ) ) ) add_to_cart_button.click() @when("the confirmation message is displayed") def confirm_message_displayed(browser): time.sleep(3) # Wait for the confirmation message to be displayed @when('the user clicks on "Aller au panier" under "Passer la commande"') def go_to_cart(browser): go_to_cart_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable( ( By.XPATH, "/html/body/div/div/div/div/div[2]/div/div[3]/div/div/span/span/a", ) ) ) go_to_cart_button.click() @when('the user clicks on "Supprimer" from the cart page') def remove_from_cart(browser): remove_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable( ( By.XPATH, "/html/body/div/div/div[3]/div[5]/div/div[2]/div/div/form/div[2]/div[3]/div[4]/div/div[2]/div/span[2]/span/input", ) ) ) remove_button.click() @then("the cart should be empty") def cart_should_be_empty(browser): time.sleep(3) # Wait for the cart update after removal empty_cart_message = WebDriverWait(browser, 10).until( EC.presence_of_element_located( (By.XPATH, "//h1[contains(text(), 'Your Amazon Cart is empty')]") ) ) assert ( "Your Amazon Cart is empty" in empty_cart_message.text ), "Cart is not empty" ``` -------------------------------- ### Lavague AI Website Navigation and Interaction (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Provides Python code using Behave for automating website interactions. It includes steps for navigating the homepage, accepting cookies, clicking specific elements, entering text into fields, and verifying displayed text. Dependencies include Selenium WebDriver and Behave. This code is specific to the Lavague AI website's structure and element locators. ```python import pytest from pytest_bdd import scenarios, given, when, then from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait, Select from selenium.webdriver.support import expected_conditions as EC # Constants BASE_URL = 'https://en.wikipedia.org/' # Scenarios scenarios('demo_wikipedia.feature') # Fixtures @pytest.fixture def browser(): driver = WebDriver() driver.implicitly_wait(10) yield driver driver.quit() ``` ```python @given('the user is on the homepage') def user_on_homepage(browser): browser.get(BASE_URL) ``` ```python @when('the user clicks on "J\'accepte" to accept cookies') def accept_cookies(browser): accept_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div/div[2]/div[2]/button")) ) accept_button.click() ``` ```python @when('the user clicks on "Envoyer un colis"') def click_envoyer_colis(browser): envoyer_colis_button = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/div/div[2]/div/main/div/div[2]/div[4]/div/div/a[3]")) ) envoyer_colis_button.click() ``` ```python @when('the user clicks on the "Format du colis" dropdown under "Dimension"') def click_format_dropdown(browser): format_dropdown = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/div/div/main/div/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div[2]/fieldset/div")) ) format_dropdown.click() ``` ```python @when('the user clicks on "Volumineux & tube" from the dropdown results') def select_volumineux_tube(browser): volumineux_tube_option = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/div/div/main/div/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div[2]/fieldset/div[2]/div/label[2]")) ) volumineux_tube_option.click() ``` ```python @when('the user enters 15 in the "Poids" field') def enter_weight(browser): weight_field = WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/div/div/main/div/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/div[2]/div/div/div/div/input")) ) weight_field.clear() weight_field.send_keys("15") ``` ```python @when('the user waits for the cost to update') def wait_for_cost_update(browser): time.sleep(3) ``` ```python @then('the cost should be "34,70 €"') def verify_cost(browser): cost_element = WebDriverWait(browser, 10).until( EC.presence_of_element_located((By.XPATH, "//span[@class='calculator__cta__price']")) ) assert cost_element.text == "34,70 €", f"Expected cost to be '34,70 €', but got '{cost_element.text}'" ``` -------------------------------- ### Preview Local Package Modifications Source: https://docs.lavague.ai/en/latest/docs/contributing/general This command installs a specific LaVague package in editable mode using pip. This is useful when you are making changes to a particular package, like `lavague.core`, and want those local modifications to be reflected immediately without reinstalling the entire project. ```bash pip install -e lavague-core ``` -------------------------------- ### Initialize Lavague AI Context Source: https://docs.lavague.ai/en/latest/docs/get-started/customization Initializes the AI context with specified language models (llm, mm_llm) and embedding model. This setup is crucial for the AI's operational capabilities. ```python context = Context(llm, mm_llm, embedding) ``` -------------------------------- ### Run Pytest BDD Scenarios (Python) Source: https://docs.lavague.ai/en/latest/docs/lavague-qa/examples Configures pytest to run Behavior-Driven Development (BDD) scenarios defined in a feature file. This snippet shows how to link feature files to the test execution. ```python from pytest_bdd import scenarios # Scenarios scenarios('demo_hsbc.feature') ``` -------------------------------- ### Custom LaVague context configuration Source: https://docs.lavague.ai/en/latest/docs/get-started/testing Example Python code for defining a custom LaVague context, including LLM, multi-modal LLM, embedding models, and a token counter. This file should be placed in the 'lavague-tests/contexts' folder. ```python from lavague.core.token_counter import TokenCounter from llama_index.llms.openai import OpenAI from llama_index.multi_modal_llms.openai import OpenAIMultiModal from llama_index.embeddings.openai import OpenAIEmbedding from lavague.core.context import Context llm_name = "gpt-4o-mini" mm_llm_name = "gpt-4o-mini" embedding_name = "text-embedding-3-large" token_counter = TokenCounter() # Initialize models llm = OpenAI(model=llm_name) mm_llm = OpenAIMultiModal(model=mm_llm_name) embedding = OpenAIEmbedding(model=embedding_name) # Initialize context context = Context(llm, mm_llm, embedding) ``` -------------------------------- ### Run a basic web agent with LaVague Source: https://docs.lavague.ai/en/latest/docs/get-started/quick-tour Creates and runs a basic web agent using SeleniumDriver, ActionEngine, and WorldModel from LaVague. Requires OPENAI_API_KEY to be set. ```python # Install necessary elements from lavague.drivers.selenium import SeleniumDriver from lavague.core import ActionEngine, WorldModel from lavague.core.agents import WebAgent # Set up our three key components: Driver, Action Engine, World Model driver = SeleniumDriver(headless=False) action_engine = ActionEngine(driver) world_model = WorldModel() # Create Web Agent agent = WebAgent(world_model, action_engine) # Set URL agent.get("https://huggingface.co/docs") # Run agent with a specific objective agent.run("Go on the quicktour of PEFT") ``` -------------------------------- ### Run LaVague tests with a built-in context Source: https://docs.lavague.ai/en/latest/docs/get-started/testing Executes LaVague tests using a pre-defined context configuration file, such as for Anthropic's Claude 3.5 Sonnet. Requires relevant API keys and lavague context packages to be installed. ```bash lavague-test-c lavague-tests/contexts/anthropic_context.py ```