We are going to learn about the TOP 51+ Selenium Selenium Java Interview Questions and Answers. These are exact questions asked for the beginners and experience people.
You can download the selenium java interview questions and answers for experienced as PDF also in below link.
1
Contents
Question #1 : What is Selenium WebDriver?
It is an automation framework that allows you to execute your tests against different browsers.
WebDriver also enables you to use a programming language in creating your test scripts
Java, .Net, PHP, Python, Ruby, Perl
It supports Chrome ,Firefox ,Opera , Safari , IE , HTML unit driver(headless)
2
Question #2 : What are the different types of exceptions you have faced in Selenium WebDriver?
- NoSuchElementException
- NoSuchWindowException
- NoSuchFrameException
- NoAlertPresentException
- ElementNotVisibleException
- ElementNotSelectableException
- TimeoutException
3
Question #3 : What is implicit wait in Selenium WebDriver?`
The implicit wait will tell the WebDriver to wait a certain amount of time before it throws a “No Such Element Exception.”
The default setting of implicit wait is zero. Once you set the time, the web driver will wait for that particular amount of time before throwing an exception.
Syntax:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
4
Question #4 : What is WebDriver Wait in Selenium WebDriver?
Explicit waits are a concept from the dynamic wait, which waits dynamically for specific conditions. It can be implemented by the WebDriverWait class
Syntax:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“input”)));
5
Question #5 : What is Fluent Wait in Selenium WebDriver?
Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Syntax:
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id(“element”));
}
}
6
Question #6 : How to Input Text into a value by Javascript?
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById("<<inputbox_id>>").value='new value’”);
7
Question #7 : How to get an attribute value of an element using Selenium WebDriver?
driver.findElement(By.Id(“button_id”)).getAttribute(“text”);
8
Question #8 : How to Send Keyboard keys like press Enter key on text box in Selenium WebDriver?
driver.findElement(By.Id(“button_id”)).sendKeys(keys.ENTER);
9
driver.get() is used to navigate particular URL(website) and wait till page load.
driver.navigate() is used to navigate to particular URL and does not wait to page load. It maintains browser history or cookies to navigate back or forward.
10
Question #10 : How to pause a test execution for 5 seconds at a specific point?
We can pause test execution for 5 seconds by using the wait command.
Syntax:
driver.wait(5);
11
Question #11 : What is Selenium Grid and when do we go for it?
Selenium Grid is used to run tests on different machines against different browsers in parallel. We use Selenium Grid in the following scenarios:
Execute your test on different operating systems
Execute your tests on different versions of same browser
Execute your tests on multiple browsers
Execute your tests in parallel and multiple threads
12
Question #12 : What is a Hub in Selenium Grid?
- Hub is the central point to the entire GRID Architecture which receives all requests.
There is only one hub in the selenium grid. Hub distributes the test cases across each node.
13
Question #13 : What is a Node in Selenium Grid?
There can be multiple nodes in Grid.
Tests will run in nodes.
Each node communicates with the Hub and performs test assigned to it.
14
Question #14 : Which WebDriver implementation claims to be the fastest?
HTML UnitDriver is the most lightweight and fastest implementation headless browser for of WebDriver. It is based on HtmlUnit.
It is known as Headless Browser Driver.
It is same as Chrome, IE, or FireFox driver, but it does not have GUI so one cannot see the test execution on screen.
15
Question #15 : What are the open source frameworks supported by Selenium WebDriver?
- TestNG
- JUnit
- Cucumber
- Robot Framework
- Appium
- Protractor
16
- navigate().to();
navigate().forward();
navigate().back();
navigate().refresh();
17
Question #17 : How can we Maximize browser window in Selenium WebDriver?
- driver.manage().window().maximize();
18
Single Cookie Delete by Name
driver.manage.deleteCookieNamed(“hello”);
All the cookies
driver.manage().deleteAllCookies();
19
Question #19 : What is the difference between driver.getWindowHandle() and driver.getWinowHandles() in Selenium WebDriver and their return type?
driver.getWindowHandle() – To get the window handle of the current window. Returns a string of alphanumeric window handle
driver.getWinowHandles() – To get the window handle of all current windows. Return a set of window handles
20
We can use the JavaScriptExecutor to handle hidden elements.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('displayed-text').value=texthHidden");
21
Question #21 : How to find more than one web element in the list?
We can find more than one web element by using the findElements() method in Selenium.
List<WebElement> elements = driver.findElements(By.tagName(“a”));
22
Question #22 : How will you find an element using Selenium?
- ID
- Name
- Tag
- Attribute
- CSS
- Linktext
- PartialLink Text
- XPath etc
23
Question #23 : What is the difference between verify and assert commands?
Assert: In other words, the test will terminated at the point where check fails.
Verify: In verification, all the commands are going to run guaranteed even if any of test fails.
24
Question #24 : Explain what are the JUnits annotation linked with Selenium?
@Before public void method() – It will perform the method () before each test, this method can prepare the test
@Test public void method() – Annotations @Test identifies that this method is a test method environment
@After public void method()- To execute a method before this annotation is used, test method must start with test@Before
25
Question #25 : Explain what is DataDriven framework and Keyword driven?
Data Driven : The test data is separated and kept outside the Test Scripts, while Test Case logic resides in Test Scripts.
Keyword Driven : The functionality of the application under test is documented in a table as well as step by step instructions for each test.
26
Question #26 : How you can switch between frames?
To switch between frames webdrivers [ driver.switchTo().frame() ] method takes one of the three possible arguments
A number: It selects the number by its (zero-based) index
A number or ID: Select a frame by its name or ID
Previously found WebElement: Using its previously located WebElement select a frame
27
Question #27 : How you can perform double click ?
Actions act = new Actions (driver);
act.doubleClick(webelement);
28
Question #28 : How to read a JavaScript variable in Selenium WebDriver?
JavascriptExecutor js = (JavascriptExecutor)driver;
//Click on button using JS
js.executeScript("arguments[0].click();", button);
29
Question #29 : How to wait for Element to visible by a AJAX call?
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
30
Question #30 : What is Page Object Model (POM) ?
Page Object Model is a design pattern for creating an object repository for web UI elements.
Each web page in the application is required to have its own corresponding page class.
The page class is thus responsible for finding the WebElements in that page and then perform operations on those web elements.
31
Question #31 : What is Page Factory?
Page Factory class in Selenium is an extension to the Page Object Design pattern. It is used to initialize the elements of the page object or instantiate the page objects itself.
Annotations in Page Factory are like this:
@FindBy(id = “userName”)
WebElement txt_UserName;
PageFactory.initElements(driver, Login.class);
32
Question #32 : What is Object Repository? How can we create an Object Repository in Selenium?
Collection of web elements belonging to Application Under Test (AUT) along with their locator values.
Whenever the element is required within the script, the locator value can be populated from the Object Repository.
Objects can be stored in an excel sheet, Page Object Class.
33
Question #33 : How can I read test data from excels?
You can use the JXL or POI API.
https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
34
Question #34 : What is a Framework?
Its a Set of guidelines, coding standards, concepts, processes, practices, project hierarchies, modularity, reporting mechanism, test data injections etc. to pillar automation testing.
35
Question #35 : How to capture screenshot in WebDriver?
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import org.apache.commons.io.FileUtils;
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Code to copy the screenshot in the desired location
FileUtils.copyFile(scrFile, new File("C:\\failscast1.jpg"))
}
36
Question #36 : How to get the Custom attribute value?
driver.findElement(By.id(“inputid“)).getCssValue(“font-size”);
driver.findElement(By.id(“buttonid“)).getAttribute(“data”);
<input id=”inputid” style=”font-size:19px”/>
<button id=”buttonid” data=”hello”>Button</button>
37
Question #37 : How to Drag and Drop from Ifram1 to 2?
driver.switchTo().frame(0); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//your xpath"));
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(4000);
driver.switchTo().defaultContent(); //Move outside to the frame.
driver.switchTo().frame(1); //Move inside to another frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement to = driver.findElement(By.id("guide_RIGHT_SAFETY_rect"));
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent();
38
Question #38 : How can we handle web-based pop-up?
1) void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.
driver.switchTo().alert().dismiss();
2) void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.
driver.switchTo().alert().accept();
3) String getText() – The getText() method returns the text displayed on the alert box.
driver.switchTo().alert().getText();
4) void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.
driver.switchTo().alert().sendKeys("Text");
39
Question #39 : What is the difference between driver.close() and driver.quit command?
close(): WebDriver’s close() method closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the WebDriver. The command neither requires any parameter nor does it return any value.
quit(): Unlike close() method, quit() method closes down all the windows that the program has opened. Same as close() method, the command neither requires any parameter nor does is return any value.
40
Question #40 : How to select value in a dropdown?
selectByValue:
Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
selectByValue.selectByValue(“greenvalue”);
selectByVisibleText:
Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));
selectByVisibleText.selectByVisibleText(“Lime”);
selectByIndex:
Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”)));
selectByIndex.selectByIndex(2);
41
Question #41 : How to Use Selenium Expected Conditions?
Selenium WebDriver comes with a lot of expected conditions such as:
ExpectedCondition < WebElement > elementToBeClickable(By locator)
ExpectedCondition < Boolean > elementToBeSelected(By locator)
ExpectedCondition < WebElement > presenceOfElementLocated(By locator)
ExpectedCondition < Boolean > titleContains(String title)
ExpectedCondition < Boolean > titleIs(String title)
ExpectedCondition < Boolean > urlContains(String fraction)
ExpectedCondition < Boolean > urlToBe(String url)
ExpectedCondition < WebElement > visibilityOfElementLocated(By locator)
There are 2 main expected condition types that can be used with explicit waits:
WebElement searchButton = wait.until(elementToBeClickable(searchButtonXpath));
searchButton.click();
if (!wait.until(titleContains(resultsTitle)) || !wait.until(urlContains(resultsUrl)))
throw new RuntimeException("results page is not displayed");
43
Question #43 : What are the limitations of Selenium?
Selenium does not support testing desktop apps or native OS features.
Selenium supports testing of only web based applications.
Captcha and Bar code readers cannot be tested using Selenium.
Selenium does not provide the Support of Reports. We have to use third party tools like TestNG or Junit.
44
Question #44 : When should we use Selenium Grid?
- Selenium Grid is used to execute the test cases in different machines across different platforms
- Selenium Grid supports Distributed test execution.
45
Question #45 : How to Test local storage of browser with Selenium?
Selenium provides the Javascript Executor functions.
You can use to get and set the local storage with this method.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“return window.localStorage.getItem('key');”
46
Question #46 : How to Handle Iframe within Iframe?
You can use the command like
driver.switchTo(“iframid”).switchTo(“iframe”)
47
Question #47 : How to Start a Hub and node in Selenium Grid?
Start the Hub
java -jar selenium-server-standalone-3.14.jar -role hub
Connect Node to the Hub at localhost:4444
java –jar selenium-server-standalone-3.14.jar –role node –hub http://hubIP:4444/grid/register
48
Question #48 : How to Upload & Download a File using Selenium Webdriver?
driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
// enter the file path onto the file-selection input field
uploadElement.sendKeys("C:\\file.text");
49
Question #49 : How To HandleHandling Multiple Browser Windows in Selenium WebDriver?
Set<String> allWindows = driver.getWindowHandles();
String window1 = (String) allWindows.toArray()[0];
String window2 = (String) allWindows.toArray()[1];
driver.switchTo().window(window1);
50
Question #50 : How to initialize GeckoDriver and ChromeDriver?
Firefox Driver Set
Download Link : https://github.com/mozilla/geckodriver/releases
System.setProperty("webdriver.gecko.driver","C:\\Downloads\\geckodriver.exe");
Chrome Driver Set.
Download Link : https://chromedriver.chromium.org/
System.setProperty("webdriver.chrome.driver","C:\\Downloads\\chromedriver.exe");
51
Question 51 : What is the difference between Page Object Model and Page Factory?
Page Object Model is a design pattern to create an Object Repository for web UI elements.
Page Factory is a built-in class in Selenium for maintaining object repository.
#1
bonus #1 : How to upload a file in Selenium WebDriver?
driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
uploadElement.sendKeys("D:\\newhtml.txt");
Selenium Java Interview Questions and Answers || That Every QA should Know - Part 1
Selenium Java Interview Questions and Answers || That Every QA should Know - Part 2
Selenium Java Interview Questions and Answers || That Every QA should Know - Part 3
Selenium Java Interview Questions and Answers || That Every QA should Know - Part 4
Are you implementing the right Test automation practices ?
15 Best Practices and Strategies for Test Automation
👪 Join Facebook Our Community - https://scrolltest.com/fb
✅ Automation Community - https://thetestingacademy.com
🐦Follow us on Twitter - https://twitter.com/itstechmode
📖 Like us on Facebook - https://www.facebook.com/scrolltest
🎤 Listen to our Podcast - https://anchor.fm/thetestingacademy