Selenium Interview Questions
Selenium Interview Questions
}
Q3. What is the use of xpath ?
Ans- it is used to find the WebElement in web page. It is very useful to identify the dynamic web
elements.
Q4. What are different types of locators ?
Ans- There are 8 types of locators and all are the static methods of the By class.
By.id(), By.name(), By.tagName(), By.className(), By.linkText(), By.partialLinkText(), By.xpath,
By.cssSelector().
Q5. What is the difference between Assert and Verify?
Ans- Both are used for verify the result.
When an assert fails, the test will be aborted.
Where if a verify fails, the test will continue executing and logging the failure.
Q6. What is the alternate way to click on login button?
Ans- use submit() method but it can be used only when attribute type=submit.
Ex:
public class ClickonLoginButtonwithoutClick {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("password")).submit();
}
Q7. How do you verify if the checkbox/radio is checked or not?
Ans- We can use isSelected() method.
Syntax
driver.findElement(By.xpath("xpath of the checkbox/radio button")).isSelected();
If the return value of this method is true then it is checked else it is not.
Ex:
public class checkboxSelection {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("password")).submit();
Boolean
state=driver.findElement(By.cssSelector("input[value='roundtrip']")).isSelected();
if (state=true)
System.out.println("check box selected");
else
System.out.println("Check box not selected");
}
}
Q8. How do you handle alert pop-up ?
Ans- To handle alert pop-ups, we need to 1st switch control to alert pop-ups then click on ok or cancel
then move control back to main page.
Ex:
public class PopupsandAlerts {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://output.jsbin.com/usidix/1");
String mainpage=driver.getWindowHandle(); // focus on main page
driver.findElement(By.cssSelector("input[value='Go!']")).click();
}
}
Q11. How do perform drag and drop using WebDriver?
Ans- use Action class.
Actions act = new Actions(driver);
WebElement source = driver.findElement(By.xpath( -----)); //source ele which you want to drag
WebElement target = driver.findElement(By.xpath( -----)); //target where you want to drop
act.dragAndDrop(source,target).perform();
Q12. Give the example for method overload in WebDriver.
Ans- frame(string), frame(int), frame(WebElement).
Q13. How do you upload a file?
Ans- To upload a file we can use sendKeys() method.
Syntax - driver.findElement(By.xpath(input field)).sendKeys(path of the file which u want to
upload);
Q14. How do you click on a menu item in a drop down menu?
Ans- if that menu has been created by using select tag then we can use the methods
selectByValue() or selectByIndex() or selectByVisibleText(). These are the methods of the
Select class.
If the menu has not been created by using the select tag then we can simply find the xpath of that
element and click on that to select.
Q15. How do you simulate browser back and forward ?
Ans- driver.navigate().back();
driver.navigate().forward();
Q16. How do you get the current page URL ?
Ans- driver.getCurrentUrl();
Q17. What is the difference between / and // ?
Ans-
findElement() - it used to find the one web element. It return only one WebElement type.
findElements()- it used to find more than one web element. It returns List of Web Elements.
Q19. How do you achieve synchronization in WebDriver ?
Ans- We can use implicit wait.
Syntax- driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Here it will wait for 10sec if while execution driver did not find the element in the page immediately.
This code will attach with each and every line of the script automatically. It is not required to write
everytime. Just write it once after opening the browser.
Q20. Write the code for Writing to Excel through Selenium ?
Ans-
Ans- A framework is set of automation guidelines which help in Maintaining consistency of Testing,
Improves test structuring, Minimum usage of code, Less Maintenance of code, Improve re-usability,
Non Technical testers can be involved in code, Training period of using the tool can be reduced,
Involves Data wherever appropriate.
There are five types of framework used in software automation testing:
1-Data Driven Automation Framework
2-Method Driven Automation Framework
3-Modular Automation Framework
4-Keyword Driven Automation Framework
5-Hybrid Automation Framework , its basically combination of different frameworks. (1+2+3).
Q26. What are the prerequisites to run selenium webdriver?
Ans- JDK, Eclipse, WebDriver(selenium standalone jar file), browser, application to be tested.
Q27. What are the advantages of selenium webdriver?
Ans- a) It supports with most of the browsers like Firefox, IE, Chrome, Safari, Opera etc.
b) It supports with most of the language like Java, Python, Ruby, C# etc.
b) Doesnt require to start server before executing the test script.
c) It has actual core API which has binding in a range of languages.
d) It supports of moving mouse cursors.
e) It support to test iphone/Android applications.
Q28. What is WebDriverBackedSelenium ?
Ans- WebDriverBackedSelenium is a kind of class name where we can create an object for it as below:
Selenium wbdriver= new WebDriverBackedSelenium(WebDriver object name, "URL path of website").
The main use of this is when we want to write code using both WebDriver and Selenium RC , we must
use above created object to use selenium commands.
Q29. How to invoke an application in webdriver ?
Ans- driver.get(url); or driver.navigate().to(url);
Q30. What is Selenium Grid ?
Ans- Selenium-Grid allows you to run your tests on different machines against different browsers in
parallel. That is, running multiple tests at the same time against different machines, different browsers
and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for
running your tests in a distributed test execution environment.
Q31. How to get the number of frames on a page ?
Ans- List framesList = driver.findElements(By.xpath("//iframe"));
int numOfFrames = frameList.size();
System.out.println("value over"+cssValue1);
driver.close();
}
}
Q43. How to change the URL on a webpage using selenium web driver ?
Ans- driver.get(url1);
driver.get(url2);
Q44. How to hover the mouse on an element ?
Ans- Actions act = new Actions(driver);
act.moveToElement(webelement); //webelement on which you want to move cursor
Q45. What is the use of getOptions() method ?
Ans- getOptions() is used to get the selected option from the dropdown list.
Q46. What is the use of deSelectAll() method ?
Ans- It is used to deselect all the options which have been selected from the dropdown list.
Q47. Is WebElement an interface or a class ?
Ans- WebDriver and WebElement are an Interfaces.
Q48. FirefoxDriver is class or an interface and from where is it inherited ?
Ans- FirefoxDriver is a class. It implements all the methods of WebDriver interface.
Q49. Which is the super interface of Webdriver ?
Ans- SearchContext.
Q50. What is the difference b/w close() and quit()?
Ans- close() - it will close the browser where the control is.
quit() - it will close all the browsers opened by WebDriver.
Q51. How to enter text without using sendkeys() ?
Ans - Yes we can enter text without using sendKeys() method. We have to use combination of
javascript and wrapper classes with WebDriver extension class, check the below codepublic static void setAttribute(WebElement element, String
attributeName, String value)
{
WrapsDriver wrappedElement = (WrapsDriver) element;
JavascriptExecutor driver = (JavascriptExecutor)
wrappedElement.getWrappedDriver();
driver.executeScript("arguments[0].setAttribute(arguments[1],
arguments[2])", element, attributeName, value);
}
call the above method in the test script and pass the text field attribute and pass the text you want to
enter.
Q52. There is a scenario whenever "Assert.assertEquals()" function fails automatically it
has to take screenshot. How can you achieve this ?
Ans- By using EventFiringWebDriver.
SyntaxEventFiringWebDriver eDriver=new EventFiringWebDriver(driver);
File srcFile = eDriver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(imgPath));
Q53. How do you handle https website in selenium
Ans- By changing the setting of FirefoxProfile.
Syntaxpublic class HTTPSSecuredConnection {
public static void main(String[] args){
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(false);
WebDriver driver = new FirefoxDriver(profile);
driver.get("url");
}
}
Q54. How to login into any site if its showing any authetication popup for user name and
pass ?
Ans - pass the username and password with url.
Syntax- http://username:password@url
ex- http://creyate:jamesbond007@alpha.creyate.com
Q55. What is the name of Headless browser.
Ans- HtmlUnitDriver.
Q56. Open a browser in memory means whenever it will try to open a browser the browser
page must not come and can perform the operation internally.
Ans- use HtmlUnitDriver.
expublic class Memory {
public static void main(String[] args) {
Q61. While explaining the framework, what are points which should be covered ?
Ans-
webelement.sendKeys(press);
Q67. What is the use of contextClick() ?
In our project Your_Project_Name (ex- CustomerCentre) , we are using Hybrid Framework which is
the combination of Data Driven, Method Driven and Modular Driven Framework.
Basically this involves 3 stages1) Designing the framework,
2) Implementing the framework,
3) Execution of the framework.
1) Designing the framework-
a) while designing the frame work in anyone of the local drive create a folder by name
HybridFramework. Launch the eclipse, set the workspace to HybridFramework.
b) in eclipse create a java project by name CustomerCentre.
c) in eclipse under CustomerCentre proj create a folder Jars & to that Jars folder include all the jar
files related to the project such WebDriver jar, poi jar etc.
d) in eclipse under CustomerCentre proj create another folder by name Drivers to this folder copy all
the executable files or driver such as Chrome Driver, IE driver and so on.
When designing the framework there are methods which can be used in more than one project such
methods we call it as Generic method. And there are methods which are related to only one single
project those methods are called as project specific method.
Naming convention for package- com.projectname.packagename (com.CustomerCentre.Library)
All the methods related to Generic, we write them under GenericLibrary class which is present in the
Libraries package. And all the project specific methods are written under ProjectSpecificLibrary class
which is present under libraries package.
All the GenericLibrary method should be public as well as static. Like getLastRowNum, getCellValue,
setCellValue, etc.
In the application there are certain types of steps which has to be used again & again such as open
browser, open application & so on. In order to handle these types of steps we create ConfigLibrary
(SuperTestNG) class in which we have written the methods by using Selenium annotations like
@BeforeMethod, @AfterMethod etc. so that we need not to write the code for repeating steps each
time. Inharit this class from all other classes to use these class methods.
We create an excel file by name Config, in which we wrote all the input data for the testing and then
copy the entire excel file, and paste it under the CustomerCentre project.
To confirm everything working fine or not, we create a dummy package with dummy @Test method. If
browser is getting launched, app is getting launched & browser is getting closed i.e. whatever we have
developed till is correct. Then delete the dummy package.
To perform the validation, we create one more class Assertion. Whenever we need to perform
validation we need to call assertText() or assertTitle() present under the Assertion class.
(Assert.assertEquals())
This is where actual implementation of the framework start. While going thru the manual test cases
we note down the common repeated steps and make those steps as a project specific methods. Even
if one step is repeating , may be in same test case or other test case make it as a method. Then write
the test script for all the manual test cases.
3)- Execution-
Right click on the project, navigate to TestNG convert to TestNG give the proper suite name and
test name and click on finish. Then execute this xml file (ctrl+f11) or right click and run as TestNG
Suite. Or to run thru cmd navigate till project then give this commndjava -cp bin;jars/* org.testng.TestNG testng.xml