Blog Detail

  • selenium-interview-questions_346.jpg

    Selenium Interview Questions for Experience Level Part-2

    Question-1

    How do you perform drag and drop operation in WebDriver?
    Code snippet to perform drag and drop operation:

    //WebElement on which drag and drop operation needs to be performed
    WebElementfromWebElement = driver.findElement(By Locator of fromWebElement);

    //WebElement to which the above object is dropped
    WebElementtoWebElement = driver.findElement(By Locator of toWebElement);

    //Creating object of Actions class to build composite actions
    Actions builder = newActions(driver);

    //Building a drag and drop action
    Action dragAndDrop = builder.clickAndHold(fromWebElement)
    .moveToElement(toWebElement)
    .release(toWebElement)
    .build();

    //Performing the drag and drop action
    dragAndDrop.perform();

    Question-2

    What are the different methods to refresh a web page in WebDriver?
    There are multiple ways of refreshing a page in Webdriver.

    1. Using driver.navigate command -

    driver.navigate().refresh();
    2. Using driver.getCurrentUrl() with driver.get() command -

    driver.get(driver.getCurrentUrl());
    3. Using driver.getCurrentUrl() with driver.navigate() command -

    driver.navigate().to(driver.getCurrentUrl());
    4. Pressing an F5 key on any textbox using the sendKeys command -

    driver.findElement(By textboxLocator).sendKeys(Keys.F5);
    5. Passing ascii value of the F5 key, i.e., "\uE035" using the sendKeys command -

    driver.findElement(By textboxLocator).sendKeys("\uE035");

    Question-3

    Write a code snippet to navigate back and forward in browser history?
    Navigate back in browser history:

    driver.navigate().back();
    Navigate forward in browser history:

    driver.navigate().forward();

    Question-4

    How to invoke an application in WebDriver?
    driver.get("url"); or
    driver.navigate().to("url");

    Question-5

    How can we get a text of a web element?
    Get command is used to get the inner text of the specified web element. The get command doesn't require any parameter, but it returns a string type value. It is also one of the widely used commands for verification of messages, labels, and errors,etc.,from web pages.

    Syntax

    String Text = driver.findElement(By.id("Text")).getText();

    Question-6

    How to select value in a dropdown?

    We use the WebDriver's Select class to select the value in the dropdown.

    Syntax:

    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");

    Select selectByIndex = new Select(driver.findElement(By.id("SelectID_Three")));
    selectByIndex.selectByIndex(2);

    Question-7

    What are the different types of navigation commands?

    The navigation commands are as follows.

    navigate().back()

    The above command needs no parameters and takes back the user to the previous webpage.

    Example

    driver.navigate().back();
    navigate().forward()

    The above command allows the user to navigate to the next web page with reference to the browser's history.

    Example

    driver.navigate().forward();
    navigate().refresh()

    The navigate().refresh() command allows the user to refresh the current web page by reloading all the web elements.

    Example

    driver.navigate().refresh();
    navigate().to()

    The navigate().to() command allows the user to launch a new web browser window and navigate to the specified URL.

    Example

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

    Question-8
    How to deal with frame in WebDriver?

    An inline frame abbreviates as an iframe. It is used to insert another document within the current document. These document can be HTML document or simply web page and nested web page.

    Select iframe by id

    driver.switchTo().frame("ID of the frame");
    Locating iframe using tagName

    driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
    Locating iframe using index

    frame(index)

    driver.switchTo().frame(0);
    frame(Name of Frame)

    driver.switchTo().frame("name of the frame");
    frame(WebElement element)

    Select Parent Window

    driver.switchTo().defaultContent();

    Question-9
    Is there an HtmlUnitDriver for .NET?


    To use HtmlUnit first use the RemoteWebDriver and pass it in the desired capabilities.

    IWebDriver driver
    = new RemoteWebDriver(DesiredCapabilities.HtmlUnit())
    For the Firefox implementation to run, use

    IWebDriver driver
    = new RemoteWebDriver(DesiredCapabilities.HtmlUnitWithJavaScript())

    Question-10
    How can you redirect browsing from a browser through some proxy?

    Selenium facilitates a PROXY class to redirect browsing from a proxy. Look at the example below.

    Example
    String PROXY = "199.201.125.147:8080";
    org.openqa.selenium.Proxy proxy = new.org.openqa.selenium.Proxy();
    proxy.setHTTPProxy(Proxy).setFtpProxy(Proxy).setSslProxy(Proxy)DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, proxy);
    WebDriver driver = new FirefoxDriver(cap);

    Question-11


    What is POM (Page Object Model)? What are its advantages?

    Page Object Model is a design pattern for creating an Object directory for web UI elements. Each web page is required to have its page class. The page class is responsible for finding the WebElements in web pages and then perform operations on WebElements.

    The benefits of using POM are as follows.

    -  It facilitates separate operations and flows in the UI from Verification - improves code readability

    -  Multiple tests can use the same Object Repository because the Object Repository is independent of Test Cases.

    -  Reusability of code


    Question-12
    How to type text in a textbox using Selenium?

    The sendKeys("String to be entered") is used to enter the string in a textbox.

    Syntax

    WebElement username = drv.findElement(By.id("Email"));// entering usernameusername.sendKeys("sth");

    Question-13

    How can you find if an element is displayed on the screen?

    WebDriver allows user to check the visibility of the web elements. These web elements can be buttons, radio buttons, drop, checkboxes, boxes, labels etc. which are used with the following methods.

    isDisplayed()

    isSelected()

    isEnabled()

    Syntax:

    isDisplayed():

    boolean buttonPresence = driver.findElement(By.id("gbqfba")).isDisplayed();

    isSelected():

    boolean buttonSelected = driver.findElement(By.id("gbqfba")).isSelected();

    isEnabled():

    boolean searchIconEnabled = driver.findElement(By.id("gbqfb")).isEnabled();


    Question-14

    How to click on a hyper link using linkText?

    driver.findElement(By.linkText("Google")).click();

    The above command search the element using a link text, then click on that element and thus the user will be re-directed to the corresponding page.

    The following command can access the link mentioned earlier.

    driver.findElement(By.partialLinkText("Goo")).click();

    The above-given command searches the element based on the substring of the link provided in the parenthesis. And after that partialLinkText() finds the web element with the specified substring and then clicks on it.



    Question-15
    How to assert the title of a webpage?

    Get the title of the webpage and store it in a variable
    String actualTitle = driver.getTitle();
    Type in the expected titleString expectedTitle = “abcdefgh";
    Verify if both of them are equal

    if(actualTitle.equalsIgnoreCase(expectedTitle))
         System.out.println("Title Matched");
    else
         System.out.println("Title didn't match");
    Alternatively,
         Assert.assertEquals(actualTitle, expectedTitle);