





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This guide offers interview questions and answers about Selenium WebDriver, a tool for automating web browser testing. It covers core concepts, configuration, element locators, and code examples for actions like form submission and screenshot capture. Advanced topics include verifying PDF content and HTTP response codes using Selenium with Java. Useful for students and professionals preparing for interviews or seeking a deeper understanding of Selenium for web application testing, it provides insights into best practices and common challenges. The content offers a concise overview of key concepts and techniques, enhancing the reader's ability to apply Selenium effectively. Code snippets illustrate testing functionalities, making it easier to grasp practical aspects.
Typology: Exams
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















Q 1: What is Selenium? Ans: Selenium is a suite of software tools to automate web browsers across many platforms (Different Operation Systems like MS Windows, Linux Macintosh etc.). It was launched in 2004, and it is open source Test Tool suite. Q 2: What is Selenium 2.0? Ans: Web testing tools Selenium RC and WebDriver are consolidated in single tool in Selenium 2. Selenium 1.0 + WebDriver = Selenium 2. Q 3: What is Selenium WebDriver? Ans: Selenium WebDriver is a tool for writing automated tests of websites. It is an API name and aims to mimic the behavior of a real user, and as such interacts with the HTML of the application. Selenium WebDriver is the successor of Selenium Remote Control which has been officially deprecated. Q 4: What is cost of WebDriver, is this commercial or open source? Ans: Selenium is an open source and free of cost. Q 5: How you specify browser configurations with Selenium 2.0? Ans: Following driver classes are used for browser configuration AndroidDriver,
ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver, InternetExplorerDriver,
implementation is fastest? Ans: HTMLUnitDriver. Simple reason is HTMLUnitDriver does not execute tests on browser but plain http request – response which is far quick than launching a browser and executing tests. But then you may like to execute tests on a real browser than something running behind the scenes Q 9: What all different element locators are available with Selenium 2.0? Ans: Selenium 2.0 uses same set of locators which are used by Selenium 1.0 – id, name, css, XPath but how Selenium 2.0 accesses them is different. In case of Selenium 1.0 you don’t have to specify a different method for each locator while in case of Selenium 2.0 there is a different method available to use a different element locator. Selenium 2.0 uses following method to access elements with id, name, css and XPath locator – driver.findElement(By.id("HTMLid"));
driver.findElement(By.name("HTMLname")); driver.findElement(By.cssSelector("cssLocator")); driver.findElement(By. className ("CalssName”)); driver.findElement(By. linkText ("LinkeText”)); driver.findElement(By. partialLinkText ("PartialLink”)); driver.findElement(By. tagName ("TanName”)); driver.findElement(By.xpath("XPathLocator)); Q 10: How do I submit a form using Selenium? Ans: WebElement el = driver.findElement(By.id("ElementID")); el.submit(); Q 11: How to capture screen shot in Webdriver? Ans: File file= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(file, new File("c:\name.png")); Q 12: How do I clear content of a text box in Selenium 2.0? Ans: WebElement el = driver.findElement(By.id("ElementID")); el.clear(); Q 13: How to execute java scripts
Below I have written a “CaptureElementClip.java“java webdriver test script of a google application where I capture google menu clip and save into project. package com.webdriver.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.Point; import
org.openqa.selenium.TakesScreenshot ; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; public class CaptureElementClip {
//get webelement object of google menu locator WebElement googleMenu = driver.findElement(By.id("gbz")); Point point = googleMenu.getLocation();
//get element dimension int width = googleMenu.getSize().getWidth(); int height = googleMenu.getSize().getHeight(); BufferedImage img = ImageIO.read(screen); BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width, height); ImageIO.write(dest, "png", screen); File file = new File("Menu.png"); FileUtils.copyFile(screen, file); } @AfterSuite public void tearDown() throws Exception { driver.quit(); } } Q 15: How to automate radio button in Selenium
//verfiy to radio button is check it return true if selected else false el.isSelected() Q 16: How to capture element image using Selenium 2.0? Ans: click link for answer: Q 17: How to count total number of rows of a table using Selenium 2.0? Ans: List {WebElement} rows = driver.findElements(By.className("//table[@id='tableID']/tr")); int totalRow = rows.size(); Q 18: How to capture page title using Selenium 2.0? Ans: String title = driver.getTitle() Q 19: How to store page source using Selenium 2.0? Ans: String pagesource = driver.getPageSource() Q 20: How to store current url using selenium 2.0? Ans: String currentURL = driver.getCurrentUrl() Q 21: How to assert text assert text of webpage using selenium 2.0? Ans:
WebElement el = driver.findElement(By.id("ElementID")); //get test from element and stored in text variable String text = el.getText(); //assert text from expected
using selenium 2.0? Ans: driver.manage().window().maximize(); Q 26: How to verify PDF content using selenium 2.0? Ans: I will explain the procedure to verify PDF file content using java WebDriver. As some time we need to verify content of web application PDF file, opened in browser. Use below code in your test scripts to get PDF file content.
//get current urlpdf file url URL url = new URL(driver.getCurrentUrl()); //create buffer reader object BufferedInputStream fileToParse = new BufferedInputStream(url.openStream()); PDFParserPDFParser = newPDFParser(fileToParse); PDFParser.parse(); //savePDF text into strong variable StringPDFtxt = newPDFTextStripper().getText(pdfParser.getPDDocument()); //closePDFParser object PDFParser.getPDDocument().close(); After applying above code, you can store allPDF file content into “pdftxt” string variable. Now you can verify string by giving input. As if you want to verify “Selenium or WebDiver” text. Use below code. Assert.assertTrue(pdftxt.contains(“Selenium or WebDiver”))
Html unit API is GUI less browser for java developer, using WebClent class we can send request to application server and verify response header status. Below code, I used in my webdriver script to verify response 200 of web application String url = "http://www.google.com/"; WebClient webClient = new WebClient(); HtmlPage htmlPage = webClient.getPage(url); //verify response Assert.assertEquals(200,htmlPage.getWebResponse().getStatusCod e()); Assert.assertEquals("OK",htmlPage.getWebResponse().getStatusMe ssage()); If HTTP authentication is required in web application use below code. String url = "Application Url";
WebClient webClient = new WebClient(); DefaultCredentialsProvider credential = new DefaultCredentialsProvider(); //Set some example credentials credential.addCredentials("UserName", "Passeord"); webClient.setCredentialsProvider(credential) ;