Hexaware Technologies Selenium Recently Asked Interview Questions Answers
List some scenarios which we cannot automate using Selenium WebDriver?
Bitmap comparison Is not possible using Selenium WebDriver
Automating Captcha is not possible using Selenium WebDriver
We can not read barcode using Selenium WebDriver
We can not automate OTP
How to Upload a file in Selenium WebDriver?
We can Upload a file in Selenium WebDriver directly by passing the Path of the file as an argument to the sendKeys() if the element contains “type=file” in the source code.
Syntax:
driver.findElement(By.xapth(“//div[@type=’file’]”)).sendKeys(“C://Downloads/Resume.html”);
If the element does not contains “type=file” in the source code then uploading a file in Selenium WebDriver can be handled by using Robot class or AutoIT.
How to set test case priority in TestNG?
We can set test case priority in TestNG by using priority attribute to the @Test annotations.
Example:
@Test(priority=2)
public void m1(){
System.out.println(“This is m1 method”);
}
@Test(priority=1)
public void m2(){
System.out.println(“This is m2 method”);
}
Output:
This is m2 method
This is m1 method
What is the default port used by Hub in Selenium Grid?
The default port used by the hub is 4444
How do u get the width of the textbox?
driver.findElement(By.xpath(“xpath of textbox ”)).getSize().getWidth();
driver.findElement(By.xpath(“xpath of textbox ”)).getSize().getHeight();
Is WebElement an interface or a class ?
WebElement is an Interface.
What is the default port used by Node in Selenium Grid?
The default port used by the Node is 5555
What are all the Wait conditions in Selenium? Difference between implicitly Wait, Explicitly Wait and Fluent Wait?
Implicit Wait:
Selenium Web Driver has borrowed the idea of implicit waits from Watir.
The implicit wait will tell to the web driver to wait for certain amount of time before it throws a “No Such Element Exception”.
The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.
It is for Page Level.
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Explicit Wait:
This wait method for Element level.
The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an “ElementNotVisibleException” exception.
The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements.
Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
Syntax:
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
Fluent Wait uses two parameters – timeout value and polling frequency.
First of all, it sets the following values.
1- The maximum amount of time to wait for a condition, and
2- The frequency to check the success or failure of a specified condition.
Also, if you want to configure the wait to ignore exceptions such as <NoSuchElementException>
Syntax :
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id(“foo”));
}
});
Step By Step Analysis Of The Above Sample Code.
Step-1: Fluent Wait starts with capturing the start time to determine delay.
Step-2: Fluent Wait then checks the condition defined in the until() method.
Step-3: If the condition fails, Fluent Wait makes the application to wait as per the value set by the <pollingEvery(5, SECONDS)> method call. Here in this example, it’s 5 seconds.
Step-4: After the wait defined in Step 3 expires, start time is checked against the current time. If the difference of the wait start time (set in step-1) and the current time is less than the time set in <withTimeout(30, SECONDS)> method, then Step-2 will need to repeat.
The above steps will recur until either the timeout expires or the condition becomes true.
How to Handle 2 Frames?
to switchto a frame:
driver.switchTo.frame(“Frame_ID”);
to switch to the default again.(parent)
driver.switchTo().defaultContent();
Explain About Maven concept?
Maven simplifies the code handling and process of building the project. Most of the projects follow maven structure.
Download all dependencies provided the dependencies are available in maven central repository. If any of the dependency is not available in maven central repository then you have to add repository path in pom.xml explicitly.
There are many other build tools available in like ant. But it is better to use maven while dealing with different versions and different dependencies. Maven even can manage the dependencies of dependencies. Other tools may not provide such flexibility like maven
Why Jenkins and Selenium?
Running Selenium tests in Jenkins allows you to run your tests every time your software changes and deploy the software to a new environment when the tests pass.
Jenkins can schedule your tests to run at specific time.
You can save the execution history and Test Reports.
Jenkins supports Maven for building and testing a project in continuous integration.
How to capture the color of Web Element or text color in WebDriver?
You can get the element color(Background color of element) by:
element.getCssValue(“background-color”);
You can get the element text/caption color by:
element.getCssValue(“color”);
How to handle 2 windows or explain window handling.
driver.getWindowHandle(); – Single Window
driver.getWindowHandles(); – Multiple windows
Difference between ANT and MAVEN?
Ant Maven
Ant doesn’t has formal conventions, so we need to provide information of the project structure in build.xml file. Maven has a convention to place source code, compiled code etc. So we don’t need to provide information about the project structure in pom.xml file.
Ant is procedural. You need to provide information about what to do and when to do through code. You need to provide order. Maven is declarative, everything you define in the pom.xml file.
There is no life cycle in Ant. There is life cycle in Maven.
It is a tool box. It is a framework.
It is mainly a build tool. It is mainly a project management tool.
The ant scripts are not reusable. The maven plugins are reusable.
It is less preferred than Maven. It is more preferred than Ant.
What is latest version of Selenium WebDriver?
3.11
How to Upload the file in selenium?
Using with SendKeys
RobotClass
AutoIT
List out some TestNG Annotations.?
@BeforeSuite – Before Suite will always execute prior to all annotations or tests in the suite.
@BeforeTest – Before Test will always execute prior to Before Class, ,Before Method and Test Method
@BeforeClass – Before Class will always execute prior to Before Method and Test Method
@BeforeMethod – Before Method will execute before every test method
@AfterMethod – After Method will execute after every test method
@AfterClass – After Class will always execute later to After Method and Test method
@AfterTest – After Test will always execute later to After Method, After Class
@AfterSuite – After suite will always execute at last when all the annotations or test in the suite have run
@Test – Mandatory annotation.
How you will share your output to managers?
TestNG – Emailable Report or XSLT Report.
How to merge all your team members’ code and how you will run?
Git Repository or SVN
How to read and Write the Data from Excel Sheet?
Read Excel Data : FileInputStream.
Write Excel data : FileOutStream
What is the difference between xpath and css selector?
CSS selectors perform far better than Xpath. Majorly in IE Browser.
Xpath : powerful selection of the DOM
CSS : looks simpler. consistent support across browsers
XPATH: no native support for xpath in IE (WebDriver uses a 3rd party library)
CSS: Tests may need to be updated with UI changes
Xpath engines are different in each browser, hence make them inconsistent
IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.
Xpath tend to become complex and hence make hard to read
Explain Testng.xml structure. ( Suite-> Test> Classes>Class)?
Example:
<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”example suite 1″ verbose=”1″ >
<test name=”Regression suite 1″ >
<classes>
<class name=”com.first.example.demoOne”/>
<class name=”com.first.example.demoTwo”/>
<class name=”com.second.example.demoThree”/>
</classes>
</test>
</suite>
We need to specify the class names along with packages in between the classes’ tags.
What are all the challenges you have faced while doing the Automation.?
Dealing with pop-up windows:
Selenium can sometimes fail to record common popups in web apps. To handle any kind of alert popup, you can apply a getAlert function. Before actually running the script, you must import a package that can generate a WebDriver script for handling alerts. The efficient interface brings with it the following commands: void dismiss(), void accept (), getText(), void sendKeys(String stringToSend). The first two basically click on the “cancel” and “OK” buttons respectively on a popup window.
No event trigger from value changes:
Because Selenium does not initiate events with a change in values, one must do it oneself using fireEvent: selenium.FireEvent(cmbCategory, “onchange”);
Timeout resulting from synchronization problems:
One should ideally use selenium.IsElementPresent(locator) to verify that the object is in a loop with Thread.Sleep
Testing Flash apps:
To automate flash apps with Selenium, one can use Flex Monkium. The application source code must be compiled with the swc files generated by Flex Monkium. Then the app and the Selenium IDE are connected, and the tests can be recorded with IDE.
Unexpected error launching Internet Explorer. Browser zoom level should be set to 100% by default for the IE browser to overcome this error.
Protected Mode must be set to the same value error occurs when trying to run Selenium WebDriver on a fresh Windows machine. This issue can be fixed by using capabilities as below when launching IE.
What are Exception and Error? Specify the Difference.?
Exception: Exception occurs in the programmer’s code .which can be handled and resolvable.
Ex: arithmetic exception
DivideByZeroException
NullPointerException
ClassNotFoundException
Error: Errors are not resolvable by the programmer. Error occurs due to lack of system resources.
Ex: Stack overflow
hardware error
JVM error
Why Exception Handling important in Selenium?
To continue the flow of execution.
Most Common Exceptions Which We Notice In Selenium WebDriver?
NoSuchElementException: An element could not be located on the page using the given search parameters.
NoSuchFrameException: A request to switch to a frame could not be satisfied because the frame could not be found.
StaleElementReferenceException: An element command failed because the referenced element is no longer attached to the DOM.
Firefox Not Connected Exception: Firefox browser upgraded to new version.
ElementIsNotSelectable Exception: An attempt was made to select an element that cannot be selected.
unknown command Exception: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.
ElementNotVisible Exception: An element command could not be completed because the element is not visible on the page.
InvalidElementState Exception: An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element).
UnknownError Exception: An unknown server-side error occurred while processing the command.
javascript error Exception: An error occurred while executing JavaScript code.
XPathLookupError Exception: An error occurred while searching for an element by XPath.
Timeout Exception: An operation did not complete before its timeout expired.
NoSuchWindow Exception: A request to switch to a different window could not be satisfied because the window could not be found.
InvalidCookieDomain Exception: An illegal attempt was made to set a cookie under a different domain than the current page.
UnableToSetCookie Exception: A request to set a cookie’s value could not be satisfied.
UnexpectedAlertOpen Exception: A modal dialog was open, blocking this operation
NoAlertOpenError Exception: An attempt was made to operate on a modal dialog when one was not open.
ScriptTimeout Exception: A script did not complete before its timeout expired.
InvalidElementCoordinates Exception: The coordinates provided to an interactions operation are invalid.
InvalidSelector Exception: Argument was an invalid selector (e.g. XPath/CSS).
Explain the difference between Absolute XPath and relative XPath?
Types of X-path
There are two types of XPath:
1) Absolute XPath.
2) Relative XPath.
Absolute XPath:
It is the direct way to find the element.
It will start with the single forward slash(/) – which means you can select the element from the root node.
Ex : html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b
Disadvantage:
If there are any changes made in the path of the element then that XPath gets failed.
Relative XPath:
It will starts from the middle of the HTML. It starts with the double forward slash (//).
It which means it can search the element anywhere on the webpage.
Ex : //*[@class=’featured-box’]//*[text()=’Testing’]
How to Priority your tests in TestNG?
Ex: @Test (priority=1)
How to handle Ajax calls?
AJAX allows the Web page to retrieve small amounts of data from the server without reloading the entire page.
To test Ajax application, different wait methods should be applied
ThreadSleep
Implicit Wait
Explicit Wait
WebdriverWait
Fluent Wait
How to handle Ajax calls?
AJAX allows the Web page to retrieve small amounts of data from the server without reloading the entire page.
To test Ajax application, different wait methods should be applied
ThreadSleep
Implicit Wait
Explicit Wait
WebdriverWait
Fluent Wait.
List some scenarios which we cannot automate using Selenium WebDriver?
Bitmap comparison Is not possible using Selenium WebDriver
Automating Captcha is not possible using Selenium WebDriver
We can not read barcode using Selenium WebDriver
We can not automate OTP
How to Upload a file in Selenium WebDriver?
We can Upload a file in Selenium WebDriver directly by passing the Path of the file as an argument to the sendKeys() if the element contains “type=file” in the source code.
Syntax:
driver.findElement(By.xapth(“//div[@type=’file’]”)).sendKeys(“C://Downloads/Resume.html”);
If the element does not contains “type=file” in the source code then uploading a file in Selenium WebDriver can be handled by using Robot class or AutoIT.
Hexaware Technologies Selenium Recently Asked Interview Questions Answers |
How to set test case priority in TestNG?
We can set test case priority in TestNG by using priority attribute to the @Test annotations.
Example:
@Test(priority=2)
public void m1(){
System.out.println(“This is m1 method”);
}
@Test(priority=1)
public void m2(){
System.out.println(“This is m2 method”);
}
Output:
This is m2 method
This is m1 method
What is the default port used by Hub in Selenium Grid?
The default port used by the hub is 4444
How do u get the width of the textbox?
driver.findElement(By.xpath(“xpath of textbox ”)).getSize().getWidth();
driver.findElement(By.xpath(“xpath of textbox ”)).getSize().getHeight();
Is WebElement an interface or a class ?
WebElement is an Interface.
What is the default port used by Node in Selenium Grid?
The default port used by the Node is 5555
What are all the Wait conditions in Selenium? Difference between implicitly Wait, Explicitly Wait and Fluent Wait?
Implicit Wait:
Selenium Web Driver has borrowed the idea of implicit waits from Watir.
The implicit wait will tell to the web driver to wait for certain amount of time before it throws a “No Such Element Exception”.
The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.
It is for Page Level.
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Explicit Wait:
This wait method for Element level.
The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an “ElementNotVisibleException” exception.
The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements.
Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax elements.
Syntax:
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
Fluent Wait uses two parameters – timeout value and polling frequency.
First of all, it sets the following values.
1- The maximum amount of time to wait for a condition, and
2- The frequency to check the success or failure of a specified condition.
Also, if you want to configure the wait to ignore exceptions such as <NoSuchElementException>
Syntax :
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id(“foo”));
}
});
Step By Step Analysis Of The Above Sample Code.
Step-1: Fluent Wait starts with capturing the start time to determine delay.
Step-2: Fluent Wait then checks the condition defined in the until() method.
Step-3: If the condition fails, Fluent Wait makes the application to wait as per the value set by the <pollingEvery(5, SECONDS)> method call. Here in this example, it’s 5 seconds.
Step-4: After the wait defined in Step 3 expires, start time is checked against the current time. If the difference of the wait start time (set in step-1) and the current time is less than the time set in <withTimeout(30, SECONDS)> method, then Step-2 will need to repeat.
The above steps will recur until either the timeout expires or the condition becomes true.
How to Handle 2 Frames?
to switchto a frame:
driver.switchTo.frame(“Frame_ID”);
to switch to the default again.(parent)
driver.switchTo().defaultContent();
Explain About Maven concept?
Maven simplifies the code handling and process of building the project. Most of the projects follow maven structure.
Download all dependencies provided the dependencies are available in maven central repository. If any of the dependency is not available in maven central repository then you have to add repository path in pom.xml explicitly.
There are many other build tools available in like ant. But it is better to use maven while dealing with different versions and different dependencies. Maven even can manage the dependencies of dependencies. Other tools may not provide such flexibility like maven
Why Jenkins and Selenium?
Running Selenium tests in Jenkins allows you to run your tests every time your software changes and deploy the software to a new environment when the tests pass.
Jenkins can schedule your tests to run at specific time.
You can save the execution history and Test Reports.
Jenkins supports Maven for building and testing a project in continuous integration.
How to capture the color of Web Element or text color in WebDriver?
You can get the element color(Background color of element) by:
element.getCssValue(“background-color”);
You can get the element text/caption color by:
element.getCssValue(“color”);
How to handle 2 windows or explain window handling.
driver.getWindowHandle(); – Single Window
driver.getWindowHandles(); – Multiple windows
Difference between ANT and MAVEN?
Ant Maven
Ant doesn’t has formal conventions, so we need to provide information of the project structure in build.xml file. Maven has a convention to place source code, compiled code etc. So we don’t need to provide information about the project structure in pom.xml file.
Ant is procedural. You need to provide information about what to do and when to do through code. You need to provide order. Maven is declarative, everything you define in the pom.xml file.
There is no life cycle in Ant. There is life cycle in Maven.
It is a tool box. It is a framework.
It is mainly a build tool. It is mainly a project management tool.
The ant scripts are not reusable. The maven plugins are reusable.
It is less preferred than Maven. It is more preferred than Ant.
What is latest version of Selenium WebDriver?
3.11
How to Upload the file in selenium?
Using with SendKeys
RobotClass
AutoIT
List out some TestNG Annotations.?
@BeforeSuite – Before Suite will always execute prior to all annotations or tests in the suite.
@BeforeTest – Before Test will always execute prior to Before Class, ,Before Method and Test Method
@BeforeClass – Before Class will always execute prior to Before Method and Test Method
@BeforeMethod – Before Method will execute before every test method
@AfterMethod – After Method will execute after every test method
@AfterClass – After Class will always execute later to After Method and Test method
@AfterTest – After Test will always execute later to After Method, After Class
@AfterSuite – After suite will always execute at last when all the annotations or test in the suite have run
@Test – Mandatory annotation.
How you will share your output to managers?
TestNG – Emailable Report or XSLT Report.
How to merge all your team members’ code and how you will run?
Git Repository or SVN
How to read and Write the Data from Excel Sheet?
Read Excel Data : FileInputStream.
Write Excel data : FileOutStream
What is the difference between xpath and css selector?
CSS selectors perform far better than Xpath. Majorly in IE Browser.
Xpath : powerful selection of the DOM
CSS : looks simpler. consistent support across browsers
XPATH: no native support for xpath in IE (WebDriver uses a 3rd party library)
CSS: Tests may need to be updated with UI changes
Xpath engines are different in each browser, hence make them inconsistent
IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.
Xpath tend to become complex and hence make hard to read
Explain Testng.xml structure. ( Suite-> Test> Classes>Class)?
Example:
<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”example suite 1″ verbose=”1″ >
<test name=”Regression suite 1″ >
<classes>
<class name=”com.first.example.demoOne”/>
<class name=”com.first.example.demoTwo”/>
<class name=”com.second.example.demoThree”/>
</classes>
</test>
</suite>
We need to specify the class names along with packages in between the classes’ tags.
What are all the challenges you have faced while doing the Automation.?
Dealing with pop-up windows:
Selenium can sometimes fail to record common popups in web apps. To handle any kind of alert popup, you can apply a getAlert function. Before actually running the script, you must import a package that can generate a WebDriver script for handling alerts. The efficient interface brings with it the following commands: void dismiss(), void accept (), getText(), void sendKeys(String stringToSend). The first two basically click on the “cancel” and “OK” buttons respectively on a popup window.
No event trigger from value changes:
Because Selenium does not initiate events with a change in values, one must do it oneself using fireEvent: selenium.FireEvent(cmbCategory, “onchange”);
Timeout resulting from synchronization problems:
One should ideally use selenium.IsElementPresent(locator) to verify that the object is in a loop with Thread.Sleep
Testing Flash apps:
To automate flash apps with Selenium, one can use Flex Monkium. The application source code must be compiled with the swc files generated by Flex Monkium. Then the app and the Selenium IDE are connected, and the tests can be recorded with IDE.
Unexpected error launching Internet Explorer. Browser zoom level should be set to 100% by default for the IE browser to overcome this error.
Protected Mode must be set to the same value error occurs when trying to run Selenium WebDriver on a fresh Windows machine. This issue can be fixed by using capabilities as below when launching IE.
What are Exception and Error? Specify the Difference.?
Exception: Exception occurs in the programmer’s code .which can be handled and resolvable.
Ex: arithmetic exception
DivideByZeroException
NullPointerException
ClassNotFoundException
Error: Errors are not resolvable by the programmer. Error occurs due to lack of system resources.
Ex: Stack overflow
hardware error
JVM error
Why Exception Handling important in Selenium?
To continue the flow of execution.
Most Common Exceptions Which We Notice In Selenium WebDriver?
NoSuchElementException: An element could not be located on the page using the given search parameters.
NoSuchFrameException: A request to switch to a frame could not be satisfied because the frame could not be found.
StaleElementReferenceException: An element command failed because the referenced element is no longer attached to the DOM.
Firefox Not Connected Exception: Firefox browser upgraded to new version.
ElementIsNotSelectable Exception: An attempt was made to select an element that cannot be selected.
unknown command Exception: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.
ElementNotVisible Exception: An element command could not be completed because the element is not visible on the page.
InvalidElementState Exception: An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element).
UnknownError Exception: An unknown server-side error occurred while processing the command.
javascript error Exception: An error occurred while executing JavaScript code.
XPathLookupError Exception: An error occurred while searching for an element by XPath.
Timeout Exception: An operation did not complete before its timeout expired.
NoSuchWindow Exception: A request to switch to a different window could not be satisfied because the window could not be found.
InvalidCookieDomain Exception: An illegal attempt was made to set a cookie under a different domain than the current page.
UnableToSetCookie Exception: A request to set a cookie’s value could not be satisfied.
UnexpectedAlertOpen Exception: A modal dialog was open, blocking this operation
NoAlertOpenError Exception: An attempt was made to operate on a modal dialog when one was not open.
ScriptTimeout Exception: A script did not complete before its timeout expired.
InvalidElementCoordinates Exception: The coordinates provided to an interactions operation are invalid.
InvalidSelector Exception: Argument was an invalid selector (e.g. XPath/CSS).
Explain the difference between Absolute XPath and relative XPath?
Types of X-path
There are two types of XPath:
1) Absolute XPath.
2) Relative XPath.
Absolute XPath:
It is the direct way to find the element.
It will start with the single forward slash(/) – which means you can select the element from the root node.
Ex : html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b
Disadvantage:
If there are any changes made in the path of the element then that XPath gets failed.
Relative XPath:
It will starts from the middle of the HTML. It starts with the double forward slash (//).
It which means it can search the element anywhere on the webpage.
Ex : //*[@class=’featured-box’]//*[text()=’Testing’]
How to Priority your tests in TestNG?
Ex: @Test (priority=1)
How to handle Ajax calls?
AJAX allows the Web page to retrieve small amounts of data from the server without reloading the entire page.
To test Ajax application, different wait methods should be applied
ThreadSleep
Implicit Wait
Explicit Wait
WebdriverWait
Fluent Wait
How to handle Ajax calls?
AJAX allows the Web page to retrieve small amounts of data from the server without reloading the entire page.
To test Ajax application, different wait methods should be applied
ThreadSleep
Implicit Wait
Explicit Wait
WebdriverWait
Fluent Wait.
Post a Comment