import logging

import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from selenium_utils import SeleniumUtility

URL_TO_FETCH = "https://www.example.com"

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()
templates = Jinja2Templates(directory="templates")


@app.get("/", response_class=HTMLResponse)
async def page_information():
    selenium_util = SeleniumUtility()
    title = (
        selenium_util.get_page_title(URL_TO_FETCH) or "Failed to retrieve page title"
    )
    return templates.TemplateResponse("page_information.html", {"page_title": title})

About this template

Use this skeleton if you need to use Selenium to build the app. Example: if the user needs to write an end to end test or an automation test or a regression test or a functional test or crawl a website when it needs to do some sophisticated steps e.g. clicks, page element.

Introduction to the SeleniumApp Template

Welcome to the SeleniumApp template guide! This template is designed to help you build applications that require web automation, such as end-to-end tests, functional tests, or web crawling with sophisticated interactions like clicks and element retrieval. The SeleniumApp template uses Selenium for browser automation, FastAPI for creating a web server, and Jinja2 for templating HTML responses.

Getting Started

To begin using this template, simply click on "Start with this Template" in the Lazy Builder interface. This will pre-populate the code in your workspace, so you won't need to copy or paste any code manually.

Test: Deploying the App

Once you have the template ready, press the "Test" button to start the deployment process. The Lazy platform will handle the deployment, and you won't need to worry about installing libraries or setting up the environment.

Entering Input

After pressing the "Test" button, if the app requires any user input, the Lazy CLI will prompt you to provide it. For this template, there is no user input required through the CLI, so you can proceed to the next step.

Using the App

After deployment, Lazy will provide you with a dedicated server link to access your app. Since this template uses FastAPI, you will also receive a link to the FastAPI documentation, which can be useful for understanding how to interact with your API.

To view the page title fetched by Selenium, simply navigate to the server link provided by Lazy. You will see a simple HTML page rendered with the title of the page you fetched using Selenium.

Integrating the App

If you need to integrate this app into another service or frontend, you can use the server link provided by Lazy as the base URL for your API calls. For example, if you want to fetch the page title from a different frontend, you can make a GET request to the "/" endpoint of your server link.

Here's a sample code snippet that you could use in an external tool to make a request to your FastAPI server:


fetch('YOUR_LAZY_SERVER_LINK/')
  .then(response => response.text())
  .then(html => {
    // Process the HTML response here
    console.log(html);
  })
  .catch(error => console.error('Error fetching page title:', error));

Replace 'YOUR_LAZY_SERVER_LINK' with the actual server link provided by Lazy.

And that's it! You've successfully deployed and integrated an app using the SeleniumApp template on Lazy. Enjoy automating the web!

Category
Technology
Last published
July 26, 2024

More templates like this

AI Scraper Selenium App

Use this skeleton to use AI to crawl a website

Selenium

Selenium App

Use this skeleton if you need to use Selenium to build the app. Example: if the user needs to write an end to end test or an automation test or a regression test or a functional test or crawl a website when it needs to do some sophisticated steps e.g. clicks, page element.

Selenium

Selenium App

Use this skeleton if you need to use Selenium to build the app. Example: if the user needs to write an end to end test or an automation test or a regression test or a functional test or crawl a website when it needs to do some sophisticated steps e.g. clicks, page element.

Selenium
Home
/
Selenium App