Selenium WebDriver Questions and Answers for Interview SET – 1
What is Automation Testing?
- Automation testing is a process of automating the manual process to test the application/system under test.
- Automation testing involves use to a separate testing tool which lets you create test scripts which can be executed repeatedly and doesn’t require any manual intervention.
- Automated testing tools are capable of executing tests, reporting outcomes and comparing results with earlier test runs.
What are the benefits of Automation Testing?
- Supports execution of repeated test cases.
- Enables parallel execution.
- Improves accuracy thereby reducing human-generated errors.
- Saves time and money.
- Increased throughput or productivity.
- Improved quality or increased predictability of quality.
- Improved robustness of processes or product.
- Increased consistency of output.
- Reduced direct human labor costs and expenses.
Why should Selenium be selected as a test tool?
- Selenium is pure open source, freeware and portable tool.
- Selenium have a large user base and helping communities.
- Selenium have cross Browser compatibility (Firefox, Chrome, Internet Explorer, Safari etc.).
- Selenium have great platform compatibility (Windows, Mac OS, Linux etc.).
- Selenium supports multiple programming languages (Java, C#, Ruby, Python, Pearl etc.).
- Selenium has fresh and regular repository developments.
- Selenium supports distributed testing.
What is Selenium? What are the different Selenium components?
- Selenium is one of the most popular automated testing suites.
- Selenium is designed in a way to support and encourage automation testing of functional aspects of web-based applications and a wide range of browsers and platforms.
- Due to its existence in the open source community, it has become one of the most accepted tools amongst the testing professionals.
Components of Selenium:
- Selenium IDE
- Selenium RC
- Selenium WebDriver
- Selenium Grid
- Selenium Integrated Development Environment (IDE):– Selenium IDE is a record and playback tool. It is distributed as a Firefox Plugin.
- Selenium Remote Control (RC):– Selenium RC is a server that allows a user to create test scripts in the desired programming language. It also allows executing test scripts within the large spectrum of browsers.
- Selenium WebDriver: – WebDriver is a different tool altogether that has various advantages over Selenium RC. WebDriver directly communicates with the web browser and uses its native compatibility to automate.
- Selenium Grid: – Selenium Grid is used to distribute your test execution on multiple platforms and environments concurrently.
What are the testing types that can be supported by Selenium?
- Functional Testing.
- Regression Testing.
- Sanity Testing.
- Smoke Testing.
- Responsive Testing.
- Cross Browser Testing.
- UI testing (black box)
- Integration Testing.
What are the limitations of Selenium?
- Selenium supports testing of only web-based applications.
- Mobile applications cannot be tested using Selenium.
- Captcha and Barcode readers cannot be tested using Selenium.
- Reports can only be generated using third-party tools like TestNG or JUnit.
- As Selenium is a free tool, thus there is no ready vendor support though the user can find numerous helping communities.
- The user is expected to possess prior programming language knowledge.
What are the different types of locators in Selenium?
The locator can be termed as an address that identifies a web element uniquely within the webpage.
- ID
- ClassName
- Name
- TagName
- LinkText
- PartialLinkText
- Xpath
- CSS Selector
- DOM
What the difference between assert and verify commands?
Assert: Assert command checks whether the given condition is true or false.
- Let’s say we assert whether the given element is present on the web page or not.
- If the condition is true then the program control will execute the next test step but if the condition is false, the execution would stop and no further test would be executed.
Verify: Verify command also checks whether the given condition is true or false.
- Irrespective of the condition being true or false, the program execution doesn’t halt i.e. any failure during verification would not stop the execution and all the test steps would be executed.
What is an XPath?
- XPath is used to locate a web element based on its XML path.
- XML stands for Extensible Markup Language and is used to store, organize and transport arbitrary data.
- It stores data in a key-value pair which is very much similar to HTML tags.
What is the difference between “/” and “//” in Xpath?
- Single Slash “/”:– Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.
- Double Slash “//”:– Double slash is used to create Xpath with relative path i.e. the xpath would be created to start selection from anywhere within the document.
Selenium WebDriver Questions and Answers for Interview
When should I use Selenium Grid?
- Selenium Grid can be used to execute same or different test scripts on multiple platforms and browsers concurrently so as to achieve distributed test execution, testing under different environments and saving execution time remarkably.
How do I launch the browser using WebDriver?
The following syntax can be used to launch Browser:
1 2 3 4 5 | WebDriver driver = new FirefoxDriver(); WebDriver driver = new ChromeDriver(); WebDriver driver = new InternetExplorerDriver(); |
What are the different types of Drivers available in WebDriver?
The different drivers available in WebDriver are:
- FirefoxDriver
- InternetExplorerDriver
- ChromeDriver
- SafariDriver
- OperaDriver
- AndroidDriver
- IPhoneDriver
- HtmlUnitDriver
What are the different types of waits available in WebDriver?
There are two types of waits available in WebDriver:
- Implicit Wait
- Explicit Wait
- Implicit Wait: Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.
- Explicit Wait: Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, explicit waits are applied for a particular instance only.
Handle textbox using Selenium?
- User can use sendKeys(“String_Name”) to enter the string in the textbox.
Syntax:
1 2 3 4 5 | WebElement username = drv.findElement(By.id(“Email”)); // entering username username.sendKeys(“sth”); |
How can you find if an element in displayed on the screen?
WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.
- isDisplayed()
- isSelected()
- isEnabled()
Syntax:
1 2 3 4 5 6 7 8 | isDisplayed(): boolean buttonPresence = driver.findElement(By.id(“gbqfba”)).isDisplayed(); isSelected(): boolean buttonSelected = driver.findElement(By.id(“gbqfba”)).isDisplayed(); isEnabled(): boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled(); |
How can we get a text of a web element?
- Get command is used to retrieve the inner text of the specified web element.
- The command doesn’t require any parameter but returns a string value.
- It is also one of the extensively used commands for verification of messages, labels, errors etc. displayed on the web pages.
Syntax:
1 | String Text = driver.findElement(By.id(“Text”)).getText(); |
How to select the value in a drop-down?
- Value in the drop down can be selected using WebDriver’s Select class.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | selectByValue: Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”))); selectByValue.selectByValue(“value”); selectByVisibleText: Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”))); selectByVisibleText.selectByVisibleText(“text”); selectByIndex: Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”))); selectByIndex.selectByIndex(2); |
What are the different types of navigation commands?
Following are the navigation commands:
- navigate().back():– The above command requires no parameters and takes back the user to the previous webpage in the web browser’s history.
1 | driver.navigate().back(); |
- navigate().forward():– This command lets the user to navigate to the next web page with reference to the browser’s history.
1 | driver.navigate().forward(); |
- navigate().refresh()– This command lets the user to refresh the current web page there by reloading all the web elements.
1 | driver.navigate().refresh(); |
- navigate().to()– This command lets the user to launch a new web browser window and navigate to the specified URL.
1 | driver.navigate().to(“https://stqatools.com”); |
How to click on a hyper link using linkText?
1 | driver.findElement(By.linkText(“Google”)).click(); |
- The command finds the element using link text and then click on that element and thus the user would be re-directed to the corresponding page.
1 | driver.findElement(By.partialLinkText(“Goo”)).click(); |
Selenium WebDriver Questions and Answers for Interview
How to handle Iframe in WebDriver?
- iFrame is a HTML document embedded inside an HTML document.
- iFrame is defined by an <iframe></iframe> tag in HTML.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Select iframe by id: driver.switchTo().frame(“ID of the frame“); Locating iframe using tagName: driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0)); Locating iframe using index: frame(index): driver.switchTo().frame(0); frame(Name of Frame): driver.switchTo().frame(“name of the frame”); Select Parent Window: driver.switchTo().defaultContent(); |
When do we use findElement() and findElements()?
findElement(): findElement() is used to find the first element in the current web page matching to the specified locator value. Take a note that only first matching element would be fetched.
Syntax:
1 | WebElement element = driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”)); |
findElements(): findElements() is used to find all the elements in the current web page matching to the specified locator value. Take a note that all the matching elements would be fetched and stored in the list of WebElements.
Syntax:
1 | List <WebElement> elementList = driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”)); |
How to find more than one web element in the list?
- Sometimes we have need to fetch multiple links / images / records to verify particular record is exist or not?
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class ElementList { // Storing the list of element List<WebElement> elementList = driver.findElements(By.xpath("//ul//li")); // Fetching the size of the list int listSize = elementList.size(); for(int i = 0;i<listSize;i++) { // Clicking on each link serviceProviderLinks.get(i).click(); // Navigating back driver.navigate().back(); } } |
What is the difference between driver.close() and driver.quit() command?
- close (): WebDriver’s close() method closes the currently working or currently access window by WebDriver.
- quit (): Unlike close() method, quit() method closes down all the windows that the program has opened.
How can we handle web based pop up?
WebDriver handle pop up using Alert interface.
Four methods that we would be use along with the Alert interface.
- void accept()– The accept() method clicks on the “Ok” button as soon as the pop up window appears.
- void dismiss()– The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.
- String getText()– The getText() method returns the text displayed on the alert box.
- void sendKeys(String stringToSend)– The sendKeys() method enters the specified string pattern into the alert box.
Syntax:
1 2 3 4 5 | // accepting alert Alert alert = driver.switchTo().alert(); alert.accept(); |
How can we handle windows based pop up?
- There are several third party tools available for handling window based pop ups along with the selenium like AutoIT, Robot class etc.
How to mouse hover on a web element using WebDriver?
- Using Action Interface to mouse hover on a drop down which then opens a list of options.
Sample Code:
1 2 3 4 5 6 7 8 9 10 | // Action Interface Actions actions=new Actions(driver); // howering on the dropdown actions.moveToElement(driver.findElement(By.id("id of the dropdown"))).perform(); // Clicking on one of the items in the list options WebElement subLinkOption=driver.findElement(By.id("id of the sub link")); subLinkOption.click(); |
How to retrieve css properties of an element?
- The values of the css properties can be retrieved using a get() method:
Syntax:
1 2 3 | driver.findElement(By.id(“id“)).getCssValue(“name of css attribute”); driver.findElement(By.id(“id“)).getCssValue(“font-size”); |
What is a framework?
- Framework is a constructive blend of various guidelines, coding standards, concepts, processes, practices, project hierarchies, modularity, reporting mechanism, test data injections etc. to pillar automation testing.
What are the advantages of Automation framework?
Advantage of Test Automation framework
- Reusability of code.
- Maximum coverage.
- Recovery scenario.
- Low cost maintenance.
- Minimal manual intervention.
- Easy Reporting.
Selenium WebDriver Questions and Answers for Interview
What are the different types of frameworks?
- Data Driven Testing Framework: Data Driven Testing Framework helps the user segregate the test script logic and the test data from each other. It lets the user store the test data into an external database. The data is conventionally stored in “Key-Value” pairs. Thus, the key can be used to access and populate the data within the test scripts.
- Keyword Driven Testing Framework: The Keyword driven testing framework is an extension to Data driven Testing Framework in a sense that it not only segregates the test data from the scripts, it also keeps the certain set of code belonging to the test script into an external data file.
- Hybrid Testing Framework: Hybrid Testing Framework is a combination of more than one above mentioned frameworks. The best thing about such a setup is that it leverages the benefits of all kinds of associated frameworks.
- Behavior Driven Development Framework: Behavior Driven Development framework allows automation of functional validations in easily readable and understandable format to Business Analysts, Developers, Testers, etc.
Can captcha be automated?
- No, captcha and bar code reader cannot be automated.
What is Object Repository? How can we create Object Repository in Selenium?
- Object Repository is a term used to refer to the collection of web elements belonging to Application under Test (AUT) along with their locator values.
- Thus, whenever the element is required within the script, the locator value can be populated from the Object Repository.
- Object Repository is used to store locators in a centralized location instead of hard coding them within the scripts.
- In Selenium, objects can be stored in an excel sheet which can be populated inside the script whenever required.
How do you read data from excel?
1 2 3 4 5 6 7 | 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(); |
How do you verify if the checkbox/radio is checked or not ?
- We can use isSelected() method.
Syntax :
1 | 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.
How do you handle alert pop-up ?
- To handle alert pop-ups, we need to 1st switch control to alert pop-ups then click on ok or cancle then move control back to main page.
Syntax-
1 2 3 4 5 6 7 8 9 10 11 12 13 | String mainPage = driver.getWindowHandle(); // to move control to alert popup Alert alt = driver.switchTo().alert(); // to click on ok alt.accept(); // to click on cancel alt.dismiss(); //Then move the control back to main web page driver.switchTo().window(mainPage); |
How to perform right click using WebDriver?
Ans- Use Actions class
1 2 3 4 5 6 | // where driver is WebDriver type Actions act = new Actions(driver); act.moveToElement(webElement).perform(); act.contextClick().perform(); |
How do perform drag and drop using WebDriver?
Ans- Use Action class
1 2 3 4 5 6 7 8 9 | Actions act = new Actions(driver); //source ele which you want to drag WebElement source = driver.findElement(By.xpath(“ -----”)); //target where you want to drop WebElement target = driver.findElement(By.xpath(“ -----”)); act.dragAndDrop(source,target).perform(); |
Selenium WebDriver Questions and Answers for Interview
How do you upload a file?
- To upload a file we can use sendKeys() method.
1 | driver.findElement(By.xpath(“input field”)).sendKeys(“path”); |
Selenium WebDriver Questions and Answers for Interview
How do you get the current page URL ?
1 | driver.getCurrentUrl(); |
Selenium WebDriver Questions and Answers for Interview
Write the code for Reading and Writing to Excel through Selenium ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | FileInputStream Excel_File = new FileInputStream(“Excel file path”); Workbook wb = WorkbookFactory.create(Excel_File); Sheet s = wb.getSheet("sheetName"); // read data of Cell String value = s.getRow(rowNum).getCell(cellNum).getStringCellValue(); // write data in Cell s.getRow(rowNum).getCell(cellNum).setCellValue("value to be set"); FileOutputStream fos = new FileOutputStream(“path of file”); // save file wb.write(fos); |
How to get typed text from a textbox ?
- Use getAttribute(“value”) method by passing arg as value.
1 | String typedText = driver.findElement(By.xpath("xpath of box")).getAttribute("value")); |
Selenium WebDriver Questions and Answers for Interview
What are the different exceptions you got when working with WebDriver ?
- ElementNotVisibleException
- ElementNotSelectableException
- NoAlertPresentException
- NoSuchAttributeException
- NoSuchWindowException
- TimeoutException
- WebDriverException
Selenium WebDriver Questions and Answers for Interview
What are the languages supported by WebDriver ?
- Python, Ruby, C# and Java are all supported directly by the development team. There are also webdriver implementations for PHP and Perl.
Selenium WebDriver Questions and Answers for Interview
How do you clear the contents of a textbox in selenium ?
Use clear() method.
1 | driver.findElement(By.xpath("xpath of box")).clear(); |
Selenium WebDriver Questions and Answers for Interview
What are the prerequisites to run selenium webdriver?
- JDK, Eclipse, WebDriver(selenium standalone jar file), browser, application to be tested.
How to get the number of frames on a page ?
1 2 3 | List <WebElement> framesList = driver.findElements(By.xpath("//iframe")); int numOfFrames = frameList.size(); |
Selenium WebDriver Questions and Answers for Interview
How do you simulate scroll down action ?
Use java script to scroll down-
1 2 3 4 5 6 7 | JavascriptExecutor jsx = (JavascriptExecutor)driver; //scroll down, value 4500 you can change as per your req jsx.executeScript("window.scrollBy(0,4500)", ""); //scroll up jsx.executeScript("window.scrollBy(450,0)", ""); |
Selenium WebDriver Questions and Answers for Interview
How to check if an element is visible on the web page ?
- Use isDisplayed() method. The return type of the method is boolean. So if it return true then element is visible else not visible.
1 | driver.findElement(By.xpath("xpath of elemnt")).isDisplayed(); |
Selenium WebDriver Questions and Answers for Interview
How to check if a button is enabled on the page ?
- Use isEnabled() method. The return type of the method is boolean. So if it return true then button is enabled else not enabled.
1 | driver.findElement(By.xpath("xpath of button")).isEnabled(); |