Add the following import:
import io.gatling.custom.browser.Predef._
Used to configure the Gatling browser protocol.
val browserProtocol: Protocol = gatlingBrowser
//// This part of setup block is optional
.withContextOptions(new NewContextOptions().setViewportSize(1920, 1080))
.withLaunchOptions(new LaunchOptions().setHeadless(false))
.enableUIMetrics()
////
.buildProtocol()
// with a static value
browserAction("HomePage")
// with a dynamic value computed from a Gatling Expression Language String
browserAction("#{actionName}")
// a dynamic value computed from a function
browserAction(session => session("actionName").as[String])
// with an absolute static url
browserAction("HomePage").open("https://docs.gatling.io/")
// with a dynamic value computed from a Gatling Expression Language String
browserAction("name").open("#{url}")
// a dynamic value computed from a function
browserAction("name").open(session => session("url").as[String])
It is possible to set additional navigation options or page load validation. See the NavigateOptions documentation here.
browserAction("name").open("https://docs.gatling.io/").withNavigateOptions(new Page.NavigateOptions().setWaitUntil(LOAD))
See the LoadValidations documentations here
val validationScript =
"""
(function () {
const el = document.querySelector('[data-test-id="chat-widget-iframe"]');
return document.readyState === "complete" &&
el !== null &&
el.getBoundingClientRect().width > 0 &&
el.getBoundingClientRect().height > 0;
})();
"""
val pageLoadValidator = PageLoadValidator(validationScript, null, new Page.WaitForFunctionOptions().setPollingInterval(100).setTimeout(30000))
browserAction("HomePage").open("https://gatling.io/").withLoadValidations(pageLoadValidator)
You can use it with default script that check pageCompleteCheckByInactivity similar that make sitespeed tool
browserAction("name").open("https://docs.gatling.io/").withLoadValidations())
Sometimes, you need to verify that a page has loaded programmatically. In such cases, you can use a script.
Note: You should always return browserSession.
browserAction("test").executeFlow((page,browserSession) => {
page.navigate("https://docs.gatling.io/")
page.locator("//*[@id=\"ai-initial-message\"]").isVisible()
browserSession
})
For advanced example see this guide
Allows manipulation of a page without tracking it in reports.
Note: Typically used to extract data and store it in Gatling sessions.
browserSessionFunction((page, browserSession) => {
val session = browserSession.getScalaSession().set("pageTitle",page.title())
browserSession.updateBrowserSession(session)
});
Provides a way to clean up the Playwright BrowserContext.
Note: Usually used at the end of a loop.
browserCleanContext()