admin管理员组文章数量:1428551
What is the best way to get a DOM element in a Taiko test?
In the browser console I can do:
element = document.getElementsByClassName("XXXX")[0]
I've tried element = await $('.XXXX').get()[0];
using Taiko's $
selector ($). But that doesn't seem to give the actual DOM element (just a Taiko ElementWrapper
()).
For context, the reason I want the DOM element is because I'd like to do element.parentElement
and then look at some of the properties to use in my test.
I'm relatively new to Taiko so any help is greatly appreciated.
Thanks
What is the best way to get a DOM element in a Taiko test?
In the browser console I can do:
element = document.getElementsByClassName("XXXX")[0]
I've tried element = await $('.XXXX').get()[0];
using Taiko's $
selector (https://taiko-preview.gauge/#$). But that doesn't seem to give the actual DOM element (just a Taiko ElementWrapper
(https://taiko-preview.gauge/#elementwrapper)).
For context, the reason I want the DOM element is because I'd like to do element.parentElement
and then look at some of the properties to use in my test.
I'm relatively new to Taiko so any help is greatly appreciated.
Thanks
Share Improve this question asked Feb 22, 2019 at 23:17 RyanRyan 511 silver badge3 bronze badges2 Answers
Reset to default 3You can use the evaluate method in Taiko here. For example, to get the classname from the parent element you could do
evaluate(()=>{var a = document.getElementsByClassName('XXXX')[0]; return a.parentElement.className})
You can use evaluate method to get DOM element. You can return the attribute values from this method or set the value of them. Here is the example,
// For getting an attribute value
const color = await evaluate(textBox({ class: 'username' }), (element) => {
return element.style.color;
});
// For setting/getting an attribute
const updatedMaxLength = await evaluate(textBox({ class: 'username' }), (element) => {
element.setAttribute('maxlength', 20);
return element.getAttribute('maxlength');
});
In your case it should be,
const inputMaxLength = await evaluate(textBox({ class: 'username' }), (element) => {
return element.parentElement.className
});
You can also use XPath of the element to use with evaluate method,
const passwordXpath = `//input[@type="password"]`;
const passwordMaxLength = await evaluate($(passwordXpath), (element) => {
return element.getAttribute('maxlength');
});
本文标签: javascriptTaiko Automated Testsget DOM elementStack Overflow
版权声明:本文标题:javascript - Taiko Automated Tests - get DOM element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745527192a2661891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论