100 Questions On Selenium Testing
100 Questions On Selenium Testing
com | 7263041604
1|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
It depends on Test case scenario complexity and length. I did automate 2-5
test scenarios per day when the complexity is limited. Sometimes just 1 or
fewer test scenarios in a day when the complexity is high.
6. What is a Framework?
If you are a beginner: You can say “No, I didn’t get a chance to create
framework from the scratch. I have used the framework which is already
available. My contribution is mostly in creating test cases by using the
existing framework.”
If you are a beginner but have good knowledge on creating framework: You
can say “Yes, I have involved in developing framework along with other
automation tester in my company.”
2|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
3|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
playback the scripts. Even though we can create scripts using Selenium IDE,
we need to use Selenium RC or Selenium WebDriver to write more advanced
and robust test cases.
Selenese is the language which is used to write test scripts in Selenium IDE.
Firefox
4|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
It allows running test cases in parallel thereby saving test execution time.
It allows multi-browser testing
It allows us to execute test cases on multi-platform
Node is the machine which is attached to the hub. There can be multiple
nodes in Selenium Grid.
• Firefox Driver
• Gecko Driver
• InternetExplorer Driver
• Chrome Driver
• HTMLUnit Driver
• Opera Driver
• Safari Driver
• Android Driver
• iPhone Driver
• EventFiringWebDriver
5|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
• Java
• C#
• Python
• Ruby
• Perl
• PHP
• Windows
• Linux
• Apple
• JUnit
• TestNG
1. ID –
2. ClassName –
3. Name –
4. TagName –
5. LinkText –
6. PartialLinkText –
7. XPath
8. CSS Selector –
6|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
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.
29. What is the difference between Absolute Path and Relative Path?
Absolute XPath starts from the root node and ends with desired descendant
element’s node. It starts with top HTML node and ends with input node. It
starts with a single forward slash(/) as shown below
1 /html/body/div[3]/div[1]/form/table/tbody/tr[1]/td/input
Relative XPath starts from any node in between the HTML page to the
current element’s node(last node of the element). It starts with a double
forward slash(//) as shown below.
1 //input[@id='email']
Assert: In simple words, if the assert condition is true then the program
control will execute the next test step but if the condition is false, the
execution will stop and further test step will not be executed.
Verify: In simple words, there won’t be any halt in the test execution even
though the verify condition is true or false.
7|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Soft Assert: Soft Assert collects errors during @Test Soft Assert does not
throw an exception when an assert fails and would continue with the next
step after the assert statement.
Note: If you use geckodriver with Selenium, you must upgrade to Selenium
3.3. Here we have to set the property as follows
System.setProperty("webdriver.gecko.driver", "D:\\Selenium
1
Environment\\Drivers\\geckodriver.exe");
8|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
SearchContext.
3 instead of creating
38. What are the different exceptions you have faced in Selenium
WebDriver?
1. ElementNotVisibleException
2. StaleElementReferenceException
9|P age
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
This exception will be thrown when you are trying to locate a particular
element on webpage that is not currently visible eventhough it is present in
the DOM. Also sometimes, if you are trying to locate an element with the
xpath which associates with two or more element.
A stale element reference exception is thrown in one of two cases, the first
being more common than the second.
We face this stale element reference exception when the element we are
interacting is destroyed and then recreated again. When this happens the
reference of the element in the DOM becomes stale. Hence we are not able
to get the reference to the element.
• WebDriverException
• IllegalStateException
• TimeoutException
• NoAlertPresentException
• NoSuchWindowException
• NoSuchElementException
1 http://username:password@url
2 e.g. http://myUserName:[email protected]
10 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
In Selenium we could see three types of waits such as Implicit Waits, Explicit
Waits and Fluent Waits.
• Implicit Waits –
• Explicit Waits –
• Fluent Waits
Implicit waits tell to the WebDriver to wait for a certain amount of time
before it throws an exception. Once we set the time, WebDriver will wait for
the element based on the time we set before it throws an exception. The
default setting is 0 (zero). We need to set some wait time to make
WebDriver to wait for the required time.
Practical example
FluentWait can define the maximum amount of time to wait for a specific
condition and frequency with which to check the condition before throwing
an “ElementNotVisibleException” exception.
44. How to input text in the text box using Selenium WebDriver?
2 driver.get("https://www.gmail.com");
45. How to input text in the text box without calling the sendKeys()?
11 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
1 // To initialize js object
2 JavascriptExecutor JS = (JavascriptExecutor)webdriver;
3 // To enter username
4 JS.executeScript("document.getElementById('User').value='SoftwareTestingMaterial.com'");
5 // To enter password
6 JS.executeScript("document.getElementById('Pass').value='tester'");
1 package softwareTestingMaterial;
2 import org.openqa.selenium.By;
3 import org.openqa.selenium.WebDriver;
4 import org.openqa.selenium.chrome.ChromeDriver;
5 import org.testng.annotations.Test;
12 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
8 @Test
10 System.setProperty("webdriver.chrome.driver", "D:\\Selenium
11 Environment\\Drivers\\chromedriver.exe");
13 driver.get("https://www.google.com");
14 String availableText =
15 driver.findElement(By.xpath("//*[@id='gbw']/div/div/div[1]/div[1]/a")).getText();
By using getAttribute(value);
HTML:
1 <input name="nameSelenium" value="valueSelenium">SoftwareTestingMaterial</input>
Selenium Code:
3 Output: valueSelenium
13 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
1 driver.findElement(By.id("form_1")).submit();
Alternatively, you can use click method on the element which does form
submission
1 Thread.sleep(5000)
1 driver.get("https://www.softwaretestingmaterial.com");
14 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
driver.get(): To open an URL and it will wait till the whole page gets loaded
driver.navigate.to(): To navigate to an URL and It will not wait till the whole
page gets loaded
1 driver.getCurrentUrl();
1 driver.manage().window().maximize();
15 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
1 driver.manage().deleteAllCookies();
16 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
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()
3 dropdown.selectByVisibleText(Text);
4 dropdown.selectByIndex(Index);
17 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
5 dropdown.selectByValue(Value);
Practical Example:
Test cases may fail while executing the test scripts. While we are executing
the test cases manually we just take a screenshot and place in a result
repository. The same can be done by using Selenium WebDriver.
i. Application issues
ii. Assertion Failure
iii. Difficulty to find Webelements on the web page
iv. Timeout to find Webelements on the web page
18 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
4 //Mouseover on an element
5 action.moveToElement(ele).perform();
Practical Example.
To handle alerts popups we need to do switch to the alert window and call
Selenium WebDriver Alert API methods.
Practical Example.
(JavascriptExecutor(driver)).executeScript("document.getElementsByClassName(ElementLoc
1
ator).click();");
73. How can you find Broken Links in a page using Selenium
WebDriver?
74. How to find more than one web element in the list?
19 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
5 //for loop
7 {
9 links.get(i).click();
11 driver.navigate().back();
12 }
By using JavascriptExecutor
1 driver.findElement(By.id("Id Value")).click();
20 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Sometimes web controls don’t react well against selenium commands and
we may face issues with the above statement (click()). To overcome such
kind of situation, we use JavaScriptExecutor interface.
2 js.executeScript(Script,Arguments);
Test data can efficiently be read from excel using JXL or POI API. POI API
has many advantages than JXL.
Click here to see a practical example of using Apache POI.
21 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
No, It’s not possible to automate captcha and bar code reader.
Handling AJAX calls is one of the common issues when using Selenium
WebDriver. We wouldn’t know when the AJAX call would get completed and
the page has been updated. In this post, we see how to handle AJAX calls
using Selenium.
AJAX stands for Asynchronous JavaScript and XML. AJAX allows the web
page to retrieve small amounts of data from the server without reloading the
entire page. AJAX sends HTTP requests from the client to server and then
process the server’s response without reloading the entire page. To handle
AJAX controls, wait commands may not work. It’s just because the actual
page is not going to refresh.
When you click on a submit button, the required information may appear on
the web page without refreshing the browser. Sometimes it may load in a
second and sometimes it may take longer. We have no control over loading
time. The best approach to handle this kind of situations in selenium is to
use dynamic waits (i.e. WebDriverWait in combination with
ExpectedCondition)
1. titleIs() – The expected condition waits for a page with a specific title.
1 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
1 wait.until(ExpectedConditions.alertIsPresent()) !=null);
22 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
wait.until(ExpectedConditions.textToBePresentInElement(By.id(“title’”), “text to be
1
found”));
23 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
model (POM) can be used in any kind of framework such as modular, data-
driven, keyword driven, hybrid framework etc. A page object is an object-
oriented class that serves as an interface to a page of your Application Under
Test(AUT). The tests then use the methods of this page object class
whenever they need to interact with the User Interface (UI) of that page.
The benefit is that if the UI changes for the page, the tests themselves don’t
need to change, only the code within the page object needs to change.
Subsequently, all changes to support that new UI is located in one place.
85. What is the difference between Page Object Model (POM) and
Page Factory?
Page Object is a class that represents a web page and hold the functionality
and members.
Page Factory is a way to initialize the web elements you want to interact
with within the page object when you create an instance of it.
Object Repository – Each page will be defined as a java class. All the fields
in the page will be defined in an interface as members. The class will then
implement the interface.
24 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
87. How can you use the Recovery Scenario in Selenium WebDriver?
1 try {
2 driver.get("www.SoftwareTestingMaterial.com");
3 }catch(Exception e){
4 System.out.println(e.getMessage());
5}
There are two cases which are majorly used to upload a file in Selenium
WebDriver such as using SendKeys Method and using AutoIT Script.
Practical Example.
Practical Example
90. How to run Selenium WebDriver Test from the command line?
1 driver.switchTo().frame();
25 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
We use JDBC Driver to connect the Database in Selenium (While using Java
Programming Language).
Practical Example
Practical Example
Practical Example
Practical Example
Practical Example
26 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Practical Example
Practical Example
27 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Sometimes, we may face a situation to get the data from the Database or to
modify (update/delete) the data from the Database. If we plan to automate
anything outside the vicinity of a browser, then we need to use other tools to
achieve our task. To achieve the Database connection and work on it, we
need to use JDBC API Driver.
The Java Database Connectivity (JDBC) API provides universal data access
from the Java programming language. Using the JDBC API, you can access
virtually any data source, from relational databases to spreadsheets and flat
files. It lets the user connect and interact with the Database and fetch the
data based on the queries we use in the automation script. JDBC is a SQL
level API that allows us to execute SQL statements. It creates a connectivity
between Java Programming Language and the database.
1 driver.Manage().Cookies.DeleteAllCookies();
28 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Here we have dealt with some important TestNG interview questions. If you
want to learn more interview questions related to TestNG then here you go.
We have a special post on TestNG Interview Questions. Also, you could
find TestNG Complete Tutorial here
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test
106. What is TestNG Assert and list out some common Assertions
supported by TestNG?
TestNG Asserts help us to verify the condition of the test in the middle of the
test run. Based on the TestNG Assertions, we will consider a successful test
only if it is completed the test run without throwing any exception.
29 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
• assertFalse(condition, message)
We use priority attribute to the @Test annotations. In case priority is not set
then the test scripts execute in alphabetical order.
1 package TestNG;
2 import org.testng.annotations.*;
4 @Test(priority=0)
7 }
8 @Test(priority=1)
11 }
12 }
Output:
30 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
1 Test Case 1
2 Test Case 2
Parameterized tests allow developers to run the same test over and over
again using different values.
Groups are specified in your testng.xml file and can be found either under
the <test> or <suite> tag. Groups specified in the <suite> tag apply to all
the <test>tags underneath.
3 System.out.println("Logged in successfully");
4}
Ans. TestNG listeners are used to configure reports and logging. One of the
most widely used listeners in TestNG is ITestListener interface. It has
31 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
Practical Example
1 @DataProvider(name="getData")
9 data[1][0] = "SecondUid";
10 data[1][1] = "SecondPWD";
11
12 return data;
13
14 }
32 | P a g e
Created By: TechDrills Solution | www.techdrills.in | [email protected] | 7263041604
2 options.addArguments("disable-infobars");
33 | P a g e