Professional Documents
Culture Documents
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
Firefox
FirefoxDriver driver = new FirefoxDriver();
Chrome Driver
System.setProperty("webdriver.chrome.driver", "path of chrome driver executable");
ChromeDriver driver = new ChromeDriver();
IE Driver
System.setProperty("webdriver.ie.driver", "path of ie driver executable");
InternetExplorerDriver driver = new InternetExplorerDriver();
What is Annotation
Annotation can be defined as metatag, which holds information about methods which are placed next to it.
Unit Testing tool like Junit and TestNg support Annotations
Annotations in Junit
@Test
@Test (timeout=500)
@Test(expected=IllegalArgumentException.class)
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
Order of Execution
@BeforeClass @Before @Test @After @AfterClass
Order of Execution
@BeforeSuite @BeforeTest @BeforeGroup @BeforeClass --> @BeforeMethod @Test
@AfterMethod @AfterClass @AfterGroup @AfterTest @AfterSuite
TestNg Junit
Generate html reports automatically For reports, we need to use ANT
Can generate report for test case as well as test We cant create report of 1 test case, only reports
suite for test suites can be generated
Can set order of execution of multiple test Cant set order of execution of multiple test
annotation in single class file using priority annotation in single class file
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
TestNg.xml is very simple and easy to understand Build.xml is complex and difficult to understand
We have option to set threads for parallel Cant set multiple threads(parallel execution)
execution(used in Grid)
driver.get("http://rediff.com"); driver.navigate().to("http://rediff.com");
driver.navigate().back();
driver.navigate().forward();
1 Thread.sleep(10)
Thread is a java class, we are calling sleep method of that class
It will add a forcefully wait at particular point
This is used when we want to always pause our execution at specific point
2 ImplicitWait
Implicitly wait are mainly used when our element on page are taking some time to load
It we dont add any wait in our script, then our driver will search for element and if not found immediately
failed that step
In case of implicitly wait, our driver will wait for specified time(time mentioned in implicitly wait) for that
element to be present
This wait will be applicable for findElements commands only
This need to placed at the start of test case, will be applied on all findElements commands
4 Fluent Wait
It implements Wait interface and we create object of FluentWait class
Here we can set maximum time we should wait for element to be present(withTimeout method)
Here we can set after how much time, it will check for element to be present (pollingEvery method)
Here we can ignore any exception if it comes at runtime while waiting (ignoring method)
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
// Setting iterator to work until value exist there Difference between Assert and Verify
while(iter.hasNext())
{
// Here we can perform different actions on window' Verify: If verify got failed, it will fail that particular step
// here in code we are just moving to the windows and execution will move to next step, remaining steps
// and closing all windows opened by webdriver will execute of that test case
driver.switchTo().window((String) iter.next()).close();
}
Assert : If assert got failed, execution of that test case
will halt on that step, no more steps of that test case will
execute
In selenium, first we pick element locator of all elements on which we are supposed to perform our action and
place in a property file(Here property file behave as Object Repository for Selenium)
Now wherever in our test case, element locator value is required. We fetch element locator value from
properties file (this is called properties file because extension of this file is .properties)
To fetch value of element locator from property file, we need to create object of ResourceBundle class and
by that object we can use getString method.
Desired Capabilities help to set properties for the Web Driver. A typical use case would be to
set the path for the Firefox Driver if your local installation doesn't correspond to the default
settings.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
How to perform Keyboard and Mouse Operations in Selenium
Or
How to Handle Page onLoad Authentication
For attachment and downloading we need to perform some keyboard or mouse operations, for that we have
few options in Selenium
POM refers to Page Object Model which encapsulate the internal state of a page into a single page
object. UI changes only affect to a single Page Object, not to the actual test codes.
Advantages :
Code re-use: Able to use the same page object in a variety of tests cases.
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
How can we get the font size, font color, font type used for a particular text on a webpage using
Selenium web driver?
driver.findelement(By.Xpath("Xpath ").getcssvalue("font-size or font-colour or font-type);
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
What is WebDriverBackedSelenium ?
WebDriverBackedSelenium is a kind of class name where we can create an object for it as below:
Selenium wbdriver= new WebDriverBackedSelenium(WebDriver object name, "URL ")
The main use of this is when we want to write code using both WebDriver and Selenium RC, we
must use above created object to use selenium commands.
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
When we compile java code, it convert java file to byte code (class file) rather than machine code,
byte code is machine and platform independent (.class file). We can interpret these class file to any
machine having JVM, JVM is used to interpret class file and convert it to machine dependent code
and then execute it.
When an object is no longer referred to by any variable, java/JVM automatically reclaims memory used by that
object. This is known as garbage collection.
System. gc() method may be used to call it explicitly.
This keyword can be used with variable, with the help of this keyword we can set which one is class variable
and which one is local variable(this.variable name shows , this is a class variable)
This keyword can also be used to call current class overloaded constructor.
This keyword can be used to call current class methods
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
Static keyword in Java can be used with variable, method and nested class(class inside another class)
static variable: static variable holds single memory location independent to number of objects we have
created, all objects will access data from same memory location.
static methods: static methods are like static variable holds single memory location for all objects.
STATIC methods/variable can be called by the class name itself without creating object of the class
Static nested class object created without creating object of its outer class
Methods, variables which are not static is called instance variable/methods
Super keyword in java is used to call parent class override method or parent class constructor
Super Class Method Calling
1--> In case child class override, parent class method. When we create child class object and call override
method it will access child class method but if we want to access parent class method, we are going to use
super keyword
Super Class Constructor Calling
2--> super keyword is used to call parent class constructor as well
Difference between :-
`
Methods which are just declared not defined or Methods which are declared and defined as well or
method without body. method with body.
Class with abstract & concrete methods. Having Class with concrete methods only
at least 1 abstract method
Cannot create object of abstract class Can create object of concrete class
Class with Abstract + Concrete methods Interface can have only abstract methods
Can have constants as well as variable Variable declared are by default final(means
constants)
We can inherit only 1 class We can inherit multiple interface(by this way java is
supporting multiple inheritance)
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
Use extends keyword for inheritance Use implements keyword for inheritance
Inheritance: With the help of inheritance, we can transfer the properties of a class to child class
Data Encapsulation: Wrapping up of data and function in a single unit is called encapsulation.
In java class implements the concept of encapsulation
Method Overloading:
When more than 1 method in a class having the same method name but different signature
(Different signature means different number and type of arguments) is said to be method overloading.
Method Overriding:
When patent and child class have same name and same signature (signature means number and type of
arguments) method, this is called method overriding.
(Method overriding exist in case of inheritance only, when we are making parent-child relationship between
classes)
Inheritance
With the help of inheritance we can transfer the properties of a class to another class
Class which is inherited is called Parent Class or Super Class
Class which inherit other class is called Child Class or Sub Class
After inheritance, child class objects can access parent class method and variables .
Type of Inheritance
Single/Simple Multilevel Inheritance Multiple Inheritance Hybrid Inheritance
Inheritance
Supported in Java Supported in Java Not directly supported Not directly supported
(supported through (supported through
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
interface) interface)
Constructors
Constructor can be defined as a group of code (same like method) which is used for initialization
Initialization means anything that we want to perform at start
Points to remember:-
Constructor name is always same as class name
Constructor does not return any value so we dont write void while creating constructor
Constructors are automatically called when object is created we need not to call them explicitly as we
do in methods( as constructor is automatically called when object is created so all code pasted inside
constructed is executed before we use class object to call any other methods, thats why it is called
initialization)
We can have more than 1 constructor in a class with different signature, this is called constructor
overloading
1>> throws keyword : we can place throws keyword in front of our method definition and we can mention
classes of all exception which we want to handle or we can mention parent class Exception
1--> Throws is used in Method Signature while Throw is used in Java Code (send the instance of Exception
class)
2--> Throws, we can mention multiple type of exception, separated by comma while in throw we can set only 1
exception class instance
3-->Throws keyword just throws exception need not to handle it, throw pass exception instance to caller
program
final :
final keyword can be used for class, method and variables.
A final class cannot be inherited, a final method cant be override.
A final variable becomes constant, we cant change its value
finally :
finally, keyword used in exception handling.
It is used with a block of code that will be executed after a try/catch block has completed
The finally block will execute whether or not an exception is thrown.
Means if exception is not throws then Try execute then Finally will execute, in case when exception is thrown
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
Example : Like we have written DB connection code in try block but exception comes
In that case connection persist with DB and no other user are allowed to create connection, So in that kind of
scenario we want whatever the condition comes, may be exception or not, my DB connection should break,
for that Ill paste connection breaking code in finally() block
finalize() : finalize() method is used to code something that we want to execute just before garbage collection
is performed by JVM
Generic means we create generic method which serves our multiple purposes like we can use same method
for multiple tasks like, sorting an integer array, String array etc.
Generic Methods : We can create generic methods which can be called by different type of
arguments, methods will server request on the behalf of type of argument coming with request
Collection is a interface, which gives architecture to hold multiple data by same name
In JAVA, Collection interface is implemented by Set, List and Map
Array ArrayList
Use to hold multiple data of single data type Use to hold multiple data of single/diff data type
Need to define size of array Need not to define size, size automatically increase
/decrease as we add/remove element
Can access data through index Can access data through Iterator
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
Answer: Iterating to List and Set is very simple but in case of HashMap we have to use an extra method
"keySet" which pass hashmap object to Iterator
package mypack;
import java.util.HashMap;
import java.util.Iterator;
import org.junit.Test;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MyClas {
public static void main(String aa[]){
HashMap hp = new HashMap();
hp.put("K1", "Val1");
hp.put("K2", "Val2");
Iterator itr= hp.keySet().iterator();
while(itr.hasNext()){
String k = itr.next().toString();
System.out.println(hp.get(k));
}}}
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
package org.abc
public class Tst1 {
public static void main(String aa[])
{
int i,j
i=100
j=300
i=i+j // 400
j=ij//
400300
=100 Now j=100
i=ij
//400100
=300 Now i=300
// Here we can see values exchanged successfully without using 3rd variable
System.out.println(i)
System.out.println(j)
}
}
Printing * Triangle
*
**
***
package mypack
public class MyClas {
public static void main(String aa[])
{
int Num=3
int i,j,k
System.out.println("Star Triangle up to "+Num+" line is below" )
// Here First loop is created for switching line
for(i=0i<=(Num1)i++) // Taken (num1)here because we started lo0p from 0
{
// Here this loop is created for printing spaces in every line
for(j=5j>ij)
{
System.out.print(" ")
}
// Here this loop is created for printing * in every line
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
for(k=0k<=ik++)
{
System.out.print(" * ")
}
System.out.println("") // Here we are switching to next line after printing *
}}}
Print * Triangle Printing Right angle * triangle
**** *
*** **
** ***
*
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
System.out.println("Element arrString*j+=s
exist in system at index "+ j) }
break }
}} }
if(flag==0) for(i=0i<(arrString.length)i++)
System.out.println("Element is not System.out.println(arrString*i+)
found")
}} }}
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
www.thetestingworld.com
TESTING
www.thetestingworld.com Call/Whatsapp - 874391 3121
WORLD
www.thetestingworld.com