Showing posts with label selenium interview questions. Show all posts
Showing posts with label selenium interview questions. Show all posts

Selenium Interview Questions and Answers , Part 4

1. What are the steps to connect with Database ?

First add jar for the database/jdbc driver

Create Connection: Create code to connect with database

Create Query: Write code for the sql query.

Execute the Query

Close the connection

If there is "Select" statement: 

Add jar for the database/jdbc driver

Create Connection: Create code to connect with database

Create Query: Write code for the sql query.

Execute the Query

store the result using result set

Close the connection

 

2. What are the ways to handle Frame in Selenium ?

There are 3 ways :

Frame WebElement, such as driver.switchTo().frame(driver.findElement(By.xpath("..."));

Frame Name or ID, such as

driver.switchTo().frame("frame name");

driver.switchTo().frame("frame id");

Frame index, such as driver.switchTo().frame(index number);

DefaultContent(), such as driver.switchTo().defaultContent(); => to return back to the main window or page or the topmost frame.

 

3. What are the methods of the action class?

Action class is used to manage the mouse and the keyboard actions. The Selenium methods to manage such actions are as below:

Methods For Mouse Actions:
clickAndHold()
dragAndDrop()
moveToElement()
doubleClick()
contextClick(): this is to right click the mouse

Methods For Keyboard Actions:
sendKeys()
keyUp()
keyDown()

 

4. What are the methods used in drop down ?

Select dropdwn = new Select(driver.findElement(By.xpath("xpath of the dropdown")));

dropdwn.selectByIndex()

dropdwn.selectByVisibleText()

dropdwn.selectByValue()

dropdwn.getOptions() => this returns the list of the dropdown elements

 

5. What is final keyword in java ?

Final keyword can be used in variable, method or class .

When used in variable, the value of the variable is the final and can not be changed.

When used in method, the method can not be overridden in the child class. We can not use that method in the subclass.

When used in class, the class can not be inherited or extended by any other class.

 

6. What is finalize() method?

It is a method to perform cleanup activity. This method is called by garbage collector before deleting an object. Once the finalize() methods runs/completes , the garbage collector will immediately deletes or destroys the associated resources, connections etc. This process is called finalization in Java.

 

7. Difference between a constructor and a method:

Constructor: 

Same name like class

Can create object of the constructor

Does not have any return type

Name starts with Upper case letter

 

Methods:

Name is different than the class name

Creating object does not apply to the methods, it is block of code.

It can have a return type or no return type.

Name starts with small letter.

 

8. What is a Java Wrapper Class ?

Java Wrapper classes make it possible to use primitive data types.

Such as :

Primitive datatypes        Wrapper class

int                    Integer

char                 Character

float                Float

byte                Byte

long                Long

double            Double

boolean            Boolean

etc.

 

9. What are hard assert and soft assert ?

Hard Assert : the test execution stops if any test step fails and moves on to the next test case in the execution list.

Soft Assert : the test execution does not stop even if a test step fails and fails the test case at the end with assert all. 

 

10. How can you upload a file in Selenium ?

There are different ways, some are mentioned below:

Using SendKeys(path of the file including file name) method

Using Robot class

Using AutoIT tool

 

Selenium Interview Questions and Answers Part 3:

To watch in YouTube: Click Here

 

1.      What are implicit wait and explicit wait in Selenium ?

Implicit wait :

It is a wait in selenium which once created, applies to all the elements. That is why it is also called global wait. It waits until the given time limit before throwing exception, "No Such Element Exception". If the element is found before the time limit then it moves on ignoring the remaining time of the given time limit. The default wait time is 0 second.

An example of explicit wait is as below, in this example, the wait time limit is 10 seconds :

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit wait:

This wait does not apply to all the elements unlike implicit wait. This wait waits for the condition such as “presence of element located by”, “visibility of element located by”. This also waits until the given time limit for the condition before throwing exception. If the condition is met before the time limit then it moves on ignoring the remaining time of the given time limit.

WebDriverWait wait = new WebDriverWait(driver,10);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath")));

 

2.      What is difference between navigate and  get  ?

“get” is used for opening a webpage in a browser.

“navigate” is used for multiple purposes.

Examples:

driver.get(“https://www.technicalpage.net”); => this will open the webpage.

 

driver.navigate.to(“https://www.technicalpage.net”); => this will open the webpage.

driver.navigate.refresh(); => this will refresh the current web page.

driver.navigate.forward(); => this will take you to the next page which comes when you click forward icon on the browser window.

driver.navigate.back();=> this will take you to the next page which comes when you click back icon on the browser window.

 

3.      How you handle multiple tabs in Selenium ?

To get the current tab we use driver.getWindowHandle()

 

But for more than one tabs, we use,

Driver.getWindowHandles() command. This will return a set of string elements as below:

Set<String> tabs = driver.getWindowHandles();

 

Then we use “iterator” to switch to each of the tabs as below:

Iterator<String> it = tabs.iterator();

String tab1 = it.next();

String tab2 = it.next();

driver.switchTo().window(tab1);

driver.switchTo().window(tab2);

 

and at the end of the testing , we close all the tabs by using below command:

driver.quit();

 

 

4.      What is the difference between the interface and abstract class ?

ToTo watch in Youtube: What are the difference between an Interface and an Abstract Class?

Interface:

1.      The objects(methods, variables etc) inside interface are by default “public” and only “public” visibility(access modifier) can be used.

2.      The visibility should not change when the interface is implemented by any class.

3.      The methods in interface should be abstract only.

4.      The implementing class should define all the undefined methods inside the interface.  

5.      An interface can extend another interface(one or many) or a class can implement an interface(one or many).

6.      The variable inside interface are by default public static and final , so the variable must be assigned a value. 

7.      Multiple inheritance is possible in the interface.

8.      Interface uses “Interface” keyword to declare the interface.

9.      An interface can not have a constructor

10.   An interface can not create object ,ie, can not instantiate.

 

Abstract Class:

1.      The objects(methods, variables etc) inside Abstract Class can be “public”, “private”, protected” or “default”.

2.      The visibility might change when an abstract class is inherited by another class.

3.      The methods inside abstract class can be abstract or non-abstract.

4.      The inheriting(child) class does not need to implement all the abstract methods of the parent class and if it does not implement all the abstract methods, then this child class also becomes an abstract class.

5.      An abstract class can implement interfaces(one or many) or can extend another class(abstract or non-abstract).

6.      The variable inside an abstract class can be static, non-static, public or other visibility, final or non-final and does not need the variable value to be assigned.

7.      Multiple inheritance is possible in an abstract class when it implements multiple interfaces. But a class(abstract or non-abstract) can not extend multiple classes. So, multiple inheritance will not be possible if we try to extend more than one class.

8.      Abstract class uses “abstract” keyword to declare the abstract class.

9.      An abstract  class can have a constructor.

10.   An abstract class can not create object ,ie, can not instantiate

 

5.      What is method overloading and method overriding?

Method Overloading:

When a class has more than one methods with same name but different parameters(either different types of parameters or different order of parameters), this is called method over loading.

findSales();

findSales(int i);

findSales(int i, String s);

findSales(String s, int i);

findSales(double i);

 

Method overriding:

When two or more classes having parent/child relationship, are having method with same name and same parameterization( or no parameterization), is called method overriding .  In such situation, when the method is called, the child method gets precedence. 

Example:

Parent class

public class Employee {

      

       public void methodA()

       {

              System.out.println("This is method in parent class.");

              }     

}

Child class

public class Dept extends Employee {

 

       public void methodA()

       {

              System.out.println("This is method in child class.");

              }

}

 

Running above codes:

public class Run {

       public static void main(String[] args) {       

              Dept obj = new Dept();

              obj.methodA();

             }

}

 

 

Output:

This is method in child class.

 

 

6.      What is inheritance ?

Inheritance is a process to establish a parent-child relationship between the classes. In this the child class ( also called sub class) uses the features(methods, properties) of parent class (also called super class). The child class uses the word “extends” to use the features  of the parent class. Hence we can use the code, method of the parent class.

 

Types of Inheritance:

Single level

Multilevel

Multiple

Hybrid

Hierarchical

 

7. How you get a page title in Selenium ?

Driver.getTitle();

Or

To store the title in a variable

String pageTitle = driver.getTitle();

 

8. What is data driven framework ?

It is a framework in which the data and the test code will be separate so that change in data will not affect the code and vice versa. 

We keep the data in Excel, CSV, XML, Database etc. And we read the data whenever required by the code from the data sources.

 

9. What is the difference between the single forward slash(“/” ) and double forward slash (“//” ) ?

Single forward slash is for absolute xpath, ie, if single slash is used in the xpath , than it is called absolute xpath. In this the xpath starts from the root node. So, the xpath becomes long and complicate. And this xpath will have more possibility of breaking as it is long and if any of the node changed in the middle, the xpath does not work.

Double forward slash is for relative xpath. In this the xpath starts from the node/point from where an unique xpath is possible. This xpath is short and there is less possibility of such xpath breaking. So, relative xpath will be more reliable than absolute xpath.

 

Example:


 

Absolute xpath: /html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td/input[@id='email']

Relative xpath: //input[@id='email']

 

 

10. What is the difference between findElement() and findElements() ?

FindElement() will locate/find the first element on the current page. It returns a single web element.

WebElement webelement = driver.findElement(By.xpath(""));

 

findElements() will find all the matching element on the current page. It returns a list of web elements.

      List<WebElement> webelements = driver.findElements(By.xpath(""));