top of page
arrow.png
arrow_edited_edited.jpg

Selenium Interview Questions 

The commands used to get all the links present on the page is 
List <WebElement>  totalLinks=driver.findElements(By.xpath("//a"));
List<WebElement>  totalLinks=driver.findElements(By.tagName("a"));

The method used to terminate the browser/window/tab which is currently having the focus in selenium is?
driver.close();

When does WebDriver throw ElementNotVisibleException?
If the webdriver could not find any element in the DOM structure, it will throw 
ElementNotVisibleException exception.

What will be returned for an Element not found by 'findElements' method in selenium?
eg., driver.findElements(By.tagName("li"));

Ans: Return an Empty arraylist

How to refresh page without using context click?
driver.navigate().refresh();
driver.navigate.to(driver.getCurrentURL());

driver.get(driver.getCurrentURL());

Where in a constructor, can you place a call to constructor defined in the super class?
First statement in the constructor

Which locator is the most efficient way of identifying an element in selenium?
By.ID

True or Flase
Implicit wait time is applied to all elements present in the test script 
Where Explicit wait time is applied only for particular specified element
Answer:True

Implicit Wait

          WebDriver driver = new ChromeDriver();
         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
          driver.get("https://google.com/search");

Explicit wait

          WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
       
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='searchBox']")));

The WebDriver method used to change focus to an alert/a frame/a browser window is?
           driver.switchTo().window(childWindow);

           driver.switchTo().defaultContent();     //To go back to the main window(default window) .

            driver.switchTo().alert().accept();           

           driver.switchTo().alert().dismiss();           

           driver.switchTo().frame("frameName");
           driver.switchTo().frame("FrameID");

In webdriver, which command takes you forward by one page on the browser’s history?
driver.navigate().forward();

Which browser does support Selenium IDE?
Firefox

The command which retrieves the text of a html element in Selenium is ?
driver.findElement(By.xpath("//h1")).getText();

The command used to enter values onto text boxes is? 
driver.findElement(By.name("q").sendKeys("selenium");

In Selenium webdriver, which method is used to navigates to a URL?
driver.get("https://www.google.com/maps");

driver.navigate().to(https://www.google.com/maps");

What is the method that counts the number of elements?
driver.findElements(By.id("item")).size();

The valid 'select' class statements that select a value from a drop down element are ?
        selectByIndex(), 
       selectByValue(), 
       selectByVisibleText()

True/False
The Selenium can only test web applications
Answer: True

Which Component is used to run multiple tests simultaneously in different browsers and platforms?
Selenium Grid

What is selenium grid used for?

Used for distributed test execution i.e., 

Used to execute same or different test scripts on multiple platforms, browsers operating systems, and machines concurrently.

Benefits of Selenium Grid 

  • Allows to run the test scripts on multiple machines in parallel (different browsers & operating systems  as well different versions of each). 

  • Allows to run more tests in less time. 

  • Works by using a hub and nodes

  • hub is a central server takes the responsility to ditribute the test among the nodes based on their availability

  • Nodes/machines run the tests. When we run a test, the hub sends the test to the node that is best suited to run it. 

  • This is determined by the capabilities of the node and the capabilities of the test.

  • Helps to increase the test coverage

  • Running tests on multiple machines can improve the reliability of tests

  • Reduces the maintenance costs of testing environment

What is Selenium webdriver?

  • An API, provides the capability to interact with web applications

  • It is a Jar file which comes with client libraries (for language binding)

  • Client library can interact with server through JSON wire protocol

  • JSON wire protocol facilitates the communication between browser and code

Select an option from static dropdown

public class DropDownExample {

public static void main(String[] args) {

    WebDriver driver = new ChromeDriver( );

     driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

    driver.manage().window().maximize();

    driver.get("URL");

    WebElement staticdropdown=driver.findElement(By.id("dropdown"));

    Select dropdown=new Select(staticdropdown);

    dropdown.selectByIndex(2);

    driver.close();

 }

 }

How can we achieve encapsulation in framework?

  • Hiding the implementation details of a class from another class

  • Hiding the fields (web elements) and exposing the action methods as public

  • Make some variables/fields as private no body can use it.

What is TestNG?

  • A testing framework that can be integrated with selenium or any other automation tool to provide multiple capabilities like assertions, reporting, parallel test execution, etc.

  •  Uses annotations to mark test methods and other elements. This makes it easy to organize and configure tests.

  • Tester can run tests in a specific order or skip tests that depend on other tests that have failed.

  • Generates reports that help to identify and fix any problems existing with the application.

How is TestNG different from Selenium WebDriver?

TestNG and Selenium WebDriver, both are tools used for automated testing, but they serve different purposes.

  • TestNG is a testing framework, which provides a structure for organizing and running tests. It also provides features such as annotations, dependencies, data-driven testing, and parallel testing.

  • Selenium WebDriver is a browser automation tool. It allows you to control a web browser from code. This can be used to automate tasks such as logging in, filling out forms, and clicking buttons.

In short, TestNG is used to organize and run tests, while Selenium WebDriver is used to automate tasks in a web browser.  By using them together, we can create comprehensive and efficient automated testing solutions.

​

​

​

​

​

​

 What is the use of the testng.xml file?

The testng.xml file is a configuration file that is used to define TestNG tests. It can be used to specify the following:

  • Name of the test suite

  • Classes that contain the test methods

  • Test methods that should be run

  • Order in which the test methods should be run

  • Dependencies between the test methods

  • Data to be used for the test methods

  • Reports that should be generated

How can we exclude a Test method from getting executed via the testng.xml file?

  • By using the 'exclude tag in the testng.xml file, one can exclude a particular test method from getting executed.

Package Name : Parallel Test

Test Name : FirefoxLoginTest

Class  Name : FirefoxLogin

Method1 Name: ValidCredentials

Method2 Name: InValidCreentials

Method3 Name: Logout

​

​

​

​

​

​

​

​

​

​

 

 

 

 

​

  • In this example, the FireFoxLogin class includes 3 methods such as ValidCredentials, InValidCredentials and Logout. 

  • Here as per the testNG xml file specification, it will exclude the Logout testcase and only the other two methods such as ValidCredentials, InValidCredentials will be executed.

How can we run test cases in parallel using TestNG?

  • To run tests in parallel, we need to set the parallel attribute in the <suite> tag in the testng.xml file.

  • The value of the parallel attribute can be one of the following:   (Methods, Classes, Tests)

Package Name: ParallelTest

Test Name: ChromeLoginTest

Class Name: ChromeLogin    

Method1 Name: ValidCredentials

Method2 Name: InValidCreentials

Method3 Name: Logout                              Methods executed in parallel

​

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Run Classes in Parallel

Package Name : Parallel Test

Test Name : LoginTest

Class 1 Name:  ChromeLogin

Method1 Name: ValidCredentials

Method2 Name: InValidCreentials

Method3 Name: Logout

 Class 2 Name : FirefoxLogin

Method1 Name: ValidCredentials

Method2 Name: InValidCreentials

Method3 Name: Logout                                Class 1 & 2 are executed in parallel

​

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Run tests in Parallel

Package Name: ParallelTest

 Test 1 Name: ChromeLogintest

Class 1 Name: ChromeLogin   

Method1 Name: ValidCredentials

Method2 Name: InValidCreentials

Method3 Name: Logout

Test 2 Name: FirefoxLogintest

Class Name2:FireFoxLogin

Method1 Name: ValidCredentials

Method2 Name: InValidCreentials

Method3 Name: Logout                             Tests 1 &2 are executed in parallel

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

​

​

​

 

What is Fluent wait?

Fluent wait is Used to tell the web driver to wait for a condition as well as the frequency with which we want to check the condition  before throwing an exception.

         Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

                                                       .withTimeout(30, TimeUnit.SECONDS)

                                                       .pollingEvery(3, TimeUnit.SECONDS)

                                                      .ignoring(NoSuchElementException.class);

WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='searchBox']")));

It tries to find the web element repeatedly at regular intervals of time until the timeout or till the object gets found.

Difference between Absolute Xpath and Relative Xpath

           Absolute Xpath                                             Relative Xpath

  • Starts from root node and                    Directly  Jumps to the current node  

         can traverse entire HTML                      based on the attribute specified

         from the root node         

  • Starts with /                                              Starts with //

  • Uses tags/Nodes                                      Uses attributes

  • E.g., html/body/a                                    //input[@name='uname'] 
     

​

​

TNG.png

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Parallel Test Suite" >

<test name="FireFoxLogintest" >

<classes>

<class name=" ParallelTest.FirefoxLogin">

<Methods>

<exclude name=”Logout”/>

</Methods>

/class>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->

 1  Test                   => 1 Class             =>  2 testcases

ChromeLogintest => ChromeLogin => (ValidCredentials, InValidCredentials)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Parallel Test Suite" parallel="methods" thread-count="2">

<test name="ChromeLogintest" >

<classes>

<class name=" ParallelTest.ChromeLogin">

<Methods>

<include name=”ValidCredentials”/>

<include name =” InValidCredentials”/>

</Methods>

/class>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->

1   Test      => 2 Classes (1 & 2 )  => 6 testcases     

Logintest => ChromeLogin => (ValidCredentials, INValidCredentials Logout)

Logintest => FireFoxLogin => (ValidCredentials, INValidCredentials Logout)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Parallel Test Suite" parallel="classes" thread-count="2">

<test name="Login Test" >

<classes>

<class name=" ParallelTest.ChromeLogin "/>

<class name=" ParallelTest.FirefoxLofin"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->

Test  1         => Class 1     => 1 testcase (Method) executed.

ChromeLogintest => ChromeLogin => (InValidCredentials)

 

Test  2         => Class 1     => 2 testcases (Methods) executed.

FirefoxLogintest => FireFoxLogin => (ValidCredentials, InValidCredentials)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Parallel Test Suite" parallel="tests" thread-count="2">

<test name="ChromeLogintest" >

<classes>

<class name=" ParallelTest.ChromeLogin">

<Methods>

<include name=”InValidCredentials”/>

</Methods>

/class>

</classes>

</test> <!-- Test -->

<test name="FireFoxLogintest" >

<classes>

<class name=" ParallelTest.FirefoxLogin">

<Methods>

<exclude name=”Logout”/>

</Methods>

/class>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->

bottom of page