Top Selenium Interview Questions and Answers – 1
Q1. What is WebDriver ?
- WebDriver is API which controls browser interaction in Selenium Suite, It is part of Selenium Suite/Component.
- WebDriver technically is Interface in WebDriver API .
Q2. What is TestNG and it’s advantage over Junit ?
WebDriver does not have mechanism for generating reports & TestNG gives this mechanism to webdriver.
TestNG Benefits as Below:
1.Annotations are easier to understand as compared to Junit.
2.Test cases can be grouped more easily in TestNG since annotationns are esy to understand as compare to Junit.
3.Parallel Testing is Possible in TestNG.
Q3. Why to use Thread.sleep ,Implicit or Explicit wait in Selenium WebDriver ?
Main use is to solve synchronization problem.
During Automation Testing two software are working i.e. Selenium and ClearTrip (AUT-Application under Test ) & there are time AUT needs to communicate with database or do API Calls which required little long time but Selenium is firing all commands with same speed .Hence there is changes of script failure due to availability of elements or Objects.
Q4. Why Assertion are Important in Selenium?
Assertions are point where comparison between expected and actual happens. Means if you use assertions then only you are doing testing using automation testing else without assertions just browser interaction happens with no testing.
Q5.What is Maven ? & in which folder maven dependencies are stored ?
- It is kind of build tool which combines all source code into single unit And using poim.xml of maven project we are declare which Jars need to be used as dependency which maven automatically downloads & attaches to project. sample is as below :
- .m2 is folder where all jars get downloaded and stored.
- You can add maven depenency of Selenium WebDriver as below in pom.xml file
<!-- Source: https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.40.0</version>
<scope>compile</scope>
</dependency>
Q6. Why to use JavaScriptExecuter ?
For Scrolling on Web Page.
Q7. Difference between driver.close() & driver.quit() ?
For closing focused tab driver.close() is used & for closing browser(and all associated tabs ) driver.quite() is used.
Q8. Difference between findElement & findElements() ?
findElement:
1.Used for working with single element on web page.
2.Return type is WebElement
3.NoSuchElement exception is thrown if web object is not found
findElements:
1.Used for working with multiple element on web page .
2.Return type is List<WebElement>
3.No exception is thrown if web object is not found(Empty list is returned in case of web object is not found )
Q9. Difference between Actions and Action ?
Actions:
1.It is Class
2.Used for controlling keyboard and mouse movements/actions.
Action:
1.It is Interface
2.Used for making composite(series) of action
Q10. What is Advantage of Relative Xpath over CSS ?
CSS can travel only in one direction (Parent node to Child node) but Xpath can travel bi-direction(Parent node to Child node & Child node to Parent node)
Q11. What is difference between Absolute and Relative Xpath ?
Absolute Xpath:It always starts with “/” and starts selection of element from root/start of Page. And it is not recommended due to dependency of Page Layout
Relative Xpath: It alway starts with “//” and starts selection from element which is having some unique property. And It recommended to use over Absolute xpath and CSS since it is not depended on page layout & selection can be done in both direction(Parent node to child node & Child node to Parent )
Q12. How to find all available links on Web Page ?
Links are having comman tagname as “a”. Hence below method can be used , 1. driver.findElements(By.tagname(“a”)) or 2.driver.findElements(By.xpath(“//a”))
Q13. How to switch to iframe ,alert and window ?
A. For switching to Alert : driver.switchTo().alert();
B. For switching to Iframe : driver.switchTo().frame(“”)
C. For switching to Window : driver.switchTo().window(“”);
Q14. What are different ways of switching to iframe ?
Before are three ways to switch to iframe
1.driver.switchTo().frame(index of iframe)
2.driver.switchTo().frame(“id or name of iframe”)
3.driver.switchTo().frame(WebElement)
Q15. In your project where you have used collection. ?
1.For making collection of elements having same property like links
List<WebElement> allLinks=driver.findElements(By.tagname(“a”))
2.During handling Cookies
Set<Cookie> allCookie= driver.manage().getCookies();
3.During handling tabs/windows
Set<String> allWin=driver.getWindowHandles();
Q16. What are cssSelector locator syntax ?
1. For Element having id as attribute
css=tagname#id
2. For Element having name/value/type as attribute
css=tagname[name/value/type=’value of name/value/type’]
3. For Element having classname as attribute
css=tagname.classname
Q17. What are ways of scrolling using WebDriver ?
1.For scrolling to particular element
((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView();”, element);
2.For scrolling pixel by pixel
((JavascriptExecutor) driver).executeScript(“window.scrollBy(x-pixels,y-pixels)”);
3.For scrolling at bottom of page
((JavascriptExecutor) driver).executeScript(“window.scrollBy(0, document.body.scrollHeight)”);
18. Give methods used to verify status(checked or not) of check box & element is displayed and clickable.
For Checking Checkbox is selected “isSelected()” is used.
For Checking Element is displayed “isDisplayed()” is used.
For Checking Element is clickable “isEnabled()” is used.
19. List Annotations of TestNG and Junit 5.
TestNG Annotations:
1. BeforeSuite
2. AfterSuite
3. BeforeTest
4. AfterTest
5. BeforeClass
6. AfterClass
7. BeforeMethod
8. AfterMethod
9. DataProvider
Junit 5(Junit Jupiter) Annotation
1. BeforeAll
2. AfterAll
3. BeforeEach
4. AfterEach
Junit 4 Annotation
1.BeforeClass
2.AfterClass
3.Before
4.After
Q20. Difference between get() and navigate().to().
driver.get(“”)
Accepts only url starting with “http” else it will throws exception
Does not maintains history of browser hence forword and backword is not possible.
Waits till page get loaded.
driver.navigate().to()
1.Accepts url in String and URL class type.Below are some possible combination
• driver.navigate().to(“http://google.com”)
• driver.navigate().to(new URL(“http://google.com”))
2.Maintains history of browser hence forword and backword is possible.
3.Does not wait to load page.
