Top Selenium interview questions and Answers

 

Difference between close() and quit() in selenium webdriver?

    • Driver.close(): By using this we can close the current browser window.
      • I.e.  Single window on which driver is having focus that will be closed only.

Difference-between-close()-and-quit()-in-selenium-webdriver

    • Driver.quit(): Every associated browser will be closed
      • I.e. closed all the windows opened by the driver

 

What is the purpose of public static void main() in java ?

>public: Since, main() should be available to JVM.It should be declared as public. If we don’t use main() as a public then it doesn’t make itself available to JVM  and JVM can not execute it. 

>Static: We should be call the main() without creating an object,such methods are called static methods and we should be declared as static. 

>Void: If main() it does not written any value then you have to put void(no value) before the main(). 

>main(): If  main() is not written in the java program then jvm will not execute the program.main() is the starting point for JVM to start execution of java

What are the pre-requisites to run the selenium webdriver?

      1. JDKE
      2. clipse IDE
      3. Selenium JAR file
      4. Testing Application
      5. Browser 

How switch back to main window from inside any frame ?

>Syntax : driver.switchTo().defaultContent(); 

How to select the dropdown value inside a frame?

TestSteps:

    • Open Firefox Browser
    • Open AppURL In Browser
    • Get the Title of WebPage 
    • Verify Title of WebPage
    • Enter the username 
    • Enter the password
    • Clicking On Login Button
    • Identify and get  the Welcome selenium text
    • Verify  Welcome selenium text 
    • Switch to frame
    • How to Select the dropdown value inside a frame
    • Again switch back to main window from frame 
    • Clicking On Logout Button 
    • Close the Firefox Browser 

Selenium Code:

package com.tests; 

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;

import org.openqa.selenium.support.ui.Select; 

public class SelectDropdownValueInsideFrame { 

public static void main(String[] args) throws InterruptedException {

// open the firefox browser 

WebDriver driver = new FirefoxDriver(); 

 // navigate the AppUrl 

driver.get("http://127.0.0.1/orangehrm-2.5.0.2/login.php"); 

 //Get the Title of WebPage 

 String title=driver.getTitle(); 

 //print the title of the webpage 

 System.out.println(title);   

//Verify Title of the WebPage 

if (title.equals("OrangeHRM - New Level of HR Management")) {   

System.out.println("title is verified successfully"); 

 } else {

System.out.println("title is not verified successfully"); 

   }   

// Enter the username   

driver.findElement(By.name("txtUserName")).sendKeys("selenium"); 

 // Enter the password   

driver.findElement(By.name("txtPassword")).sendKeys("selenium"); 

 // Clicking On Login Button 

 driver.findElement(By.name("Submit")).click(); 

 // Identify and get  the Welcome selenium text and store into text variable 

String text = driver.findElement(By.xpath("//*[@id='optionmenu']/li[1]")).getText(); 

// Print the welcome selenium text 

 System.out.println(text); 

 //  To verify whether the welcome page successfully opened or not 

 if (text.equals("Welcome selenium")) { 

System.out.println("Welcome selenium is verified successfully");   

    } else {   

System.out.println("Welcome selenium is not verified successfully");   

  } 

 // Switch to Frame by ID  

driver.switchTo().frame("rightMenu"); 

System.out.println("*****How to Select the dropdown value inside a frame*****");

 /*    If u want to select the dropdown value then we need to create the  select object for that dropdown   */

// Select the dropdown value by using index 

 new Select(dropdown) .selectByIndex(2); 

/* // Select the dropdown value by using  selectByVisibleText 

new Select(dropdown).selectByVisibleText (2); 

// Select the dropdown value by using  selectByValue

new Select(dropdown).selectByValue("2");   */ 

// switch back to main window from inside any frame 

driver.switchTo().defaultContent(); 

// Clicking On Logout Button 

driver.findElement(By.xpath("//*[@id='option-menu']/li[3]/a")).click(); 

 // Close the Firefox Browser 

driver.close(); 

       }  

How to Handling Web-Based alert Popup/Javascript Alert Popup. ?

TestSteps : 

  • open the firefox browser
  • Navigate the url
  • Enter the Username
  • Enter the password
  • click on login button
  • click on logout button
  • switchTo alert, get the alert text and store the alert text into  alert_text variable
  • verify alert text
  • click on ok button
  • close the browse

Selenium code:

package seleniumProject;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

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;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test; 

public class WebBasedAlertPopup_JavascriptAlertPopup {   

        public static void main(String[] args) throws InterruptedException

{

// open the firefox browser     

WebDriver driver = new FirefoxDriver(); 

 // Navigate the url 

 driver.get("http://demo.guru99.com/v4/"); 

 // Enter the Username 

driver.findElement(By.xpath("html/body/form/table/tbody/tr[1]/td[2]/input")).sendKeys("mngr193544"); 

// Enter the password 

driver.findElement(By.xpath("html/body/form/table/tbody/tr[2]/td[2]/input")).sendKeys("EbepYgE"); 

// click on login button 

driver.findElement(By.xpath("html/body/form/table/tbody/tr[3]/td[ 2]/input[1]")).click(); 

 // Wait 5sec 

Thread.sleep(5000); 

// click on logout button 

driver.findElement(By.xpath("html/body/div[3]/div/ul/li[15]/a")).c lick();  

// Wait 5sec 

Thread.sleep(5000); 

// switchTo alert ,get the alert text and store the the alert text in alert_text   

// variable 

 String alert_text = driver.switchTo().alert().getText(); 

System.out.println("Print alert text   :" + alert_text); 

 // verify alert text 

if (alert_text.equals("You Have Succesfully Logged Out!!"))

{   

System.out.println("alert text verified successfully");   

   } else {   

System.out.println("alert text not verified successfully");   

// click on ok button 

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

// click on cancel button 

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

// close the browser 

// Wait 5sec 

Thread.sleep(5000); 

// close the browser window 

driver.close(); 

      }  

Note  :

    1. Until you do not handle alert  you cannot perform any action in the parent window. 
    2. Web-Based alert and Java Script alerts are same so do not get confused.

 

Kosmik is the best  Selenium Training institute Hyderabad,KPHB. We are providing Selenium Training in hyderabad & lab facilities with complete real-time Selenium trainingSelenium Training in Kukatpally is based on complete advance concepts. So that you can get easily "hands-on experience". We will give 100% job assistance.Selenium Training in kphb, Selenium Training in Kukatpally,Selenium Training in Telangana, Selenium training institutes in hyderabadSelenium course in hyderabad.