Tests for CustomJS Callbacks

Hello, I have created a minimal reproducible example of my bokeh app. All this example does is when the user double taps on the plot, it moves the span to the location of the double tap. I want to write a test to test that when the user double taps, the span actually moves to that location.

I initially used python callbacks and those were easy to test but as the application grew I had to change the callbacks to CustomJS for speed.

I am having trouble testing the customJS callbacks. When using selenium, I struggle to find the span because the HTML only shows a canvas element and I cant seem to figure out how to access it and test the location.

I have also tried p._trigger_event() and passed the doubletap event with position but this did not seem to actually do anything. The span location did not get updated and the assert statement failed.

I’d appreciate any tips on the best way to test a CustomJS callback. I am using bokeh 3.3.4.


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
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.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

from bokeh.plotting import figure, output_file, show
from bokeh.models import Span, CustomJS
from bokeh.events import DoubleTap
import time

span = Span(location=1, line_color="green", line_width=2, dimension="height")

# callback to move span to position of double tap
callback = CustomJS(args=dict(span=span), code="""
    span.location = cb_obj.x

    """)

p = figure(
    x_range = (0,10),
    y_range = (0,1)
)
p.add_layout(span)
p.js_on_event(DoubleTap, callback)

output_file("test_bokeh_app.html")

show(p)

This might not be exactly what you’re going for but might help:

AFAIK, you can add a “name” to every bokeh model you instantiate, so say for your span:

span = Span(location=1, line_color="green", line_width=2, dimension="height",name='my_special_span')

You can then access this specific instance on the JS side by going:

Bokeh.documents[0].get_model_by_name('my_special_span')

in the javascript console on your web browser:

So if you can use selenium to execute some javascript ( and I think you can → How to use JavascriptExecutor in Selenium | BrowserStack ), you could then get the properties of the span in the current html/browser instance (like its location) in a python test.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.