3 min read

Selenium Interview Questions (Part1)

Selenium Interview Questions
Selenium Interview Questions

What are the new features of Selenium4?
Reference link:

  1. Enhanced Selenium Grid
  2. Upgraded Selenium IDE
  3. Relative Locators in Selenium 4 : To left of, To right of, Above, Belo
  4. Better Window/Tab Management in Selenium 4 :.switchTo().newWindow(WindowType.TAB

- Screenshot code? We will be getting a screenshot of the full page or visible screen?

System.setProperty("webdriver.chrome.driver", WorkSpaceLocation+"Drivers\\chromedriver.exe");              
WebDriver driver = new ChromeDriver();           
driver.manage().window().maximize();              
driver.get("https://google.com");              
File scrFile;              
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);         
FileUtils.copyFile(scrFile, new File("D:\\MyWork\\testimages.png"));
/** This takes screenshot of browser page only.**/


- what is Javascript executor? Code to invoke it and why new keyword is not used in invoking it?
Using Selenium:

WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

Using javascript:

JavascriptExecutor js = (JavascriptExecutor)driver;             
if(Gender.equalsIgnoreCase("Male")) {              
WebElement GenderElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(GenderMale)));
js.executeScript("arguments[0].click();", GenderElement);
}else {              
WebElement GenderElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(GenderFemale)));
js.executeScript("arguments[0].click();", GenderElement);              }}catch(TimeoutException e){                     driver.findElement(By.id(Gender)).click();
}              
}


- How to scroll to element? (using Selenium and using JS executor)

WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);

- How to switch to window (write code). How to switch to particular window if 5 are open.

Set allTabs = driver.getWindowHandles();
Iterator itr = allTabs.iterator();
String MyTab = null;
while (itr.hasNext()) {
    String newTab = (String) itr.next();
    String MyHandle = driver.switchTo().window(newTab).getTitle();
    if (MyHandle.equals("Update Profile|Mynaukri")) {
        MyTab = newTab;
    } else {
        driver.switchTo().window(newTab).close();
    }

Is ChromeDriver driver = new ChromeDriver correct or incorrect?
- Why we write WebDriver driver = new ChromeDriver why not ChromeDriver driver = new ChromeDriver
In reality, all the browser drivers like FirefoxDriver, ChromeDriver, IEDriver, etc implemented WebDriver interface (in fact RemoteWebDriver class implements WebDriver interface and browser drivers extends RemoteWebDriver), so if we will use WebDriver driver, then we can use the driver (as common object variable) for all browsers we want to automate.

WebDriver driver = new FirefoxDriver();
driver = new ChromeDriver(); //allowed
driver = new SafariDriver(); //allowed

What if we want only one instance of Driver?
In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time.
After the first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to any variable inside the class through any instance, it affects the variable of the single instance created and is visible if we access that variable through any variable of that class type defined.

  • We can make constructor as private. So that We can not create an object outside of the class.
  • This property is useful to create singleton class in java.
  • Singleton pattern helps us to keep only one instance of a class at any time.
  • The purpose of singleton is to control object creation by keeping private constructor.

Java singleton Sample code

private WebDriver driver; 
/**     * Create private constructor     */
private MySingleTon() {
    System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
    driver = new ChromeDriver();
} 
/**     * Create a static method to get instance.     */
public static MySingleTon getInstance() {
    if (myObj == null) {
        myObj = new MySingleTon();
    }
    return myObj;
}
// To get driver 
public WebDriver getDriver() {
    return driver;
}
public static void main(String a[]) {
    MySingleTon st = MySingleTon.getInstance();
    st.getDriver();
}
}


- What argument we pass while creating Select object?
Select SelectObj = new Select(WebElement); SelectObj.selectByVisibleText("2010")
- What is Action class? What all we can do with the help of action?
In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.

// Configure the Action
Actions action = new Actions(driver);
// To click on the element
elementaction.moveToElement(element).click().perform();
clickAndHold() contextClick() doubleClick() dragAndDrop(source,target)

- Difference between Action and Actions?
Action:
In Selenium, Action is an interface which represents a single user-interaction action. It is defined in org.openqa.selenium.interactions. It contains one of the most widely used method perform().
Actions:
In Selenium, Actions is a Class. It is defined in org.openqa.selenium.interactions. This is the user-facing API for emulating complex user gestures. Actions Class implements the builder pattern, which can build a CompositeAction containing all actions specified by the method calls.