Test Automation
Test Automation
/Qasim Nazir
On Weekend
WebDriver Interface:
List<WebElement> links =
driver.findElements(By.cssSelector("a"));
8. Methods of WebDriver?
o Some common methods are: get(), getCurrentUrl(), getTitle(),
findElement(), findElements(), getPageSource(), close(), quit(),
navigate(), manage().
9. Who is the parent of WebDriver?
o SearchContext is the parent interface of WebDriver.
10. How do you download/install WebDriver? Explain the steps.
o Download the WebDriver executable from the official site of the respective
browser (e.g., ChromeDriver for Chrome).
o Extract the executable.
o Set the path to the WebDriver executable in your code:
System.setProperty("webdriver.chrome.driver",
"path/to/chromedriver");
/Qasim Nazir
On Weekend
/Qasim Nazir
On Weekend
23. How do you capture a screenshot for a failed test script? Write syntax.
o Use TakesScreenshot interface:
24. When do we go for findElements method and what is the return type?
o Use findElements when you expect multiple elements. It returns a list of
WebElements.
25. What is IllegalStateException? When do we get it?
o IllegalStateException is thrown when a method has been invoked at an illegal
or inappropriate time. In Selenium, it can occur if the WebDriver executable is
not set correctly.
26. Why do we get WebDriverException?
o This exception is thrown when WebDriver is unable to interact with the browser.
Possible reasons include incorrect WebDriver setup, browser crashes, or network
issues.
27. Does WebDriver have any constructor?
o No, WebDriver is an interface and does not have a constructor.
28. Disadvantages of WebDriver?
o Does not support Windows-based application testing.
o Limited support for mobile testing (use Appium for that).
o Requires programming knowledge.
29. We have 4 tabs in the browser currently I am in parent tab how do you switch into
4th tab?
o Use getWindowHandles() to get a list of window handles and switch:
/Qasim Nazir
On Weekend
element.sendKeys("text");
41. How do you pass data into a text field without using sendKeys()?
o Use JavaScript:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='text';", element);
42. How do you click on the WebElement without using click() method?
o Use JavaScript:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
/Qasim Nazir
On Weekend
43. How will you check whether the element is displayed or not?
o Use the isDisplayed() method:
44. How will you check whether the element is enabled or not?
o Use the isEnabled() method:
45. How will you check whether the element is selected or not?
o Use the isSelected() method:
46. How do you get all the links of the current web page?
o Use the CSS selector a:
List<WebElement> links =
driver.findElements(By.cssSelector("a"));
/Qasim Nazir
On Weekend
52. How will you handle scroll up and scroll down webpage?
o Use JavaScript:
// Scroll down
js.executeScript("window.scrollBy(0,1000)");
// Scroll up
js.executeScript("window.scrollBy(0,-1000)");
Synchronization:
/Qasim Nazir
On Weekend
Locators:
/Qasim Nazir
On Weekend
Pop-Ups:
76. How do you identify whether it is an alert pop-up or not? How do you handle it?
o Selenium automatically switches to alert pop-ups. Use
driver.switchTo().alert() to handle alerts.
77. Can I inspect Alert pop-up?
o No, JavaScript alerts cannot be inspected.
78. Is Alert an interface or class? Explain the methods of it.
o Alert is an interface. Methods include accept(), dismiss(), getText(), and
sendKeys().
79. How do you identify whether it is a hidden-division pop-up or not? How do you
handle it?
o Hidden-division pop-ups are regular DOM elements. Use WebDriver methods to
interact with them.
80. How do you identify whether it is a notification pop-up or not? How do you handle
it?
o Notification pop-ups are handled by the browser. You can use WebDriver to
interact with the notification permissions.
81. How do you handle authentication pop-up?
o Pass credentials in the URL:
driver.get("http://username:[email protected]");
driver.switchTo().alert().sendKeys("text");
DesiredCapabilities capabilities =
DesiredCapabilities.internetExplorer();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,
true);
Runtime.getRuntime().exec("path/to/autoit/script.exe");
/Qasim Nazir
On Weekend
Drop Down:
String alertText = driver.switchTo().alert().getText();
90. How will you identify if a dropdown is single select or multi select?
o Use isMultiple() method of the Select class:
92. WebDriver driver = new ChromeDriver(); Select select = new Select(driver); True
or false? Justify.
o False. Select constructor requires a WebElement, not a WebDriver instance.
93. How do you handle if the dropdown options are dynamic?
o Use explicit waits to wait for options to be populated.
94. How will you handle dropdown without using Select class?
o Click on the dropdown and then click on the desired option using WebDriver
methods.
95. How will you get all the options from dropdown?
o Use getOptions() method of the Select class:
/Qasim Nazir
On Weekend
105. How do you de-select the selected option from the dropdown? - Use
deselectByVisibleText(), deselectByIndex(), or deselectByValue() methods of
the Select class.
106. How do you de-select all the selected options from the dropdown? - Use
deselectAll() method of the Select class.
107. We have two dropdowns in a web page. How will you compare all the options
of one dropdown with all the options of another dropdown? - Get the options of both
dropdowns using getOptions() method and compare their text values.
Actions Class:
109. How to perform double click on the WebElement? - Use Actions class:
111. How will you perform drag and drop action? - Use Actions class:
112. How do you move the cursor to a specific WebElement? - Use Actions class:
113. Can I perform keyboard actions with the help of Actions class? - Yes, use
methods like sendKeys() in the Actions class.
114. What is the difference between perform() and build()? - build(): Compiles
the actions into a single step. - perform(): Executes the actions.
115. What happens if I do not call perform() method? - The actions will not be
executed.
116. Write a syntax to double-click on WebElement using Actions class.
Actions actions = new Actions(driver); actions.doubleClick(element).perform();
117. How will you perform keyboard action? Write syntax. -
Actions actions = new Actions(driver); actions.sendKeys(Keys.ENTER).perform();
/Qasim Nazir
On Weekend
Frames:
118. How to work with frame-window? - Use switchTo().frame() method:
driver.switchTo().frame(frameElement);
119. How to work with nested frames? - Switch to the outer frame first, then to the
inner frame.
120. How to work with multiple frames of a web page? - Use switchTo().frame()
with different frame references.
121. How many ways to work with frames? - By index, by name or ID, and by
WebElement.
122. How to work with frame, when frame does not have id & @name attribute? -
Use WebElement reference.
123. How do you identify frame in a web page? - Inspect the page to find the
<iframe> or <frame> tags.
124. What is the action of defaultContent()? - Switches the context to the main
document from a frame.
125. What is the action of parentFrame()? - Switches to the parent frame of the
current frame.
Data-driven:
126. What is data-driven framework? - A framework where test data is driven from
external data sources like Excel, CSV, or databases.
127. Why do we go for Excel? - Excel is user-friendly, supports complex data
structures, and is easy to manipulate.
128. What is POI? - Apache POI is a Java API for manipulating Microsoft documents
like Excel.
129. How do you get the data from Excel sheet? Write syntax. -
FileInputStream fis = new FileInputStream("path/to/excel"); Workbook workbook =
WorkbookFactory.create(fis); Sheet sheet = workbook.getSheetAt(0); Row row =
sheet.getRow(0); Cell cell = row.getCell(0); String data = cell.getStringCellValue();
130. How do you set the data into Excel sheet? Explain with syntax. -
FileOutputStream fos = new FileOutputStream("path/to/excel"); Cell cell =
row.createCell(1); cell.setCellValue("data"); workbook.write(fos);
/Qasim Nazir
On Weekend
TestNG:
144. What is TestNG? - TestNG is a testing framework inspired by JUnit and NUnit,
designed for testing needs in Java.
145. Why do we go for TestNG? - It provides powerful features like annotations,
parallel execution, and test configuration.
146. Why do we call TestNG as unit testing tool? - Because it allows writing and
running unit tests.
147. What are the ways we have to install TestNG? - Install as a plugin in Eclipse or
add it as a dependency in Maven/Gradle.
148. How do you install TestNG? Explain with steps. - In Eclipse: Go to Help >
Eclipse Marketplace > Search for TestNG > Install. - Maven: Add the TestNG
dependency to pom.xml.
149. When do we get TestNGException? Why? - When there are issues with TestNG
configuration or when tests fail to execute properly.
150. Uses of TestNG? - Organizing tests, setting test priorities, data-driven testing,
generating reports, and parallel execution.
151. Is TestNG an interface or class? - TestNG is a class.
152. Explain the annotations of TestNG? - Common annotations include @Test,
@BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @BeforeSuite,
@AfterSuite.
153. Explain the order of execution of all the annotations? - @BeforeSuite,
@BeforeTest, @BeforeClass, @BeforeMethod, @Test, @AfterMethod, @AfterClass,
@AfterTest, @AfterSuite.
154. How do you perform group execution using TestNG? - Use groups attribute in
@Test and configure groups in testng.xml.
155. How do you perform batch execution using TestNG? - Define tests in
testng.xml and execute them together.
156. How do you perform controlled batch execution using TestNG? - Use
dependsOnMethods and dependsOnGroups attributes to control test execution order.
/Qasim Nazir
On Weekend
157. What do you mean by parallel execution? - Running multiple tests concurrently
to save time.
158. What do you mean by thread-count? - Specifies the number of threads to be
used for parallel execution.
159. How do you perform parallel execution using TestNG? - Set parallel and
thread-count attributes in testng.xml.
160. I have 200 test cases, what is your approach if I want to execute them
parallelly in two different browsers? - Define two <test> tags in testng.xml, each
specifying a different browser.
161. I have 200 test cases, what is your approach if I want to execute them
parallelly in the same browser? - Use the parallel="methods" attribute in
testng.xml.
162. How do you run test cases at the class level? - Use @Test annotation at the class
level.
163. How do you run test cases at the package level? - Define the package in
testng.xml.
164. How do you run all the test cases at the project level? - Use the testng.xml
file at the project root or use Maven/Gradle build tools.
165. How do you run a single test case? - Use testng.xml to define a single test or
run it directly from the IDE.
166. Why do we go for @DataProvider annotation? - For parameterizing tests and
providing multiple sets of data to a test method.
167. How do you perform cross-browser testing? - Use WebDriver capabilities and
configure browsers in testng.xml.
168. How do you run the same test case 100 times? - Use invocationCount
attribute in the @Test annotation:
@Test(invocationCount = 100)
169. How will you create dependency between two test cases? - Use
dependsOnMethods attribute in the @Test annotation.
170. How do you skip a test case? - Use enabled=false attribute in the @Test
annotation:
@Test(enabled = false)
171. Is it possible to skip a test case without using enabled? - Yes, use throw new
SkipException("Skipping test") in the test method.
172. How do you prioritize the test cases? - Use priority attribute in the @Test
annotation:
@Test(priority = 1)
/Qasim Nazir
On Weekend
173. Why do we need to prioritize the test cases? - To define the order of test
execution.
174. How do you convert all the test cases into testNG.xml file? - Use the TestNG
Eclipse plugin to generate testng.xml or manually create the file.
175. What is the use of testNG.xml file? - It is used to configure and organize the
execution of test cases.
176. Can I create multiple testNG.xml files? - Yes, you can create multiple
testNG.xml files for different test suites.
177. Can I create multiple testNG.xml files with the same name? - No, file names
must be unique within the same directory.
178. Can I run testNG.xml file? - Yes, you can run it directly from the IDE or
through the command line.
179. Can I run multiple .xml files at the same time? - Yes, you can specify multiple
XML files to be run together.
180. What is the advantage of TestNG report? - It provides detailed HTML reports
of test execution with logs and results.
181. How do you execute only failed test cases? - Use testng-failed.xml
generated by TestNG after a test run.
182. What do you know about include and exclude? - Used in testng.xml to
include or exclude specific test methods or groups.
183. How do you configure testng.xml file to perform parallel execution? - Set
the parallel attribute in testng.xml to tests, classes, or methods.
184. Can I run testng.xml file through command prompt? - Yes, use the
command:
185. Why do we use log() method? - To log messages during test execution.
186. I will run 100 test cases but 10 got failed. How do you run only those failed
test cases? - Use testng-failed.xml generated after the initial run.
187. Advantages of TestNG over JUnit? - More annotations, parallel execution,
flexible test configuration, detailed reports, and data-driven testing support.
POM:
188. What is POM? - Page Object Model (POM) is a design pattern to create object
repositories for web UI elements.
189. Why do we go for POM? - To enhance code reusability, maintainability, and
readability by encapsulating page elements and actions.
190. Explain the rule of POM class design? - Each page of the application should
have a corresponding class. This class should contain WebElements and methods to
interact with those elements.
191. Why/when do we get StaleElementReferenceException? - When a
WebElement is no longer attached to the DOM.
192. How do you handle StaleElementReferenceException? - Re-locate the
element, use retry logic, or wait until the DOM is stable.
193. Is it possible to handle StaleElementReferenceException without POM
/Qasim Nazir
On Weekend
196. Difference between findBy, findBys and findAll? - findBy: Finds a single
element. - findBys: Finds elements matching all specified criteria. - findAll: Finds
elements matching any of the specified criteria.
197. Why do we need to follow encapsulation rule to design POM class? - To
ensure that the WebElements are accessed only through methods in the POM class,
enhancing maintainability and readability.
198. Why do we need to call initElements() method only inside the constructor?
- To initialize the elements as soon as the object of the POM class is created.
General:
199. Why do we go for automation? - To save time, reduce human errors, increase
test coverage, and perform repetitive tasks efficiently.
200. What is automation testing? - Automation testing involves using tools and
scripts to perform tests on software applications automatically.
201. Can I automate hardware-related test cases? - Generally, no. Automation
testing focuses on software applications.
202. What is the prerequisite for automation? - A stable application, test cases,
automation tools, and a testing environment.
203. What is not automatable? - Highly dynamic UI changes, complex human
interactions, and non-deterministic tasks.
204. Advantages and disadvantages of Selenium IDE? - Advantages: Easy to use,
record-and-playback, no programming required. - Disadvantages: Limited functionality,
not suitable for complex test cases, no support for programming logic.
205. Advantages and disadvantages of Selenium RC? - Advantages: Supported
multiple browsers and platforms, allowed programming logic. - Disadvantages: Requires
server to start, slower than WebDriver, outdated.
206. Any idea of GitHub? - GitHub is a web-based platform for version control and
collaboration using Git. It allows multiple developers to work on projects simultaneously,
track changes, and manage repositories.
207. Any idea of Maven? - Maven is a build automation tool used primarily for Java
projects. It helps manage project dependencies, build processes, and project
documentation.
208. Any idea of Jenkins? - Jenkins is an open-source automation server that helps in
continuous integration and continuous delivery (CI/CD) of software projects. It
automates the building, testing, and deployment of applications.
/Qasim Nazir