Dear everyone.
I have created an app that adds datatable values when a button is clicked.
I want to test this app’s callbacks using dash[testing].
So I created a test like below.
#!/usr/bin/python
import dash
from dash import Dash, Input, Output, State, html, dash_table, callback
import time
from selenium.webdriver.common.by import By
def test_001(dash_duo):
app = dash.Dash(__name__)
app.layout = html.Div([
dash_table.DataTable(
id='table',
columns=(
[{'id': p, 'name': p} for p in ['Column']]
),
data=[
dict(Model=i, **{param: 1 for param in ['Column']})
for i in range(1, 3)
],
style_header={
"backgroundColor": "black",
"color": "white",
},
style_table={
'width':'10%',
},
editable=True
),
html.Button('Button', id='button', n_clicks=0),
html.Div(id="result")
])
dash_duo.start_server(app)
# Change value in table
datatable = dash_duo.wait_for_element("#table")
mean_parms = [1000, 2000]
for i, p in enumerate(mean_parms, start=2):
time.sleep(1)
dash_duo.driver.execute_script(f'arguments[0].innerHTML = {p};', datatable.find_element(By.XPATH, f'//*[@id="table"]/div[2]/div/div[2]/div[2]/table/tbody/tr[{i}]/td/div'))
time.sleep(1)
# Click button
dash_duo.wait_for_element('#button').click()
time.sleep(10)
@callback(
Output("result", "children"),
Input("button", "n_clicks"),
State("table", "data"),
prevent_initial_call=True,
)
def sum_table_value(n, table):
return int(table[0]['Column']) + int(table[1]['Column'])
State of the test.
If you look at the video, you can see that the result of the callback is wrong.(I want 3000, but it’s actually 2)
Also, when I click the datatable cell, the value returns to the initial value.
I’m thinking I probably didn’t enter the datatable values correctly, but I can’t solve this problem.
Do you know how to resolve this issue?