[🐛 Bug]: SetValue() Does Not Work Under Any Loop
Introduction
In this article, we will discuss a bug that occurs when using the setValue()
method in a loop with WebdriverIO. The bug is that the setValue()
method does not work under any loop, resulting in an error.
Problem Description
The problem occurs when trying to set a value to an element using the setValue()
method inside a loop. The loop can be a for
loop, a forEach()
loop, or a map()
loop. The error message is "Request failed with status 400 due to invalid element state: invalid element state".
Relevant Code
The relevant code is as follows:
for (const filter of await SystemAdmin.filters) {
await filter.click()
await browser.pause(200)
await filter.setValue("test")
await browser.pause(5000)
}
This code is trying to set a value to each filter element in the SystemAdmin.filters
array.
Expected Behavior
The expected behavior is that the setValue()
method should work correctly and set the value to each filter element without any errors.
Actual Behavior
The actual behavior is that the setValue()
method does not work correctly and results in an error. The error message is "Request failed with status 400 due to invalid element state: invalid element state".
Log Output
The log output is as follows:
2025-03-12T01:12:15.721Z INFO webdriver: [POST] http://127.0.0.1:57658/session/23f28666a311cacc081404205944a069/element/f.6BAB5273049FA6BDDFC2C46E2F5BA0FE.d.B01AD7351753D7ECC12702D2EBB3596E.e.15420/clear
[0-2] 2025-03-12T01:12:15.745Z WARN webdriver: Request failed with status 400 due to invalid element state
[0-2] (Session info: chrome=136.0.7052.2)
[0-2] 2025-03-12T01:12:15.745Z INFO webdriver: Retrying 1/3
[0-2] 2025-03-12T01:12:15.745Z INFO webdriver: [POST] http://127.0.0.1:57658/session/23f28666a311cacc081404205944a069/element/f.6BAB5273049FA6BDDFC2C46E2F5BA0FE.d.B01AD7351753D7ECC12702D2EBB3596E.e.15420/clear
[0-2] 2025-03-12T01:12:15.779Z WARN webdriver: Request failed with status 400 due to invalid element state
[0-2] (Session info: chrome=136.0.7052.2)
[0-2] 2025-03-12T01:12:15.779Z INFO webdriver: Retrying 2/3
[0-2] 2025-03-12T01:12:15.779Z INFO webdriver: [POST] http://127.0.0.1:57658/session/23f28666a311cacc081404205944a069/element/f.6BAB5273049FA6BDDFC2C46E2F5BA0FE.d.B01AD7351753D7ECC12702D2EBB3596E.e.15420/clear
[0-2] 2025-03-12T01:12:15.816Z WARN webdriver: Request failed with status 400 due to invalid element state
[0-2] (Session info: chrome=136.0.7052.2)
[0-2] 2025-03-12T01:12:15.816Z INFO webdriver: Retrying 3/3
[0-2] 2025-03-12T01:12:15.816Z INFO webdriver: [POST] http://127.0.0.1:57658/session/23f28666a311cacc081404205944a069/element/f.6BAB5273049FA6BDDFC2C46E2F5BA0FE.d.B01AD7351753D7ECC12702D2EBB3596E.e.15420/clear
[0-2] 2025-03-12T01:12:15.874Z ERROR webdriver: Request failed with status 400 due to invalid element state: invalid element state
[0-2] (Session info: chrome=136.0.7052.2)
[0-2] 2025-03-12T01:12:15.878Z INFO webdriver: COMMAND takeScreenshot()
[0-2] 2025-03-12T01:12:15.878Z INFO webdriver: [GET] http://127.0.0.1:57658/session/23f28666a311cacc081404205944a069/screenshot
[0-2] 2025-03-12T01:12:15.975Z INFO webdriver: RESULT iVBORw0KGgoAAAANSUhEUgAABAoAAAL8CAIAAADm3cBFAAAQAElEQVR4nOydB...
[0-2] invalid element state in "System Admin.System Admin Users"
invalid element state: invalid element state
(Session info: chrome=136.0.7052.2)
at async Context.<anonymous> (file:///C:/Users/user/childFolder/test/specs/systemAdmin.spec.js:23:13)
Conclusion
In conclusion, the setValue()
method does not work correctly when used in a loop with WebdriverIO. The error message is "Request failed with status 400 due to invalid element state: invalid element state". This issue can be reproduced by running the code in the "Relevant Code" section.
Workaround
A workaround for this issue is to use the setValue()
method outside of the loop, as shown in the following code:
for (const filter of await SystemAdmin.filters) {
await filter.click()
await browser.pause(200)
}
await SystemAdmin.emailFilter.setValue('test')
await browser.pause(5000)
This code sets the value to the first filter element outside of the loop.
Future Development
In the future, it would be great to have a fix for this issue, so that the setValue()
method can be used correctly in a loop with WebdriverIO.
Related Issues
This issue is related to the following issues:
Code of Conduct
I agree to follow this project's Code of Conduct.
Is there an existing issue for this?
Q: What is the issue with the setValue() method in a loop?
A: The issue is that the setValue() method does not work correctly when used in a loop with WebdriverIO. The error message is "Request failed with status 400 due to invalid element state: invalid element state".
Q: What is the expected behavior of the setValue() method in a loop?
A: The expected behavior is that the setValue() method should work correctly and set the value to each element in the loop without any errors.
Q: What is the actual behavior of the setValue() method in a loop?
A: The actual behavior is that the setValue() method does not work correctly and results in an error. The error message is "Request failed with status 400 due to invalid element state: invalid element state".
Q: How can I reproduce this issue?
A: You can reproduce this issue by running the code in the "Relevant Code" section.
Q: What is the workaround for this issue?
A: A workaround for this issue is to use the setValue() method outside of the loop, as shown in the following code:
for (const filter of await SystemAdmin.filters) {
await filter.click()
await browser.pause(200)
}
await SystemAdmin.emailFilter.setValue('test')
await browser.pause(5000)
This code sets the value to the first filter element outside of the loop.
Q: Is this issue related to any other issues?
A: Yes, this issue is related to the following issues:
Q: Can this issue be fixed?
A: Yes, this issue can be fixed by improving the setValue() method to work correctly in a loop with WebdriverIO.
Q: What is the current status of this issue?
A: This issue is currently being investigated and a fix is being developed.
Q: How can I get updates on this issue?
A: You can get updates on this issue by following the issue on GitHub and subscribing to the project's newsletter.
Q: Can I contribute to the fix of this issue?
A: Yes, you can contribute to the fix of this issue by submitting a pull request with a fix for the issue.
Q: What is the timeline for fixing this issue?
A: The timeline for fixing this issue is currently unknown, but it is expected to be fixed in the near future.
Q: How can I get help with this issue?
A: You can get help with this issue by posting a question on the project's forum or by reaching out to the project's maintainers directly.