How to set attribute of a Selector Object
I am looking for a way to set attribute for a Selector without using a selector string allow me to demonstrate with an example
- go to www.google.com
- let selectorString = 'div[class="FPdoLc tfB0Bf"] input[aria-label="Google Search"]'
- do this in the code:
t.eval(() => {
(document.querySelector(String(selectorString)) as any).value = "Hello World"},
{ boundTestRun: this.browser, dependencies: { selector: String(selector) } })
})
ignore the boundTestRun as thats just something specific to our framework that sets the context for the evaluation.
4) if you do the above the google search button will change to "Hello World"
What I want is the ability to do this with an already defined Selector object. In many cases we might not have the selector string used to define the Selector object and in these cases, we are unable to set attributes like in the example above.
For example if I define a Selector and then call .children() on it. there is no way to derive a selector string from the child Selectors since they are defined via a functional style selector.
One thing that could be done is adding a setAttribute method to the selector api to allow us to modify a given Selector's attribute in the DOM.
Hi @switch201
TestCafe API doesn't allow you to modify the HTML element via Selector API.
Use ClientFunction for that:
import { ClientFunction } from 'testcafe';
fixture `Fixture`
.page('https://devexpress.github.io/testcafe/example/');
const setAttribute = ClientFunction(selector => {
var element = document.querySelector(selector);
element.setAttribute('disabled', 'disabled');
});
test('test', async t => {
await setAttribute('#developer-name');
await t.wait(5000);
});
Hi @miherlosev
I already saw that solution on another forum or stack post, but the issue with it is that in your case above the selector is a locator string. In some cases, for example when dealing with the return value .children(), you don't have a locator string that represents the Selector at hand, since the Selector was derived programmatically via a function call rather than with a string. My question is asking if there is a way to modify the element without using a selector string.
Is there a way to do this with ClientFunction? or is this simply not possible.
Thank you for your time.
https://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client/#overwriting-options
import { ClientFunction, Selector } from 'testcafe';
fixture `Fixture`
.page('https://devexpress.github.io/testcafe/example/');
const setAttribute = ClientFunction(() => {
debugger;
targetElement().setAttribute('value', 'value');
}, {
dependencies: { targetElement: null }
});
test('test', async t => {
const element = Selector('#developer-name');
await setAttribute.with({
dependencies: { targetElement: element }
})();
await t.wait(5000);
});
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.