MainMenu

Home Java Overview Maven Tutorials

Tuesday 6 February 2018

Frequently Asked Selenium Interview Questions and aswers




Hell Friends,

In this article we will discuss about basic Selenium interview Questions for Selenium Tester :

0.What is Automation Testing?
When Testing script/cases/tests run by some robot/tool , then it will become automation testing.

1. What is selenium?
A tool, which will automate your web browser.

Question :- What is the difference between CSS & Xpath?
Answer :- Difference Between CSS & Xpath
There are some differences between xpath and css listed below −


1).Xpath allows bidirectional flow which means the traversal can be both ways from parent to child and child to parent as well. Css allows only one directional flow which means the traversal is from parent to child only.

2).Xpath is slower in terms of performance and speed. Css has better performance and speed than xpath.

3).Customized css can be created directly with the help of attributes id and class. For id, the css expression is represented by # followed by the id [ #<<id expression>>.
For class, the css expression is represented by . followed by the class [.<<class expression>>]. Xpath does not have any feature like this.

4).Xpath expression is represented by [//tagname[@attribute = 'value']. The css expression is repression is represented by [tagname[attribute = 'value'].

5).There are two types of xpath – absolute and relative. But css has no such types.

Question :- Why do we write WebDriver driver = new ChromeDriver(), explain?

Answer :- WebDriver driver = new ChromeDriver();
WebDriver is an interface and all the methods which are declared in WebDriver interface are implemented by respective driver class.
We generally do it this way because usually we want to be able to run our tests on multiple browsers. If we declare the driver as a specific driver type, we are then anchored to only that driver. This is not a problem if you only ever need to test on say Chrome for example. But what if you later want your tests to also be able to work with IE, Opera, Firefox, etc.? These are extended classes of the WebDriver interface.
If your main tests and other classes define the commonly shared driver as simply WebDriver instead of specifically being tied to ChromeDriver, then the same tests can be run without change to the test code itself simply by initializing the shared driver object with a different driver extended class.
Let’s see why can’t we use the following statement.
WebDriver driver = new WebDriver();
We cannot write our code like this because we cannot create Object of an Interface.
WebDriver is an interface.


Question :- Difference between Implicit wait & explicit wait?
Answer :- 1). Implicit-wait :- The driver is asked to wait for a specific amount of time for the element to be available on the DOM of the page. It is a global wait and applied to all elements on the webpage.
It affects the execution speed since each step waits for this wait till it gets the element it is looking for.
The implicit wait timeout is set to 0 by default.

Explicit-wait:- The driver is asked to wait till a certain condition is satisfied.It is not a global wait and applied to a particular scenario.
It does not affect the execution speed since it is applicable to a particular element of the page.
WebDriverWait objwait = new WebDriverWait(obj, 10); // this will wait for 10 seconds
objwait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(""));
Note : - The default polling time is 500 mili seconds.
So until your wait condition is true and the specified time is not over , the WebDriver checks for the element once every 500 mili seconds.

Question :- Staleness exception of selenium ?
Answer :-A stale element reference exception is thrown in one of two cases, the first being more common than the second:
The element has been deleted entirely.
The element is no longer attached to the DOM.
Solution : Refreshing the web page



Question :- How to Handle Disabled Elements in selenium ?
Answer :- WebElement dis = driver.findElement(By.xpath("//*[@id='csc']/button"));
if(dis.isEnabled())
{
dis.click();
}
else
{
System.out.println("Not enabled");
}

Question :- How to Handle hidden elements in selenium?
Answer :- WebElement element = driver.findElement(By.xpath("/html/body/p"));
JavascriptExecutor js = (JavascriptExecutor)driver;
String y2 = (String) js.executeScript("return arguments[0].innerText", element);
System.out.println(y2);

Question :- Staleness exception of selenium ?
Answer :-A stale element reference exception is thrown in one of two cases, the first being more common than the second:
The element has been deleted entirely.
The element is no longer attached to the DOM.
Solution : Refreshing the web page

Question :- What is Call by value & call by refrence in Java??

Answer :-
Call by Value :A copy of actual arguments is passed to respective formal arguments.
"Both types of parameters are stored in different locations."
"Any changes inside functions are not reflected in actual parametrs of caller".

Call by Reference :- Both actual and formal parameters refer to same locations and any changes made are reflected in actual parameters of caller.
Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value.
Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.

Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters.

The parameters passed to function are called actual parameters whereas the parameters received by function are called formal parameters.


Example of call by value:-
public class Practice
{
static int x = 10;
static int y = 20;
public static void main(String args[])
{
Practice obj = new Practice();
System.out.println(obj.sum(x, y));
System.out.println(obj.sum(obj.x, obj.y));
}
public int sum(int a, int b)
{
int c = a+b;
return c;
}
}

Output :-
30
30


Example of call by Reference:-

class simpleone
{
String name;
}

public class Practice
{
public static void main(String args[])
{
Practice obj = new Practice();
simpleone obj1 = new simpleone();
transform(obj1);
}
public static void transform(simpleone obj2)
{
obj2.name = "way2testing";
System.out.println( obj2.name);
}
}

Output :-
way2testing

Question :- Assert & verify in selenium?
Answer :- Both Assert and Verify commands are used to find whether a given input is present or not on the webpage. There are some difference between Assert and Verify in Selenium.

Assert command in selenium:
When an “assert” command fails, the test execution will be aborted. So when the Assertion fails, all the test steps after that line of code are skipped.
The solution to overcoming this issue is to use a try-catch block. We use the Assertion in the try catch block. Mostly, the assert command is used when the end result of the check value should pass to continue to the next step.

Verify command in selenium:
When a “verify” command fails, the test will continue executing and logging the failure.
Mostly, the Verify command is used to check non-critical things.
In such cases where we move forward even though the end result of the check value is failed.

Example of Soft & Hard Assert :-
public class Practice
{
@Test
public void Tes()
{
int x = 10;
int y = 20;
SoftAssert sa = new SoftAssert();
sa.assertEquals(x, y);
System.out.println("I will be executed only after soft assertion");
sa.assertAll();
Assert.assertEquals(x, y);
System.out.println("I will not be executed");
}
}


Example of verify :-
if(condition)
{
executed when condition is true
}
else
{
executed when condition is false
}

try
{
executed when condition is true
}
catch(Exception e)
{
executed when condition is false
}

No comments:

Post a Comment