SlideShare a Scribd company logo
JavaScript
Basic Examples
-Vikas Thange
(vikasthange@gmail.com)
www.vikasthange.blogspot.com
Topics to be covered
1. Basic JavaScript Examples
2. JavaScript Comments
3. JavaScript Variables
4. JavaScript Conditional If ... Else
5. JavaScript Popup Boxes
6. JavaScript Functions
7. JavaScript Loops
8. JavaScript Events
9. JavaScript Error Handling
1. Basic JS Examples
• Write to the Document with JavaScript
document.write("<p>My First JavaScript</p>");
• Change HTML elements with JavaScript
<p id="demo">My First Paragraph.</p>
document.getElementById("demo").innerHTML="My First
JavaScript";
• An external JavaScript
<script src="myScript.js">
2. JavaScript Comments
• Comments
Single line comment - //
Multiline comments /*.......*/
3. JavaScript Variables
• Declare a variable, assign a value to it, and display it
• Code:
var firstname;
firstname="Hege";
document.write(firstname);
document.write("<br>");
firstname="Tove";
document.write(firstname);
4. JavaScript Conditional If ... Else
• If statement
• If...else statement
• Random link
• Switch statement
4.1 If Statement
function myFunction()
{
var x="";
var time=new Date().getHours();
if (time<20)
{
x="Good day";
}
document.getElementById("demo").innerHTML=x;
}
4.2 If....else Statement
function myFunction()
{
var x="";
var time=new Date().getHours();
if (time<20)
{
x="Good day";
}
else
{
x="Good evening";
}
document.getElementById("demo").innerHTML=x;
}
4.3 Random Link
var r=Math.random();
var x=document.getElementById("demo")
if (r>0.5)
{
x.innerHTML="<a href='http://w3schools.com'>Visit W3Schools</a>";
}
else
{
x.innerHTML="<a href='http://wwf.org'>Visit WWF</a>";
}
4.4 Switch Statement
function myFunction()
{
var x;
var d=new Date().getDay();
switch (d)
{
case 0:
x="Today is Sunday";
break;
case 1:
x="Today is Monday";
break;
}
document.getElementById("demo").innerHTML=x;
5. JavaScript Pop Up Boxes
• Alert box
alert("I am an alert box!");
• Alert box with line breaks
alert("HellonHow are you?");
5. JavaScript Pop Up Boxes
• Confirm Box
function myFunction()
{
var x;
var r=confirm("Press a button!");
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
}
document.getElementById("demo").innerHTML=x;
}
5. JavaScript Pop Up Boxes
• Prompt Box
function myFunction()
{
var x;
var person=prompt("Please enter your name","Harry Potter");
if (person!=null)
{
x="Hello " + person + "! How are you today?";
document.getElementById("demo").innerHTML=x;
}
}
6. Javascript Functions
• Call a function
• Function with an argument
• Function with an argument 2
• Function that returns a value
• Function with arguments, that returns a value
6.1 Call a Function
function myFunction()
{
alert("Hello World!");
}
myFunction();
HTML:
<button onclick="myFunction()">Try it</button>
6.2 Function with an argument
<script>
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script>
HTML:
<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
6.3 Function with an argument 2
<script>
function myfunction(txt)
{
alert(txt);
}
</script>
HTML:
1 :- <input type="button“ onclick="myfunction('Good Morning!')“ value="In
the Morning">
2:- <input type="button“ onclick="myfunction('Good Evening!')“ value="In
the Evening">
6.4 Function that returns a value
<script>
function myFunction()
{
return ("Hello world!");
}
</script>
<script>
var msg;
msg = myFunction();
document.write(msg)
</script>
6.5 Function with arguments, that
returns a value
<script>
function myFunction(a,b)
{
return a*b;
}
document.getElementById("demo").innerHTML=myFunction(4,3);
</script>
7. JavaScript Loops
• For loop
• Looping through HTML headers
• While loop
• Do While loop
• Break a loop
• Break and continue a loop
• Using a for... to loop through the elements of an object
7.1 For loop
<script>
function myFunction()
{
var x="",i;
for (i=0;i<5;i++)
{
x=x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML=x;
}
</script>
7.2 Looping through HTML headers
<script>
function myFunction()
{
var x="",i;
for (i=1; i<=6; i++)
{
x=x + "<h" + i + ">Heading " + i + "</h" + i + ">";
}
document.getElementById("demo").innerHTML=x;
}
</script>
7.3 While loop
<script>
function myFunction()
{
var x="",i=0;
while (i<5)
{
x=x + "The number is " + i + "<br>";
i++;
}
document.getElementById("demo").innerHTML=x;
}
</script>
7.4 Do While loop
<script>
function myFunction()
{
var x="",i=0;
do
{
x=x + "The number is " + i + "<br>";
i++;
}
while (i<5)
document.getElementById("demo").innerHTML=x;
}
</script>
7.5 Break a loop
function myFunction()
{
var x="",i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
break;
}
x=x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML=x;
}
7.6 Break and Continue a loop
function myFunction()
{
var x="",i=0;
for (i=0;i<10;i++)
{
if (i==3)
{
continue;
}
x=x + "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML=x;
}
7.7 Using a for... to loop through the
elements of an object
function myFunction()
{
var txt="";
var person={fname:"John",lname:"Doe",age:25};
for (var x in person)
{
txt=txt + person[x];
}
document.getElementById("demo").innerHTML=txt;
}
8. JavaScript Events
• Acting to the onclick event
• Acting to the onmouseover event
8. JavaScript Error Handling
• The try...catch statement
• The try...catch statement with a confirm box
• The onerror event
Reference
• W3Schools
• http://www.w3schools.com/js/js_examples.asp
Thank You
-Vikas Thange
Selenium & QTP Automation expert
(vikasthange@gmail.com)
www.vikasthange.blogspot.com

More Related Content

PDF
JavaScript Basics and Best Practices - CC FE & UX
PDF
Basics of JavaScript
PPT
JavaScript Basics
PPT
A Deeper look into Javascript Basics
ODP
Javascript
PPTX
Intro to Javascript
PDF
Anonymous functions in JavaScript
PPTX
Lab #2: Introduction to Javascript
JavaScript Basics and Best Practices - CC FE & UX
Basics of JavaScript
JavaScript Basics
A Deeper look into Javascript Basics
Javascript
Intro to Javascript
Anonymous functions in JavaScript
Lab #2: Introduction to Javascript

What's hot (20)

PDF
Fundamental JavaScript [UTC, March 2014]
PDF
Java Script Best Practices
PDF
JavaScript - From Birth To Closure
PPTX
5 Tips for Better JavaScript
PDF
Advanced javascript
PDF
A Re-Introduction to JavaScript
PDF
Intro to JavaScript
PPT
JavaScript - An Introduction
PPT
JavaScript Tutorial
PPTX
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
PPT
Advanced JavaScript
PDF
JavaScript 101
PDF
Performance Optimization and JavaScript Best Practices
PPT
Advanced JavaScript
PPT
Beginning Object-Oriented JavaScript
PDF
Introduction to web programming with JavaScript
PPTX
Lecture 5 javascript
PPTX
Object Oriented Programming In JavaScript
PDF
Patterns for JVM languages - Geecon 2014
PDF
Object Oriented Programming in JavaScript
Fundamental JavaScript [UTC, March 2014]
Java Script Best Practices
JavaScript - From Birth To Closure
5 Tips for Better JavaScript
Advanced javascript
A Re-Introduction to JavaScript
Intro to JavaScript
JavaScript - An Introduction
JavaScript Tutorial
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
Advanced JavaScript
JavaScript 101
Performance Optimization and JavaScript Best Practices
Advanced JavaScript
Beginning Object-Oriented JavaScript
Introduction to web programming with JavaScript
Lecture 5 javascript
Object Oriented Programming In JavaScript
Patterns for JVM languages - Geecon 2014
Object Oriented Programming in JavaScript
Ad

Viewers also liked (6)

PPT
Web application security
PPT
Web application security
PPTX
Android Automation Testing with Selendroid
PPTX
Getting Started with Mobile Test Automation & Appium
PPT
Android & iOS Automation Using Appium
PDF
Appium: Automation for Mobile Apps
Web application security
Web application security
Android Automation Testing with Selendroid
Getting Started with Mobile Test Automation & Appium
Android & iOS Automation Using Appium
Appium: Automation for Mobile Apps
Ad

Similar to Javascript basics for automation testing (20)

PPTX
Wt unit 5
PDF
WT UNIT 2 presentation :client side technologies JavaScript And Dom
ODP
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
PPTX
PPTX
Basics of Java Script (JS)
PPTX
Javascript 1
PDF
Wt unit 2 ppts client side technology
PDF
Wt unit 2 ppts client sied technology
PDF
27javascript
PDF
Javascript
PDF
Javascript Frameworks for Joomla
PDF
Javascript MVC & Backbone Tips & Tricks
PPT
JavaScript Training
PPTX
BITM3730 10-3.pptx
PPT
PDF
Java script
PPTX
BITM3730 10-4.pptx
KEY
Week 4 - jQuery + Ajax
PDF
PDF
Wt unit 5
WT UNIT 2 presentation :client side technologies JavaScript And Dom
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Basics of Java Script (JS)
Javascript 1
Wt unit 2 ppts client side technology
Wt unit 2 ppts client sied technology
27javascript
Javascript
Javascript Frameworks for Joomla
Javascript MVC & Backbone Tips & Tricks
JavaScript Training
BITM3730 10-3.pptx
Java script
BITM3730 10-4.pptx
Week 4 - jQuery + Ajax

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction and Scope of Bichemistry.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Pharma ospi slides which help in ospi learning
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
O7-L3 Supply Chain Operations - ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Introduction and Scope of Bichemistry.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
102 student loan defaulters named and shamed – Is someone you know on the list?
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Final Stretch: How to Release a Game and Not Die in the Process.
Pharma ospi slides which help in ospi learning
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Revamp in MTO Odoo 18 Inventory - Odoo Slides
Cardiovascular Pharmacology for pharmacy students.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF

Javascript basics for automation testing

  • 2. Topics to be covered 1. Basic JavaScript Examples 2. JavaScript Comments 3. JavaScript Variables 4. JavaScript Conditional If ... Else 5. JavaScript Popup Boxes 6. JavaScript Functions 7. JavaScript Loops 8. JavaScript Events 9. JavaScript Error Handling
  • 3. 1. Basic JS Examples • Write to the Document with JavaScript document.write("<p>My First JavaScript</p>"); • Change HTML elements with JavaScript <p id="demo">My First Paragraph.</p> document.getElementById("demo").innerHTML="My First JavaScript"; • An external JavaScript <script src="myScript.js">
  • 4. 2. JavaScript Comments • Comments Single line comment - // Multiline comments /*.......*/
  • 5. 3. JavaScript Variables • Declare a variable, assign a value to it, and display it • Code: var firstname; firstname="Hege"; document.write(firstname); document.write("<br>"); firstname="Tove"; document.write(firstname);
  • 6. 4. JavaScript Conditional If ... Else • If statement • If...else statement • Random link • Switch statement
  • 7. 4.1 If Statement function myFunction() { var x=""; var time=new Date().getHours(); if (time<20) { x="Good day"; } document.getElementById("demo").innerHTML=x; }
  • 8. 4.2 If....else Statement function myFunction() { var x=""; var time=new Date().getHours(); if (time<20) { x="Good day"; } else { x="Good evening"; } document.getElementById("demo").innerHTML=x; }
  • 9. 4.3 Random Link var r=Math.random(); var x=document.getElementById("demo") if (r>0.5) { x.innerHTML="<a href='http://w3schools.com'>Visit W3Schools</a>"; } else { x.innerHTML="<a href='http://wwf.org'>Visit WWF</a>"; }
  • 10. 4.4 Switch Statement function myFunction() { var x; var d=new Date().getDay(); switch (d) { case 0: x="Today is Sunday"; break; case 1: x="Today is Monday"; break; } document.getElementById("demo").innerHTML=x;
  • 11. 5. JavaScript Pop Up Boxes • Alert box alert("I am an alert box!"); • Alert box with line breaks alert("HellonHow are you?");
  • 12. 5. JavaScript Pop Up Boxes • Confirm Box function myFunction() { var x; var r=confirm("Press a button!"); if (r==true) { x="You pressed OK!"; } else { x="You pressed Cancel!"; } document.getElementById("demo").innerHTML=x; }
  • 13. 5. JavaScript Pop Up Boxes • Prompt Box function myFunction() { var x; var person=prompt("Please enter your name","Harry Potter"); if (person!=null) { x="Hello " + person + "! How are you today?"; document.getElementById("demo").innerHTML=x; } }
  • 14. 6. Javascript Functions • Call a function • Function with an argument • Function with an argument 2 • Function that returns a value • Function with arguments, that returns a value
  • 15. 6.1 Call a Function function myFunction() { alert("Hello World!"); } myFunction(); HTML: <button onclick="myFunction()">Try it</button>
  • 16. 6.2 Function with an argument <script> function myFunction(name,job) { alert("Welcome " + name + ", the " + job); } </script> HTML: <button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
  • 17. 6.3 Function with an argument 2 <script> function myfunction(txt) { alert(txt); } </script> HTML: 1 :- <input type="button“ onclick="myfunction('Good Morning!')“ value="In the Morning"> 2:- <input type="button“ onclick="myfunction('Good Evening!')“ value="In the Evening">
  • 18. 6.4 Function that returns a value <script> function myFunction() { return ("Hello world!"); } </script> <script> var msg; msg = myFunction(); document.write(msg) </script>
  • 19. 6.5 Function with arguments, that returns a value <script> function myFunction(a,b) { return a*b; } document.getElementById("demo").innerHTML=myFunction(4,3); </script>
  • 20. 7. JavaScript Loops • For loop • Looping through HTML headers • While loop • Do While loop • Break a loop • Break and continue a loop • Using a for... to loop through the elements of an object
  • 21. 7.1 For loop <script> function myFunction() { var x="",i; for (i=0;i<5;i++) { x=x + "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML=x; } </script>
  • 22. 7.2 Looping through HTML headers <script> function myFunction() { var x="",i; for (i=1; i<=6; i++) { x=x + "<h" + i + ">Heading " + i + "</h" + i + ">"; } document.getElementById("demo").innerHTML=x; } </script>
  • 23. 7.3 While loop <script> function myFunction() { var x="",i=0; while (i<5) { x=x + "The number is " + i + "<br>"; i++; } document.getElementById("demo").innerHTML=x; } </script>
  • 24. 7.4 Do While loop <script> function myFunction() { var x="",i=0; do { x=x + "The number is " + i + "<br>"; i++; } while (i<5) document.getElementById("demo").innerHTML=x; } </script>
  • 25. 7.5 Break a loop function myFunction() { var x="",i=0; for (i=0;i<10;i++) { if (i==3) { break; } x=x + "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML=x; }
  • 26. 7.6 Break and Continue a loop function myFunction() { var x="",i=0; for (i=0;i<10;i++) { if (i==3) { continue; } x=x + "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML=x; }
  • 27. 7.7 Using a for... to loop through the elements of an object function myFunction() { var txt=""; var person={fname:"John",lname:"Doe",age:25}; for (var x in person) { txt=txt + person[x]; } document.getElementById("demo").innerHTML=txt; }
  • 28. 8. JavaScript Events • Acting to the onclick event • Acting to the onmouseover event
  • 29. 8. JavaScript Error Handling • The try...catch statement • The try...catch statement with a confirm box • The onerror event
  • 31. Thank You -Vikas Thange Selenium & QTP Automation expert ([email protected]) www.vikasthange.blogspot.com