Browser Logic Function Structure
When you create a Browser Logic function from the Catalyst CLI, it is created with a boilerplate code structure as shown below. Catalyst also creates the required dependencies and configuration files for your function and your project. You can refer to the Project Directory Structure help page for details about the general configuration files.
Java
Note: This boilerplate code below will be present in your function file when you initialize a browser logic function in Java with Selenium
copyimport java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.simple.JSONObject; import org.openqa.selenium.chrome.ChromeDriver; import com.catalyst.browserlogic.SeleniumHandler; public class BrowserlessExample implements SeleniumHandler { private static final Logger LOGGER = Logger.getLogger(SeleniumExample.class.getName()); JSONObject responseData = new JSONObject(); @Override @SuppressWarnings("unchecked") public void runner(HttpServletRequest request, HttpServletResponse response,ChromeDriver driver) throws Exception { try { //Fetches the endpoint and method to which the call was made String url = request.getRequestURI(); String method = request.getMethod(); driver.get("https://www.example.com"); responseData.put("message", "Title of the page "+driver.getTitle()); //Sends the response back to the Client response.setContentType("application/json"); response.getWriter().write(responseData.toString()); response.setStatus(200); } catch (Exception e) { //The actions are logged. You can check the logs from Catalyst Logs. LOGGER.log(Level.SEVERE, "Exception in SeleniumExample", e); responseData.put("error", "Internal server error occurred. Please try again in some time."); response.getWriter().write(responseData.toString()); response.setStatus(500); } } }
NodeJS
Note: This boilerplate code below will be present in your function file when you initialize a browser logic function in NodeJS with Puppeteer
copymodule.exports.puppeteer = async (request, response, page) => { await page.goto('https://example.com/',{waitUntil: "domcontentloaded"}); const pageTitle = await page.title(); response.setHeader('Content-Type', 'application/json'); response.write(JSON.stringify({ output: pageTitle })); response.end(); };
Last Updated 2023-05-08 11:50:17 +0530 +0530
Yes
No
Send your feedback to us
Skip
Submit