How to dynamically add a script to the page using watir-webdriver - watir-webdriver

I have some *.js script that I want to execute using watir-webdriver during my test runs. So the question is there any way to upload the script to the page using watir-webdriver?

You could use the execute_script method to add a script element with the src attribute as your js file. The method call would look like:
browser.execute_script(
"var the_script = document.createElement('script');
the_script.setAttribute('src','your_script.js');
document.head.appendChild(the_script);"
)
To see that it works, let us assume you have the page:
<html>
<body>
<input type="text" id="field" value="100">
</body>
</html>
If you try to execute jQuery on the page (the '$' in the script), an exception occurs because the page does not know what jQuery is:
field = browser.text_field
p browser.execute_script('return $(arguments[0]).val();', field)
#=> $ is not defined (Selenium::WebDriver::Error::JavascriptError)
If you add the jQuery script file (http://code.jquery.com/jquery-1.10.2.js), via execute_script, you will now be able to use jQuery:
browser.execute_script(
"var the_script = document.createElement('script');
the_script.setAttribute('src','http://code.jquery.com/jquery-1.10.2.js');
document.head.appendChild(the_script);"
)
field = browser.text_field
p browser.execute_script('return $(arguments[0]).val();', field)
#=> "100"

Related

Flask - boolean form

i'm running locally a python file. when i access 127.0.01:5000/string i get to a specific html page. Up to now, using javascript, i managed to put some checkbox (boolean form) on that page, but how can i assign the value of each one of them (True or False) to a variable in the python file?
i'm being unable to use the user's response in anyway.
i'm using flask-ext-wtforms, render_template, etc.
this is what i have on the html file so far.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var creature=0
var artifact=0
function suggest(){
if ($('#Creature').is(':checked')){creature=1;}
if ($('#Artifact').is(':checked')){artifact=1;}
}
</script>
<input type="checkbox" id = "Creature">Creature<br>
<input type="checkbox" id = "Artifact">Artifact<br>
<input type="checkbox" id = "Enchantment"> Enchantment<br>
<input type="checkbox" id = "Sorcery"> Sorcery<br>
<button type="button" onclick = "suggest(); alert('creature ' + creature + ' artifact ' + artifact)">Submit</button>
It tells me whether or not the user has clicked one of the first two boxes, but that's it. i don't know how to make the python file access such information.
It'd be useful to see what you have in the actual Python file, but I'll give it a go. Also I'll state right off the bat that for these sorts of questions the documentation should be your best friend and first go-to, but here's a start.
From the html you've posted it doesn't look like you're actually using a Flask-WTF Form instance. You would want to first create a Form with BooleanFields like so:
from flask.ext.wtf import Form
from wtforms import BooleanField
class MyForm(Form):
creature = BooleanField()
# etc
submit = SubmitField()
then in your template render the form & the fields like so:
<form method="POST" action="/string">
{{ form.creature.label }}
{{ form.creature() }}
{# ... etc ... #}
{{ form.submit() }}
</form>
Then finally in your view, you have to (A) specify that the view accepts POST requests, (B) create the form, and (C) make sure you pass the form to the template for rendering. If you do all these things, then when you, or someone else, or a robot or whatever click submit, then the browser will POST the data from the form to the flask view, and you will be able to access it in the view as, say, form.creature.data. Example:
#route('/string', methods=['GET', 'POST']) # part A
def get_critters():
form = MyForm() # part B
if form.validate_on_submit():
# do something with form.creature, or form.whatever
return render_template("string.html", form=form) # part C
All of this is very ably covered in multiple parts of each project's documentation. See:
https://flask-wtf.readthedocs.org/en/latest/quickstart.html
https://wtforms.readthedocs.org/en/latest/fields.html#wtforms.fields.BooleanField
and virtually everything at http://flask.pocoo.org/docs/0.10/ but especially http://flask.pocoo.org/docs/0.10/patterns/wtforms/ and http://flask.pocoo.org/docs/0.10/tutorial/.

custom tag in HTML and parser with php

i have a problem and need your help.
i want use my custom tag in my script code like [tag] and analyze all html code then parser codes and Replace these tags with php code or my output world and echo my output
a simple code :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>[MY_PAGE_TITLE]</title>
</head>
<header></header>
<div class="left">[MY_WEBSITE_LEFT_SIDEBAR_INFORMATION]</div>
<article>[MY_WEBSITE_ARTICLE]</article>
<div class="right">[MY_WEBSITE_RIGHT_SIDEBAR_INFORMATION]</div>
<footer></footer>
<body>
</body>
</html>
my first tag [MY_PAGE_TITLE] should be replaced with one world like "my website"
i want use this for my language website.
i get this word from a array like :
mylangarray [
MY_PAGE_TITLE="my website"
]
but for my any other tag i want load some module to left or right sidebar or load some article from my DB
How can I do this?
i solved my problem.
if you have this problem, you can use this method.
step1: you define your custom tag in your main view file and then in the server side with PHP ob_start function run output buffer then require or require_once or include or include_once your file.
now with define a variable and ob_get_contents function in PHP, store all file code in a variable. like this:
$html = ob_get_contents();
now you have all view file in a variable.
you can clean your buffer with ob_end_clean function.
example:
ob_start();
require_once 'mainViewFile.php';
$html = ob_get_contents();
ob_end_clean();
step2: you can use str_replace funcation for replace your defined custom tag with your new data.
$leftSideBarInformation = 'We stored left bar information in this variable!';
$html = str_replace('[MY_WEBSITE_LEFT_SIDEBAR_INFORMATION]', $leftSideBarInformation, $html);
if you have array of custom tag you can use a foreach loop.

JSP javascript function onclick to different jsp page

I have a form and javascript function to open 2.jsp page but it keeps giving me
Message: Object doesn't support this property or method
Here is how I do it. I tried to put in only the important parts.
<form name = iForm action=1.jsp method=POST>
<input type="button" value="Button2" name=button2 onclick="OnButton1()"/>
</form>
<script language="JavaScript">
function OnButton1{
document.iFrom.action = "2.jsp";
document.iFrom.target = "_blank";
document.iFrom.submit();
return true;
}
< /script>
I know a way to open through 'window' but I was wondering if there is this way
You did not put parenthesis after your JavaScript function name and you misspelled the form name in your script.
Correct those mistakes and it should work as you might expect.
As well, the target attribute does work for forms.

how to pass html form data to variable (nodejs / javascript)

so i'm using nodejs and nodejsdb-mysql. i want to make form for adding and searching posts.
I make html page with form, transform it to node using jsdom but how do i set up variables?
for example my html form is like:
name = text input
surname = text input
submit
and how do i pass inserted name/surname to var??
You will need to turn your submit into a button with an event such as onclick - this might be what you are looking for:
<input type="button" value="Submit" id="submit" name="submit" onclick="submit()">
<script type="text/javascript">
function submit(){
var name = document.getElementById('**name**').value;
var surname = document.getElementById('**surname**').value;
alert("Thanks for submitting, " +name);
}
</script>
With nodejsdb-mysql sooner or later you will face real problems like date localization problems and so on, use https://github.com/felixge/node-mysql instead

How to integrate CKFinder with CKEditor?

How do you integrate CKFinder with the new CKEditor.
It is very underdocumented on the website, and i am literally getting nowhere.
A step by step guide would be greatly appreciate as, as far as i am aware.. this is the only free/good image upload solution for a wysiwyg editor that is any good. Can someone confirm?
Thanks
You can find a tutorial on integrating CKFinder with CKEditor here:
http://www.webshaolin.com/index.php?page=article&articleid=40
Try doing following steps.
1. Download CKEditor and CKFinder. Integrated code may be available on http://dwij.co.in/ckeditor-ckfinder-integration-using-php/
2. Put extracted code of both in one folder inside xampp as below.
3. Create index file (index.html) which will be containing the editor as below code.
<html>
<head>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder.js"></script>
</head>
<body>
<h1>CKEditor CKFinder Integration using PHP</h1>
<textarea id="editor1" name="editor1" rows="10" cols="80"></textarea>
<script type="text/javascript">
var editor = CKEDITOR.replace( 'editor1', {
filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?type=Images',
filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?type=Flash',
filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
});
CKFinder.setupCKEditor( editor, '../' );
</script>
</body>
</html>
so your folder structure will be something like this:
htdocs
|_integrated
|_ckeditor
| |_config.js
| |_...
|_ckfinder
| |_config.php
| |_...
|_uploads
|_index.html
Now open file config.php inside ckfinder & make following changes:
function CheckAuthentication() {
// WARNING : DO NOT simply return "true". By doing so, you are allowing
// "anyone" to upload and list the files in your server. You must implement
// some kind of session validation here. Even something very simple as...
// return isset($_SESSION['IsAuthorized']) && $_SESSION['IsAuthorized'];
return true; // not good option though; go for sessions
}
$baseUrl = 'http://localhost/integrated/uploads/';
$enabled = true;
$config['SecureImageUploads'] = false;
$config['ChmodFolders'] = 0777 ;
Now open url http://localhost/integrated/ and try uploading image.
Check the documentation site for your server language: http://docs.cksource.com/CKFinder_2.x
For example this part of the PHP docs: http://docs.cksource.com/CKFinder_2.x/Developers_Guide/PHP/CKEditor_Integration
And btw, CKFinder is not free, you must get a license in order to use it.
First you must have a textbox to convert to CKEditor:
<textarea id="newTextArea">Some text</textarea>
Then all you need is some javascript code for the conversion of this texteditor to a CKEditor instance and the integration of this editor with CKFinder.
<script type="text/javascript">
var newCKEdit = CKEDITOR.replace('newTextArea');
CKFinder.setupCKEditor(newCKEdit, '/ckfinder/');
</script>
The second parameter of the setupCKEditor function must be the folder in your website where you uploaded ckfinder.
http://docs.cksource.com/CKFinder_2.x/Developers_Guide/PHP/CKEditor_Integration