I am trying to input the content of a variable generated with the Automator app onto the next action step, to define the value of a javascript script variable. Change the variable using javascript code and push the result further to the next action step. Then turn it back into an Automator variable, or update the one previously used as input. I am sure you will find it as a simple thing, but I couldn't find a way to get it working. This question might be silly, after all. Many thanks in advance. Check the screenshot of the Automator sequence of steps. Screenshot of the actions set in Automator App
function run(input) {
var str = input;
var l1 = str.replace(/ /g, "_");
str = l1;
var l2 = str.replace(/:/g, "-");
return l2;
}
The input to an Automator action is an array, so you will need to step through the array or convert/coerce to whatever class you are wanting to work with. In your posted script, the replace() method is for a string, so you are getting an error trying to use that method on the input array.
If you just want a single input item you can use input[0] to get the first item in the array, or input.join() to join the array items together.
Related
I am currently working on a project where I need to integrate the use of the Selenium Webdriver. I am using the Chrome implementation of Web Driver and running it via Javascript. I am currently testing a simple quantity input form. I am having trouble with a particular aspect of this project and that is ... I need the test to run through the form and put in different values everytime. I am placing the values via the sendKeys function. Now the trouble starts here... I need to grab the value that the sendKeys function inputs into the field and console.log a message depending on the value.
If the value is over a 100 I need the test to console.log the message "Exceeds 100".
If the value is less than 0 I need it to console.log the message "Below 0".
And if there is no value I need it to console.log the message "No input".
It runs through and puts in new values just fine. But the issue has been grabbing the value and console.logging a message depending on the value. I've tried many different options but there's just so little documentation related to this exact topic. I will link my code below, and I appreciate any input you guys may have... because it has me stumped unfortunately.
Also I am curious if this can be done using assertions in any way...
Test File Below:
https://gist.github.com/anonymous/89a84dbc15ba4088719400be1f359045
There is a method getAttribute(String attrName) it will accept a string parameter, pass attribute name against which value got set.
for example:
WebElement element =driver.findElement("your unique element locator");
String valueText=element.getAttribute("value");
about the answer above me - you should try adding a .getText(), So the attribute value would become a String.
WebElement element = driver.findElement("your unique element locator");
String valueText = element.getAttribute("value").getText();
Please add the full error message, A screenshot of the console would be good.
My problem involves using JavaScript in a gmail sheet to search emails using getRawContent.
I've been using indexOf to get a particular data from the string, I'm able to find the starting point for the data I want to use.
For example var startxmailer = rawmail.indexOf("X-Mailer: ",0);, but I'm unable to identify or locate end of line/carriage return to give me an end point to use sub-string to get the data for example var allxmailer = rawmail.substring(endxmailer,test1);.
I've tried indexOf(/[\n,\r]/,0) and (/\r,\n,|\r|,\n,\$,\O,\x/).
I've put the section of text into a variable in the script and used the above to locate end of line/carriage return but not been to successful any assistance would be much appreciated as I would like to be able to different section of data from a email in Raw Content.
Try this:
var xmailer = (rawmail.match(/X-Mailer: (.*)/i) || [])[1];
This will get the content of the X-Mailer header into the xmailer variable, or undefined if no such header was found. This works because . explicitly matches everything that isn't a new line.
So, I'm running casperjs through Windows' command line and I'm trying to search for a name in a table and get the row to which it belongs.
For example: If I'm searching for the name "Pete" and it's on the 4th line, it should return 4.
As the name is the fist element of each row in the table I was thinking about getting the page's source code and parsing everything beetween and so I could get the name and compare it to what I have, if it's not equal I would just do a i++ and go on to the next row (i being the row's number).
I couldn't find any way to get the source code, if I could I would certainly be able to parse it.
Using pseudo-code it should be something like this:
function searchForName(desiredName) {
var i = 1;
while(desiredName != nameIFoundOnTheColumn(i)) {
i++;
}
return i;
}
So, how can I get the source using JavaScript on a headless browser like PhantomJS?
I just used a simple function called this.getHTML() on Casper which I haven't seen before on Casper's documentation. Then I parsed the desired tags using another function to get what's between two tags.
I have 10 small text field boxes in HTML where I want users to enter numbers 1 to 10 - and the same number can not be entered twice in another box.
I'm struggling to work out how to accomplish this in Javascript/jQuery.
Any help would be appreciated.
Simply create an array with the values and check for duplication.
var formdata=new Array();
formdata[0]=document.getElementByid('id').value;
formdata[1]=document.getElementByid('id').value;
formdata[2]=document.getElementByid('id').value;
Now check for duplications in the array using the following - I guess the following question would be useful on stack overflow:
Easiest way to find duplicate values in a JavaScript array.
If the breakpoint is one simply display an alert() to the user to renter the data.
Instead of using documents.getElementById() you can also use
oForm = document.forms[index];
oText = oForm.elements["text_element_name"].value; OR
oText = oForm.elements[index].value;
More information can be found at:
http://www.javascript-coder.com/javascript-form/javascript-get-form.phtml
From an open code I have this line
var average = parseFloat($(this).attr('id').split('_')[0]),
It gets the first part of a div id with '_' as delimiter. The problem is that an id cannot start with a number (naming violation convention). So I am going to add a letter before the id value in my php script. How do I insert substr(1) to this var to remove this letter and get 'average' as expected?
Assuming you're talking about this format for an id:
<div id="A1000_bc"></div>
You can insert the substr(1) like this:
var average = parseFloat(this.id.split('_')[0].substr(1));
I might prefer to do it like this so it's a little less presumptious about the exact format and just grabs the first floating point numeric sequence:
var average = parseFloat(this.id.match(/[\d\.\+\-]+/)[0]);
Also, notice how I removed the jQuery. $(this).attr("id") performs a lot worse than this.id and offers no advantages here. jQuery should be used only when it's actually better than plain JS.
Both of these methods assume you are only going to present the code with properly formatted ids. If you want to handle a default condition when the id is not in the right format, then you will need multiple lines of code with some if conditions to check for validity and offer a default result when not valid.
Both options work here: http://jsfiddle.net/jfriend00/B4Rga/
Incidentally, if you control the HTML here, then there are better places to put data like this than in an id. I'd suggest a custom data attribute (HTML5 standard, but works everywhere).
<div id="whatever" data-avg="3.5"></div>
Then, you can get the data like this without having to parse it:
var average = parseFloat(this.getAttribute("data-avg"));
var average = parseFloat(
$(this) // you've got a jQuery object here - bad place
.attr('id') // you've got a string here - why not
.split('_') // you've got an array here - bad idea
[0] // you've got a string here - why not
// you need to have a number string here
);
Remeber: substr(1) can only be called on Strings.