Core Java (For Automation Testers) – Interview Questions and Answers – Part 1

1. What is OOPS?

Object Oriented Programming System is the programming technique to write programs based on the real world objects. The states and behaviors of an object are represented as the member variables and methods. In OOPS programming programs are organized around objects and data rather than actions and logic.

2. What are the advantages of OOPS concepts?

Major advantages of OOPS programming are;
1. Simplicity: OOPS programming objects model real world objects, so the complexity is reduced and the program structure is clear.
2. Modularity: Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
3. Modifiability: It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
4.  Extensibility: Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
5.  Maintainability: Objects can be maintained separately, making locating and fixing problems easier.
6.  Reusability: Objects can be reused in different programs1

3. What are the core concepts of OOPS?

OOPS core concepts are:
1. Abstraction
2. Encapsulation
3. Polymorphism
4. Inheritance
5. Composition
6. Association
7. Aggregation

4. Explain JDK,JRE and JVM in short.

JVM
JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification.
JVMs are available for many hardware and software platforms (so JVM is platform dependent).
JRE
JRE stands for Java Runtime Environment. It is the implementation of JVM.
JDK
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.

5. What is constructor?

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

6. What is the purpose of default constructor?

The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class

7. Does constructor return any value?

Ans:yes, that is current instance (You cannot use return type yet it returns a value)

8. Is constructor inherited?

No, constructor is not inherited.

9. Can you make a constructor final?

No, constructor can’t be final.

10. What is static variable?
  •  static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  •  static variable gets memory only once in class area at the time of class loading.
11. What is static block?
  • Is used to initialize the static data member.
  • It is executed before main method at the time of class loading.
12. Can we execute a program without main() method?

Yes, one of the way is static block.

13. What is final variable?

If you make any variable as final, you cannot change the value of final variable(It will be constant).

14. What is final method?

Final methods can’t be overriden.

15. What is final class?

Final class can’t be inherited.


Continue to: CORE JAVA (FOR AUTOMATION TESTERS) – INTERVIEW QUESTIONS AND ANSWERS – PART 2

Selenium Interview Question and Answers – Part 1

On this page, you can find Basic Selenium Interview questions and answers which may benefit to freshers to experienced professional. This is part 1 of this interview questions series.

Very Basic Interview Questions –

1  What types of Testing are supported by selenium?

Functional Testing and regression testing

2. When should we go for selenium Grid?

Selenium Grid is used for test execution and not for test case design. It is used to execute test cases on same or different platforms on multiple machines parallel.

3. What is latest version of Selenium?

Selenium 3

4. What is difference between single slash (/) and double slash(//)?

/ – denotes that it is absolute Xpath ,that is starting from root node of the webpage
//- denotes it is relative Xpath, meaning, can starts from anywhere in the page.

5. What are different types of locators in selenium webdriver?

Id, class, Xpath, CSS, Name, Tagname, linkText, partial LinkText etc.

6. What is difference between assert and verify?

Assert – Test case terminated once assert fails.
verify – Test case is not terminated even if verify fails.

7. What are different types of drivers available in selenium WebDriver?

Firefox Driver, IEDriver, Chrome Driver, Safari Driver, HtmlUnitDriver etc.

 

 

Refer Part 2 onwards for advanced Interview Questions.

Selenium Interview Question and Answers – Part 3

In this Article, you can find Selenium Interview question and answers – Part 2. This is in continuation of Part2.

Selenium Interview Questions and Answers

11. How to handle alerts and confirmation boxes in Selenium WebDriver?.

Selenium provides an API to handle this. Alert alert().
Below are the methods present in Alert API, which are used to handle Alerts
void accept(): This is equivalent to the OK button action on the dialog. The corresponding OK button actions are invoked when the accept() action is taken on a dialog.
void dismiss():This is equivalent to clicking on the CANCEL action button.
java.lang.S tring getText(): This will return the text that appears on the dialog. This can be used if you want to evaluate the text on the modal dialog.
void sendKeys(java.lang.S tring keysToSend): This will allow the developer to type in some text into the alert if the alert has some provision for it.

12. How to handle colours in Selenium Webdriver ?

Get the value of a given CSS property. Color values should be returned as rgba strings, so, for example if the “background-color” property is set as “green” in the HTML source, the returned value will be “rgba(0, 255, 0, 1)”.

13. How to press shift +tab in Selenium?

public class SendKeys
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q")); searchBox.sendKeys(Keys.chord(Keys.SHIFT, "packt publishing")); }
}

14. How to take a screenshot in selenium?

WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere 

FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

15. Is there a way to click hidden link in Selenium WebDriver?

There is a easier way to work around the problem using JavascriptExecutor.
For example:

document.getElementsByClassName('post-tag')[0].click();

The above javascript would click on the “Selenium” tag on the top right of this page (next to your question), even if it were hidden.
All you need to do is issue this JS instruction via the JavascriptExecutor interface like so:

(JavascriptExecutor(webdriver)).executeScript("document.getElementsByClassName('post-tag')[0].click();");

16. What are the technical challenges that you faced with selenium?

Answer to this question differ person to person. Do tell all the challenges in real which you faced.

17. What is an Xpath in Selenium?

Xpath is used to locate a web element based on its XML path. XML stands for Extensible Markup Language and is used to store, organize and transport arbitrary data. It stores data in a key-value pair which is very much similar to HTML tags. Both being markup languages and since they fall under the same umbrella, Xpath can be used to locate HTML elements.

18. How to refresh a page without using context click?

1.Using sendKeys.Keys method

driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);

2.Using navigate.refresh() method

driver.get("url");
driver.navigate().refresh();

3.Using navigate.to() method

driver.get("url");
driver.navigate().to(driver.getCurrentUrl());

4.Using get() method

driver.get("url");
driver.get(driver.getCurrentUrl());

5.Using sendKeys() method

driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");

19. How to handle autocomplete box in web driver?

driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword"); 
Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator")); listItems.get(0).click();
driver.findElement(By.id("your searchButton")).click();

20. Difference between the getWindowHandle and getWindowHandles.

getWindowHandles() –  Syntax: Set getWindowHandles()
Example: driver.getWindowHandles();
Purpose: Return a set of window handles which can be used to iterate over all the open windows of this Webdriver instance  by passing them to switchTo().WebDriver.Options.window() Returns: A set of window handles which can be used to iterate over all the open windows.

getWindowHandle() – Syntax: String getWindowHandle()
Example: driver.getWindowHandle();
Parameter: Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date.

 

Refer below posts for next interview questions –

Selenium Interview Questions and Answers – Part 1

Selenium Interview Question and Answers – Part 3

Selenium Interview Question and Answers – Part 4

Selenium Interview Question and Answers – Part 5

Selenium Interview Questions and Answers – Part 2

On this page, you can find Selenium Interview questions and answers which may benefit to freshers to experienced professional. This is part 2 of this interview questions series.

1. How to Handle windows authentication popup using any third party tool .

Below are the steps to achieve this Using Autoit (Third Party Tool) –
Download and in Autoit editor write script through below command
ControlFocus(), ControlSet(), ControlClick()
Save file and Compile Script with your version and after that you get .exe file.
Java code for executing .exe file
Runtime.getRuntime().exec(\\Path)
It throws checked exception IOexception.

2. What are the different types of Exceptions in Selenium Webdriver?

Refer this post for detailed answer

3. What is StaleElementException?

StaleElementException occurs if I find an element, the DOM gets updated then I try to interact with the element.

4. Can we enter text without using sendKeys()?

Yes by using JavascriptExecutor

WebDriver driver = new FirefoxDriver();
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById("textbox_id").value='new value';);

5. How to find more than one web element in the list?

At times, we may come across elements of same type like multiple hyperlinks, images etc arranged in an ordered or unordered list. Thus, it makes absolute sense to deal with such elements by a single piece of code and this can be done using WebElement List.
Sample Code:

// Storing the list
List <WebElement> elementList = driver.findElements(By.xpath("//div[@id='example']//ul//li"));

// Fetching the size of the list 
int listSize = elementList.size(); 
for (int i=0; i<listSize; i++)
{
// Clicking on each service provider link 

serviceProviderLinks.get(i).click();

// Navigating back to the previous page that stores link to service providers 
driver.navigate().back();
}

6. 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 is 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.

7. Difference between findElement/findElements ?

findElement () will return only single WebElement and if that element is not located or we use some wrong selector then it will throw NoSuchElement exception.
findElements() will return List of WebElements – for this we need to give locator in such a way that it can find multiple elements and will return you list of webelements then using List we can iterate and perform our operation.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class findElementDemo {
public static void main(String[] args) throws Exception {
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://google.com");
Thread.sleep(5000);
List<WebElement> links=driver.findElements(By.xpath(".//*[@id='menu-top']/li")); System.out.println("Total element is "+links.size());
Iterator<WebElement> i1=links.iterator();
while(i1.hasNext)
WebElement ele1=i1.next();
String name=ele1.getText();
System.out.println("Elements name is "+name);
}
}
}

8. Difference between assert and verify in selenium Webdriver

When an “assert” fails, the test will be aborted. Assert is best used when the check value has to pass for the test to be able to continue to run log in.
Where if a “verify” fails, the test will continue executing and logging the failure.Verify is best used to check non critical things. Like the presence of a headline element.

9. How will you execute your login script using chrome browser from your editor using selenium?

System.setProperty("webdriver.chrome.driver","C:\\Users\\Public\\Documents\\Selenium\\chromedriver.exe"); 
WebDriver driver = new ChromeDriver();

10. Can we use multiple catch in try, how ?

Yes, we can use. Refer below Example –

public static void main(String args[]){ try{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception e)
{
System.out.println("common task completed");
}
 catch(ArithmeticException e) 
{
System.out.println("task1 is completed");
}
 catch(ArrayIndexOutOfBoundsException e)
 {
System.out.println("task 2 completed");
}
 System.out.println("rest of the code...");
}
}

Refer below posts for next interview questions –

Selenium Interview Questions and Answers – Part 2

Selenium Interview Question and Answers – Part 3

Selenium Interview Question and Answers – Part 4

Selenium Interview Question and Answers – Part 5