I am working on nighwatch.js for web ui testing, I want to set value to a textarea, and textarea has an attribute which has my actual text, I am writing full textarea and how I am setting in following.
<div class="textarea-description">
<textarea cols="50" class="myClass setText" data-def placeholder="text to be replaced using nightwatch"/>
</div>
I am trying to set value in above textarea's attribute data-def placeholder as following ways
browser.setValue('.textarea-description textarea[type=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[data-def placeholder=text]','nightwatch'); or
browser.setValue('.textarea-description textarea[type=data-def placeholder]','nightwatch');
but nothing is working.
This might not be the best solution but it works:
browser
.execute("document.querySelector('.textarea-description .myClass').setAttribute('placeholder', 'nightwatch');")
If you have jQuery you can make it a bit nicer:
browser
.execute("$('.textarea-description .myClass').attr('placeholder', 'nightwatch');")
Thank you for your all valuable suggestions, all suggestions provided by you was able to give good knowledge but unfortunately none of the suggestion worked. I have resolved it by using following.
client.setValue('.textarea-description textarea','new text to be write.');
Actually attribute "data-def placeholder" was using only watermark that was not actual text, so it is working.
You could use xpath to get the attribute.
.useXpath().setValue('//textarea[contains(#placeholder,'text to be replaced using nightwatch')]#placeholder', 'nightwatch')
How to select specified node within Xpath node sets by index with Selenium?
This worked for me.
.assert.visible('div.textarea-description textarea.setText')
.moveToElement('div.textarea-description textarea.setText', null, null)
.mouseButtonClick('left')
.keys([browser.Keys.CONTROL, "a"])
.keys([browser.Keys.CONTROL, "nightwatch"])
Related
I want javascript that will automatically populate the blinking cursor in a text box on a new page, instead of having people click on the text box to type their response. I have been searching multiple forums but cannot find an answer.
Thanks!
Find the element in the DOM and call the focus method.
Something like this:
var x = document.getElementsByTagName("textarea");
if(x.length > 0)
x[0].focus();
Find working fiddle here: https://jsfiddle.net/pwse652d/
This is very simple and stupid implementation just to explain how it could be done. You might like to find element by using class selector and if using JQuery or something of like, little different then.
You don't need JavaScript to do that, although you can use it if you really want.
Just add the autofocus attribute to the textarea.
<textarea name="message" autofocus></textarea>
Alternatively, you can use JavaScript.
document.querySelector('textarea[name="message"]').focus();
This is my 2nd day into learning Selenium. I would like to extract text between these html tags called .
HTML Code Sample:
<div id="media-buttons" class="hide-if-no-js"/>
<textarea id="DescpRaw" class="ckeditor" name="DescpRaw" rows="13" cols="100" style="visibility: hidden; display: none;">
Cactus spines are produced from specialized structures
called areoles, a kind of highly reduced branch. Areoles
are an identifying feature of cacti.
</textarea>
</div>
Required results:
Cactus spines are produced from specialized structures
called areoles, a kind of highly reduced branch. Areoles
are an identifying feature of cacti.
I have tried with Selenium driver below, but it comes out empty.
String bodyhtml = driver.findElement(By.xpath("//textarea[#name='DescpRaw']")).getText();
Thank you!
String bodyhtml = driver.findElement(By.xpath("//textarea[#name='DescpRaw']")).getAttribute("innerHTML");
also I recommend using ID since it is available and it is faster.
String bodyhtml = driver.findElement(By.id("DescpRaw")).getAttribute("innerHTML");
A couple things...
If an element has an ID, you should always prefer to use the ID. By HTML standards, it should be unique on the page so it is the ideal identifier for any element.
The problem you are running into is that the TEXTAREA is hidden. You can tell this given style="visibility: hidden; display: none;" on the element. Selenium is designed to interact with the webpage as a user would. Any element that isn't visible, Selenium won't interact with. The ideal situation would be to figure out how to expose or make visible the textarea field... click some button/link/whatever and then get the text from it. With a TEXTAREA field you will likely need to .getAttribute("value") on the element.
A couple alternatives to making the element visible are to use Javascript to grab the element text or to use .getAttribute("innerHTML") as others have suggested.
I'm trying to get the value of a textarea with Shopify Product Options by Bold and it is currently not working. I am able to get the value of a textarea locally, but I can not get the value when I move the code over to Shopify. I have looked here and here to no avail.
Here's the relevant code:
document.getElementById("248832").onkeyup=function(){getVal()};
var textbox = document.getElementsByName("properties[Message Body]")[0].value;
and here's the textarea I'm trying to get the value of
<textarea data-option-key="ta_248832" id="248832" class="bold_option_child shapp_full_width " name="properties[Message Body]"></textarea>
When I try to run this on Shopify, I get an error saying "Uncaught TypeError: Cannot set property 'onkeyup' of null", although I did notice that at one point shopify runs the following jQuery code, which might be what's causing my problem:
<script>
jQuery('#248832').change(function (){conditional_rules(7437760391);})
</script>
I am trying to get the value of the textarea so I can run get the amount of words in said textarea.
Any help would be greatly appreciated!
Look to see if the element that is returned as null has loaded yet. Others in similar situations fixed this by loading the script last. Hope this is helpful :)
https://stackoverflow.com/a/25018299/1305878
https://stackoverflow.com/a/31333349/1305878
https://stackoverflow.com/a/23544789/1305878
Solution
You are trying to use something similar to jQuery methods without jQuery:
document.getElementById("248832").onkeyup=function(){getVal()};
while DOM elements don't have the onkeyup method. It should be
jQuery("#248832").on("keyup",function(){getVal()});
Extra notes
even without using the recommended '.on' method, you have to write it as
jQuery("#248832").keyup(function(){getVal()});
(docs), not as
jQuery("#248832").onkeyup(function(){getVal()});
If you'd like to use DOM methods, that would be
document.getElementById("248832").addEventListener("keyup",function(){getVal()});
Also, may be you have wrote this as a minimal example, but the fact that you only call getVal() and don't pass the value somewhere sounds strange, although I don't know if what getVal() does exactly.
I am using this plugin http://code.google.com/p/lwrte/, but I can not select the textarea or the id with jquery, I know it creates an iframe, but I read the docs and It does not mention anything about this issue, I just want to count the characters in the textarea and then that the user can not type, but I dont find a solution for this, has someone has a solution? what can I do?
<textarea id="message" rows="10" cols="120" class="rte1"></textarea>
$('#message').keyup(function(){ //tried with this does not work
});
any more help??
LWRTE takes textAreas and turns them into <iframe>s. So you need to use something like Google Chrome or Firebug to identify the new name of the object, then reference that directly. Something like this:
$('body iframe').contents().find('body').html()
Replace $('message') with $('#message') to get the actual element.
i am using tinyEditor as my text editor, but i donno how to get a tinyEditor textarea's value in javascript.
when i user document.getElementById('texteditor').value; it gives me null. nothing.
anyone know how to fix this problem?
this is the site for tinyEditor
Try this (editor is the instance):
editor.post();
var textAreaHtml = editor.t.value;
Well, the standard way is:
tinyMCE.get('element-ID').getContent();
which element-ID is the id of the textarea used for tinyMCE.
editor.i.contentWindow.document.body.innerHTML
this will return value of editor, but this is hack, this is not valid access, as I can see method for getting content doesn't exists.
so "editor" is name of your editor.