Selenium
Selenium
Ans. Selenium is a robust test automation suite that is used for automating web-based applications. It
supports multiple browsers, programming languages, and platforms.
Selenium WebDriver – Selenium WebDriver is used to automate web applications by directly calling the
browser’s native methods.
The Selenium IDE Plugin – Selenium IDE is an open-source test automation tool that works on record
and playback principles.
Selenium RC component – Selenium Remote Control(RC) is officially deprecated by Selenium and it used
to work using javascript to automate the web applications.
Selenium Grid – Allows Selenium tests to run in parallel across multiple machines.
Selenium is open source and free to use without any licensing cost.
Using Selenium IDE component, non-programmers can also write automation scripts.
Using the Selenium Grid component, distributed testing can be carried out on remote machines.
For creating robust scripts in Selenium Webdriver, programming language knowledge is required.
Also, we have to rely on external libraries and tools for performing tasks like – logging(log4J), testing
framework-(TestNG, JUnit), reading from external files(POI for excels), etc.
Firefox – FireFoxDriver
Safari – SafariDriver
HtmlUnit (Headless browser) – HtmlUnitDriver
Android – Selendroid/Appium
IOS – ios-driver/Appium
Ans. No. Selenium WebDriver uses the browser’s native method to automate the web applications. So,
there is no support for testing web services using Selenium WebDriver.
Id
XPath
CSS selector
className
tagName
name
link text
partialLinkText
Ques.8. How can we inspect the web element attributes in order to use them in different locators?
Ans. In order to locate web elements, we can use the Developer tool and plugins like Firebug.
The developer tool can be launched by pressing F12 on the browser. Users can easily hover over any
element and find its different HTML properties.
Firebug is a plugin of Firefox that provides various development tools for debugging applications. From
an automation perspective, Firebug is used specifically for inspecting web elements in order to find their
attributes like id, class, name, etc. in different locators.
Ans. Xpath or XML path is a query language that is used for selecting nodes from XML documents. Also,
it is one of the locators supported by Selenium Webdriver.
Ans. An absolute XPath is a way of locating an element using an XML expression, beginning from the
root node i.e. HTML node in the case of web pages.
The main disadvantage of absolute XPath is that even if there is a slight change in the UI or any element
then also whole XPath will fail.
Example – html/body/div/div[2]/div/div/div/div[1]/div/input
Ans. A relative XPath is a way of locating an element using an XML expression, starting from anywhere in
the HTML document.
In this way, there are different ways of creating robust relative XPaths that are unaffected by changes in
other UI elements.
Example – //input[@id=’username’]
Ques.12. What is the difference between single slash(/) and a double slash(//) in XPath?
Ans. In XPath, a single slash is used for creating absolute XPaths, beginning from the root node. Whereas
double slash is used for creating relative XPaths.
Ques.13. How can we locate an element by only partially matching the value of its attributes in Xpath?
Ans. Using contains() method we can locate an element by partially matching its attribute’s value. This is
particularly helpful in scenarios where the attributes have dynamic values with a certain constant part.
Basically, the above statement will match all the values of the name attribute containing the word ‘user’
in them.
xPathExpression = //*[text()='username']
Ans. Using ‘/..’ after the XPath expression of the child element, we can move to the parent of an
element.
For example, the locator //div[@id=”childId”]/.. will move to the parent of the div element with id value
as ‘childId’.
Ans. Basically, there are two ways of navigating to the nth element using XPath-
Using position()-
Ans. By using .className in the CSS locator, we can select all the elements belonging to a particular class
e.g. ‘.red’ will select all elements having class ‘red’.
Ans. By using #idValue in the CSS locator, we can select all the elements belonging to a particular class
e.g. ‘#userId’ will select the element having an id – userId.
Ques.19. How can we select elements by their attribute value using the CSS Selector?
Ans. Using [attribute=value] in the CSS locator, we can select all the elements belonging to a particular
class e.g. ‘[type=small]’ will select the element having attribute type of value ‘small’.
Ques.20. How can we move to the nth-child element using the CSS selector?
Ans. Using :nth-child(n) in the CSS locator, we can move to the nth child element e.g. div:nth-child(2) will
locate 2nd div element of its parent.
Ques.21. What is the fundamental difference between XPath and CSS selectors?
Ans. The fundamental difference between XPath and CSS selector is – using XPaths we can traverse up in
the document i.e. we can move to parent elements. Whereas using the CSS selector, we can only move
downwards in the document.
Ans. By creating an instance of the desired browser driver e.g. below command will initialize the Firefox
browser.
Ques.23. What is the use of driver.get(“URL”) and driver.navigate().to(“URL”) commands? Is there any
difference between the two?
Ans. Both driver.get(“URL”) and driver.navigate().to(“URL”) commands are used to navigate to a URL
passed as parameter.
driver.navigate() allows moving back and forward in browser history with the help of
driver.navigate().forward() and driver.navigate().back() commands.
In the case of single-page applications (where the URL is appended by ‘#’ to navigate to different
sections of the page), driver.navigate().to() navigates to a particular section by changing the URL without
refreshing the page whereas driver.get() refreshes the page also.
This refreshing of the page is also the primary reason because of which history is not maintained in the
case of the driver.get() command.
Ans. With the help of sendKeys() method we can type text in a textbox-
searchTextBox.sendKeys("searchTerm");
Ans. In order to delete the text written in a textbox, we can use the clear() method.
driver.findElement(By.id("elementLocator")).clear();
Ans. The same click() method used for clicking buttons or radio buttons can be used for checking the
checkbox as well.
driver.findElement(By.id("form1")).submit();
Also, the click() method can be used for the same purpose.
The getWindowHandles() command returns a list of ids corresponding to each window. If we pass a
particular window handle to the driver.switchTo().window(“{windowHandleName}”) command then we
can switch control/focus to that particular window.
driver.switchTo().window(handle);
}
Ques.30. What is the difference between driver.getWindowHandle() and driver.getWindowHandles()
in Selenium?
Ans. The driver.getWindowHandle() returns a handle of the current window (a single unique identifier).
Ans. The driver.switchTo() commands can be used for switching to a particular iframe.
driver.switchTo().frame("{frameIndex/frameId/frameName}");
For locating a frame, we can either use the index (starting from 0), its name, or its Id.
Ques.32. Can we move back and forward in the browser using Selenium?
Ans. We can maximize the browser window using the following command-
driver.manage().window().maximize();
Ans. Using the getText() method we can fetch the text over an element.
Ques.36. How can we find the value of different attributes like name, class, value of an element?
Ans. Using getAttribute(“{attributeName}”) method, we can find the value of different attributes of an
element e.g.-
String valueAttribute =
driver.findElement(By.id("locator")).getAttribute("value");
driver.manage().deleteAllCookies();
Ans. An implicit wait is a type of wait that waits for a specified time while locating an element before
throwing NoSuchElementException. By default, Selenium tries to find web elements immediately when
required without any wait. So, it is good to use implicit wait. This wait is applied to all the elements of
the current driver instance.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Ans. An explicit wait is a type of wait that is applied to a particular web element until the expected
condition specified is met.
It is advisable to use explicit waits over implicit waits because higher timeout value of implicit wait (set
for handling only some of the elements) gets applied to all the web elements. Thus increasing the overall
execution time of the script. On the other hand, we can apply different timeouts to the different
elements in case of explicit waits.
Check our detailed tutorial here – Implicit & Explicit Waits in Selenium.
Ques.40. What are some expected conditions that can be used in Explicit waits?
Ans. Some of the commonly used expected conditions of an element that can be used with explicit waits
are-
stalenessOf(WebElement element)
visibilityOf(WebElement element)
visibilityOfElementLocated(By locator)
invisibilityOfElementLocated(By locator)
alertIsPresent()
titleContains(String title)
titleIs(String title)
textToBePresentInElementLocated(By, String)
Ques.41. What is a fluent wait?
Ans. A fluent wait is a type of wait in which we can also specify polling interval (the time intervals after
which driver will try to find the elements when not located) along with the maximum timeout value.
.withTimeout(20, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
return driver.findElement(By.id("textBoxId"));
);
Ques.42. What are the different keyboard operations that can be performed in Selenium?
Ans. The different keyboard operations that can be performed in Selenium are-
.pressKey(“non-text keys”) – Used for keys like control, function keys etc that are non-text.
.releaseKey(“non-text keys”) – Used in conjunction with keypress event to simulate releasing a key from
keyboard event.
Ques.43. What are the different mouse actions that can be performed using Selenium?
click(WebElement element)
doubleClick(WebElement element)
contextClick(WebElement element)
mouseDown(WebElement element)
mouseUp(WebElement element)
mouseMove(WebElement element)
mouseMove(WebElement element, long xOffset, long yOffset)
WebElement element=driver.findElement(By.id("elementId"));
action.doubleClick(element).perform();
WebElement element=driver.findElement(By.id("elementId"));
action.contextClick(element).perform();
WebElement element=driver.findElement(By.id("elementId"));
action.moveToElement(element).perform();
Ans. In order to fetch the current page URL, we can use the getCurrentURL() command.
driver.getCurrentUrl();
Ans. Using driver.getTitle() command, we can fetch the page title in Selenium. This method returns a
string containing the title of the webpage.
Ans. Using the driver.getPageSource() command, we can fetch the page source in selenium. This method
returns a string containing the page source.
Ans. Tooltips web elements have an attribute of type ‘title’. By fetching the value of the ‘title’ attribute,
we can verify the tooltip text in selenium.
String toolTipText = element.getAttribute("title");
Ans. Using linkText() and partialLinkText() methods, we can locate a link. The difference between the
two is – linkText() matches the complete string passed as a parameter to the link texts. Whereas
partialLinkText() only matches the string parameter partially.
Ans. Desired capabilities are a set of key-value pairs that are used for storing or configuring browser-
specific properties. For example – browser’s version, platform, etc in the browser instances.
Ans. All the links are of anchor tag ‘a’. So by locating elements of tagName ‘a’ we can find all the links on
a webpage.
NoAlertPresentException – When we try to switch to an alert box but the targetted alert is not present.
NoSuchFrameException – When we try to switch to a frame but the targetted frame is not present.
NoSuchWindowException – When we try to switch to a window but the targetted window is not
present.
InvalidElementStateException – When the state of an element is not appropriate for the desired action.
NoSuchAttributeException – When we are trying to fetch an attribute’s value but the attribute is not
correct.
WebDriverException – When there is some issue with the driver instance preventing it from getting
launched.
Ans. In order to take screenshots in Selenium, we can use the getScreenshotAs method of the
TakesScreenshot interface.
dropdown.selectByVisibleText("India");
dropdown.selectByIndex(1);
dropdown.selectByValue("Ind");
Ans. Using is Selected() method, we can check the state of a dropdown’s option.
dropdown.selectByVisibleText("India");
System.out.println(driver.findElement(By.id("India")).isSelected());
Ans. Using the isDisplayed() method we can check if an element is getting displayed on a web page.
driver.findElement(By locator).isDisplayed();
Ques.59. How can we check if an element is enabled for interaction on a web page?
Ans. Using the isEnabled method, we can check if an element is enabled or not.
driver.findElement(By locator).isEnabled();
findElement() returns a single WebElement (found first) based on the locator passed as a parameter.
Whereas findElements() returns a list of WebElements, all satisfying the locator value passed.
Syntax of findElement()-
Another difference between the two is- if no element is found then findElement() throws
NoSuchElementException whereas findElements() returns a list of 0 elements.
Ques.61. How can we handle window UI elements and window POP ups using selenium?
Ans. Selenium is used for automating web-based applications only(or browsers only). If we want to
handle window GUI elements then we can use tools like AutoIT.
AutoIT is a freeware used for automating window GUI. The AutoIt scripts follow simple BASIC language
like syntax. Also, it can be easily integrated with Selenium tests.
robot.keyPress(KeyEvent.VK_ENTER);
Using element.sendKeys(“path of file”) on the webElement of input tag and type file i.e. the elements
should be like –
Ques.64. How to handle the HTTPS website in Selenium or how to accept the SSL untrusted
connection?
Ans. Using profiles, we can handle accept the SSL untrusted connection certificate. Profiles are basically
set of user preferences stored in a file.
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
Ans. Using Action class, drag and drop can be performed in Selenium. Sample code-
.moveToElement(TargetElement)
.release(TargetElement)
.build();
dragAndDrop.perform();
Ans. JavaScript code can be executed in Selenium using JavaScriptExecuter. Sample code for javascript
execution-
((JavascriptExecutor)driver).executeScript("{JavaScriptCode}");
Ans. In order to accept or dismiss an alert box, the alert class is used. This requires first switching to the
alert box and then using accept() or dismiss() command as the case may be.
alert.accept();
alert.dismiss();
Ques.68. What is HtmlUnitDriver?
Ans. HtmlUnitDriver is the fastest WebDriver. Unlike other drivers (FireFoxDriver, ChromeDriver, etc),
the HtmlUnitDriver is non-GUI. On executing test scripts, no browser gets launched.
(JavascriptExecutor(driver))
.executeScript("document.getElementsByClassName(locator).click();");
Ans. Page Object Model(POM) is a design pattern in Selenium. A design pattern is a solution or a set of
standards that are used for solving commonly occurring software problems.
Now coming to POM – POM helps to create a framework for maintaining selenium scripts. In POM for
each page of the application, a class is created having the web elements belonging to the page and
methods handling the events on that page. The test scripts are maintained in separate files and the
methods of the page object files are called from the test scripts file.
Using POM, we can create an Object Repository i.e. a set of web elements in separate files along with
their associated functions. In this way, keeping the code clean.
For any change in UI(or web elements) only page object files are required to be updated leaving test files
unchanged.
Ans. Page factory is an implementation of the Page Object Model in Selenium. It provides @FindBy
annotation to find web elements. In addition, there is a PageFactory.initElements() method to initialize
all web elements defined with @FindBy annotation.
WebDriver driver;
@FindBy(id="search")
WebElement searchTextBox;
@FindBy(name="searchBtn")
WebElement searchButton;
//Constructor
this.driver = driver;
PageFactory.initElements(driver, this);
//Sample method
searchTextBox.sendKeys(searchTerm);
searchButton.click();
Ans. An object repository is the centralized location of all the objects or WebElements of the test scripts.
In Selenium, we can implement an object repository using the Page Object Model as well as Page
Factory design patterns.
Ans. A data-driven framework is one in which the test data is put in external files like CSV, Excel, etc.
Basically, the test data is separated from the test logic that is written in test script files. The test data
drives the test cases, i.e. the test methods run for each set of test data values.
TestNG provides inherent support for data-driven testing using @dataProvider annotation.
Ans. A keyword-driven framework is one in which the normal set of actions are associated with
keywords and are kept in external files usually in tabular form.
For example, an action of launching a browser will be associated with keyword – launchBrowser(), action
to write in a textbox with keyword – writeInTextBox(webElement, textToWrite), etc.
The code to perform the action based on a keyword specified in the external file is implemented in the
framework itself.
In this way, the test steps can be written in a file by a person of a non-programming background also
(provided all the used keywords are implemented in the framework).
Ques.76. What is a hybrid framework?
Ans. A hybrid framework is a combination of two or more frameworks. For example, a combination of
data-driven and keyword-driven frameworks can be considered as a hybrid framework.
Ans. Selenium Grid is a tool that helps in the distributed testing. Using Grid, we can run test scripts in
different machines having different browsers, browser versions, platforms, etc in parallel. In the
Selenium grid, there is a hub that is a central server managing all the distributed machines known as
nodes.
It allows running test cases in parallel thereby saving test execution time.
Multi-browser testing is possible using the Selenium grid by running the test on machines having
different browsers.
Also, we can do multi-platform testing by configuring nodes having different operating systems.
Ans. A hub is a server or a central point in the Selenium grid that controls the test executions on the
different machines.
Ans. Nodes are the machines that are attached to the selenium grid hub and have selenium instances
running the test scripts. Unlike a hub, there can be multiple nodes in the selenium grid.
Ans. In the line of code Webdriver driver = new FirefoxDriver(); ‘WebDriver’ is an interface and we are
creating an object of type WebDriver instantiating an object of FirefoxDriver class.
Ques.82 What is the purpose of creating a reference variable- ‘driver’ of type WebDriver instead of
directly creating a FireFoxDriver object or any other driver’s reference in the statement Webdriver
driver = new FirefoxDriver();?
Ans. By creating a reference variable of type WebDriver, we can use the same variable to work with
multiple browsers like ChromeDriver, IEDriver, etc.
Ques.83. Name an API used for reading and writing data to excel files.
Ans. Apache POI API and JXL(Java Excel API) can be used for reading, writing, and updating excel files.
Ques.84. Name an API used for logging in Java.
Ans. Log4j is an open-source API widely used for logging in Java. It supports multiple levels of logging like
– ALL, DEBUG, INFO, WARN, ERROR, TRACE, and FATAL.
Ans. Logging helps in debugging the tests when required and also provides storage of the test’s runtime
behavior.
Ans. TestNG(NG for Next Generation) is a testing framework that can be integrated with Selenium or any
other automation tool. Moreover, it provides multiple capabilities like assertions, reporting, parallel test
execution, etc.
TestNG provides different assertions that help in checking the expected and actual results.
We can define the dependency of one test method over others in TestNG.
@BeforeSuite – The annotated method will run only once before all tests in this suite have run.
@AfterSuite -The annotated method will run only once after all tests in this suite have run.
@BeforeClass – The annotated method will run only once before the first test method in the current
class is invoked.
@AfterClass – The annotated method will run only once after all the test methods in the current class
have been run.
@BeforeTest – The annotated method will run before any test method belonging to the classes inside
the <test> tag is run.
@AfterTest – The annotated method will run after all the test methods belonging to the classes inside
the <test> tag have run.
@BeforeMethod – The annotated method will run before each test method marked by @Test
annotation.
@AfterMethod – The annotated method will run after each test method marked by @Test annotation.
@DataProvider-The @DataProvider annotation is used to pass test data to the test method. The test
method will run as per the number of rows of data passed via the data provider method.
assertEquals(String actual, String expected, String message) – (and other overloaded data type in
parameters)
assertNotEquals(double data1, double data2, String message) – (and other overloaded data type in
parameters)
assertNotNull(Object object)
true(String message)
Ans. A testng.xml file is used for configuring the whole test suite. In this file, we can create a test suite,
create test groups, mark tests for parallel execution, add listeners, and pass parameters to test scripts.
Later, this testng.xml file can be used for triggering the test suite.
Ques.91. How can we pass the parameter to test script using TestNG?
Ans. Using @Parameter annotation and ‘parameter’ tag in testng.xml we can pass parameters to the
test script.
Sample testng.xml –
<suite name="sampleTestSuite">
<test name="sampleTest">
<classes>
</test>
</suite>
@Test
@Parameters("sampleParamName")
Ans. Using @DataProvider we can create a data-driven framework. Basically, we can pass test data to
the associated test method and then multiple iterations of the test run for the different test data values
passed from the @DataProvider method. The method annotated with @DataProvider annotation return
a 2D array of object.
@DataProvider(name = "dataProvider1")
//This method is bound to the above data provider returning 2D array of 3*2 matrix
//The test case will run 3 times with different set of values
@Test(dataProvider = "dataProvider1")
It has methods like onTestSuccess, onTestFailure, onTestSkipped, etc. We need to implement this
interface creating a listener class of our own. After that using the @Listener annotation, we can specify
that for a particular test class, a customized listener class should be used.
@Listeners(PackageName.CustomizedListenerClassName.class)
@Test
//test logic
Ques.94. How can we make one test method dependent on others using TestNG?
Ans. Using the dependsOnMethods parameter inside @Test annotation in TestNG, we can make one
test method run only after the successful execution of the dependent test method.
@Test(dependsOnMethods = { "preTests" })
Ans. Using the priority parameter in @Test annotation in TestNG we can define the priority of test cases.
The default priority of the test when not specified is integer value 0. Example-
@Test(priority=1)
Ans. The default priority of a test when not specified is integer value 0. So, if we have one test case with
priority 1 and one without any priority then the test without any priority value will get executed first.
Ans. A Test method can be disabled from getting executed by setting the “enabled” attribute as false.
@Test(enabled = false)
//Test logic
}
//Test logic
Ans. In order to run the tests in parallel just add these two key-value pairs in the suite-
parallel=”{methods/tests/classes}”
Ans. @Factory annotation helps in the dynamic execution of test cases. Using @Factory annotation, we
can pass parameters to the whole test class at run time. The parameters passed can then be used by one
or more test methods of that class.
For example – there are two classes TestClass and the TestFactory class. Because of the @Factory
annotation, the test methods in class TestClass will run twice with the data “k1” and “k2”.
//Constructor
this.str = str;
@Test
System.out.println(str);
}
//The test methods in class TestClass will run twice with data "k1" and "k2"
@Factory
Ans. @Factory method creates instances of test class and runs all the test methods in that class with a
different set of data.
Whereas, @DataProvider is bound to individual test methods and run the specific methods multiple
times.