Selenium Interview Questions

What type of tests have you automated?
Our main focus is to automate test cases to do Regression testing, Smoke testing, and Sanity testing.

How many test cases you have automated per day?                                                         
I did automate 2-5 test scenarios per day when the complexity is limited. Sometimes just 1 or fewer test scenarios in a day when the complexity is high.

What are the different exceptions you have faced in Selenium WebDriver?
  1. ElementNotVisibleException
  2. StaleElementReferenceException
  3. WebDriverException
  4. IllegalStateException
  5. TimeoutException
  6. NoAlertPresentException
  7. NoSuchWindowException
  8. NoSuchElementException
What is the alternative to driver.get() method to open an URL using Selenium WebDriver?
Alternative method to driver.get(“url”) method is driver.navigate.to(“url”)
driver.get(): open an URL and it will wait till the whole page gets loaded

driver.navigate.to()navigate to an URL and It will not wait till the whole page gets loaded.


How to delete cookies in Selenium?                                                                                    

To delete cookies we use deleteAllCookies() method 

driver.manage().deleteAllCookies();
What is the difference between driver.getWindowHandle() and driver.getWindowHandles() in Selenium WebDriver?
driver.getWindowHandle() – It returns a handle of the current page (a unique identifier)

driver.getWindowHandles() – It returns a set of handles of the all the pages available.


How to handle Ajax calls in Selenium WebDriver?
The best approach to handle this kind of situations in selenium is to use dynamic waits (i.e. WebDriverWait in combination with ExpectedCondition)
How to create and run TestNG.xml? 
In TestNG framework, we need to create TestNG XML file to create and handle multiple test classes. We do configure our test run, set test dependency, include or exclude any test, method, class or package and set priority etc in the XML file.
What is Parameterized testing in TestNG?
Parameterized tests allow developers to run the same test over and over again using different values.
Explain what is assertion in Selenium and what are the types of assertion?
Assertion is used as a  verification point. It verifies that the state of the application conforms to what is expected.  The types of assertion are “assert” , “verify” and “waifFor”.
How you can use “submit” a form using Selenium ?
You can use “submit” method on element to submit form-
element.submit () ;
Explain how you can login into any site if it’s showing any authentication popup for password and username?
Pass the username and password with url
 Explain how you can find broken images in a page using Selenium Web driver ?
To find the broken images in a page using Selenium web driver is
  • Get XPath and get all the links in the page using tag name
  • In the page click on each and every link
  • Look for 404/500 in the target page title
Explain using Webdriver how you can perform double click ?
You can perform double click by using
  •  Actions act = new Actions (driver);
  • act.doubleClick(webelement);
 How do you read data from excel ?

FileInputStream fis = new FileInputStream(“path of excel file”);
Workbook wb = WorkbookFactory.create(fis);
Sheet s = wb.getSheet(“sheetName”);
String value = s.getRow(rowNum).getCell(cellNum).getStringCellValue();

Write the code for Reading and Writing to Excel through Selenium ?
FileInputStream fis = new FileInputStream(“path of excel file”);

 Workbook wb = WorkbookFactory.create(fis);

 Sheet s = wb.getSheet("sheetName");

 String value = s.getRow(rowNum).getCell(cellNum).getStringCellValue(); // read data

 s.getRow(rowNum).getCell(cellNum).setCellValue("value to be set"); //write data

 FileOutputStream fos = new FileOutputStream(“path of file”);

 wb.write(fos); //save file


What is the use of getOptions() method ?
getOptions() is used to get the selected option from the dropdown list.

Is WebElement an interface or a class ?
WebDriver is an Interface.

FirefoxDriver is class or an interface and from where is it inherited ?
FirefoxDriver is a class. It implements all the methods of WebDriver interface

What is the use of contextClick() ?
It is used to right click.

How can we capture screenshots in selenium ?
Using getScreenshotAs method of TakesScreenshot interface we can take the screenshots in selenium.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(“D:\\testScreenShot.jpg”));

Where you have applied OOPS in Automation Framework?

ABSTRACTION

In Page Object Model design pattern, we write locators (such as id, name, xpath etc.,) in a Page Class. We utilize these locators in tests but we can’t see these locators in the tests. Literally we hide the locators from the tests.

INHERITANCE

We create a Base Class in the Framework to initialize WebDriver interface, WebDriver waits, Property files, Excels, etc., in the Base Class.
We extend the Base Class in other classes such as Tests and Utility Class. Extending one class into other class is known as Inheritance.

POLYMORPHISM

Combination of overloading and overriding is known as Polymorphism. 
We use implicit wait in Selenium. Implicit wait is an example of overloading. In Implicit wait we use different time stamps such as SECONDS, MINUTES, HOURS etc.,
Declaring a method in child class which is already present in the parent class is called Method Overriding. Examples are get and navigate methods of different drivers in Selenium