Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Selenium Java Interview Questions and Answers Part-9: By: Bijan Patel - in

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

 By: Bijan Patel|In: Automation

Selenium Java Interview Questions and Answers Part-9


1) How to handle Selenium WebDriver Exceptions?
We can handle selenium exceptions by using try catch block methods of Java.

try{
driver.findElement(by.id("button")).click();
}
catch(NoSuchElementException e){
System.out.println("Element not present");
}
 

2) There are four browser windows opened and you don’t have any idea where the required element
is present. What will be your approach to find that element?
– use getWindowHandles() method to get Window handles of all browser windows
– use switchTo() method to switch to each browser window using the handle id
– Find the element in each browser window and close the window if not present

3) How do you handle an alert pop-up in Selenium?


We can use the following methods to handle an alert in Selenium:

- dismiss()
driver.switchTo().alert().dismiss();
- accept()
driver.switchTo().alert().accept();
 

4) How do you retrieve the text displayed on an Alert?


String text = driver.switchTo().alert().getText();
 

5) How do you type text into the text box on an Alert?


driver.switchTo().alert().sendKeys("Text");
 
6) Is Alert in Selenium an Interface or Class?
Alert is an interface in Selenium.

7) How do you handle frames in Selenium?


We can switch to frames by following methods:
– By Index
driver.switchTo().frame(0);
– By Name or Id
driver.switchTo().frame(“id of the element”);
– By Web Element
driver.switchTo().frame(WebElement);

8) Give an example for method overloading concept that you have used in Selenium?
Implicit Wait in Selenium use method overloading as we can provide different Timestamp or TimeUnit
like SECONDS, MINUTES, etc.

9) How do you select a value from a drop-down field and what are the different methods available?
We can select value from drop-down using methods of Select class. Following are the methods:
– selectByVisibleText
– selectByValue
– selectByIndex

Select elements = new Select(driver.findElement(By.id("button"));


elements.selectByVisibleText("Selenium");
elements.selectByIndex(1);
 

10) When your XPath is matching more than one element, how do you handle it to locate the required
element?
We can use index of the element to locate it or we can use different Xpath axes methods to locate the
element like Following, Ancestor, Child, Preceding or Following-sibling

11) How do you capture screen-shots in Selenium and what is the best place to have the screen-shot
code?
//Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
//Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile=new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
 

12) Write the code for connecting to Excel files and other operations?
XSSFWorkbook srcBook = new XSSFWorkbook("Demo.xlsx");
XSSFSheet sourceSheet = srcBook.getSheetAt(0);
int rownum=rowcounter;
XSSFRow sourceRow = sourceSheet.getRow(rownum);
XSSFCell cell1=sourceRow.getCell(0);
13) How do you read and write into a PDF file?
BufferedInputStream file = new BufferedInputStream("Path of PDF file");
PDFParser pdf = new PDFParser(file);
pdf.parse();
String text = new PDFTestStripper().getText(pdf.getPDDocument());
 

14) What are the disadvantages of Selenium?


– It supports only web applications and cannot automate desktop applications
– No default reporting mechanism
– No default object repository
– Cannot automate captcha

15) How do you debug your automation code when it is not working as expected?
– Add breakpoints on the lines of code where it is not working

– Run code in debugging mode

– Use different actions like F7(Step Into), F8(Step Over), F9(Step Out) to debug the problem

16) What are the end methods you use for verifying whether the end result is achieved by our
Selenium automation scripts?
We can use different assertion methods available in different test frameworks like TestNG or Junit.

17) How do you clear the cookies of a browser using Selenium, before starting the execution?
driver.manage().deleteAllCookies();
 

18) How do you implement collections in your framework?


Collections can be used in framework in situations where you have to store large number of objects. For
example, findElements() method returns a list of all matching elements.

19) Give a scenario where inheritance is used in your framework?


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.

20) Give a scenario where interface is used in your framework?


WebDriver is an interface and when we create an instance of the driver object to use its different
methods.

21) Write a code using JavascriptExecutor to scroll the web page?


//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
 

22) What is the use of property file in Selenium?


Property file can be used to store the different web elements of an application or to store all the
different application, framework configurations.

23) How do you handle multiple browsers selection in Selenium?


We can select different browsers in Selenium using TestNG framework.

24) What do you use for reporting in your Selenium Project?


We can use the default TestNG or Cucumber report. We can also use different reporting libraries like
Extent reports.

25) How Cross Browser testing is handled in Selenium?


@BeforeTest
@Parameters("browser")
public void setup(String browser) throws Exception{
//Check if parameter passed from TestNG is 'firefox'
if(browser.equalsIgnoreCase("firefox")){
//create firefox instance
System.setProperty("webdriver.gecko.driver", ".\\geckodriver.exe");
driver = new FirefoxDriver();
}
//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
//set path to chromedriver.exe
System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();
}
testng.xml:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="com.qascript.crossbrowsertests">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="Firefox" />
<classes>
<class name="com.qascript.crossbrowsertests">
</class>
</classes>
</test>
</suite>
 

You might also like