Some important Questions and Answers on Selenium WebDriver

Q#1. What are the benefits of Automation Testing?

  1. Support execution of repeated test cases
  2. Testing a large group of Test Cases
  3. Enable parallel execution
  4. Encourages unattended execution
  5. Improve accuracy and thereby reducing human generated error
  6. Save time and money

Q#2. What are the limitations of Selenium?

  1. Selenium supports testing of only based applications
  2. Mobile applications cannot be tested using Selenium
  3. Reports can only be generated using third party tools like TestNG or Junit
  4. As Selenium is a free tool, thus there is no ready vendor support though the user can find numerous helping communities
  5. User is expected to possess prior programming language

Q#3. What are the different types of locators in Selenium?

Locator can be termed as an address that identifies a web element uniquely within the webpage. Thus, to identify web elements accurately and precisely we have different types of locators in Selenium:

  • ID
  • ClassName
  • Name
  • TagName
  • LinkText
  • PartialLinkText
  • Xpath
  • CSS Selector
  • Dom

Q#4. What are the differences between Assert and Verify commands?

Assert: Assert command checks whether the given condition is true or false.  Let’s say we assert whether the given element is present on the web page or not. If the condition is true then the program will execute the next step but the condition is false, the execution would stop and no further test would be executed.

Verify: Verify command also check whether the given condition is true or false. Irrespective of the condition being true or false, the program execution does not halt i.e., any failure during verification would not stop the execution and all the test steps would be executed.

Q#5. What are the different types of waits available in WebDriver?

Implicit wait: Implicit waits are used to provide a default certain amount of waiting time (say 30 second) before throwing an exception that it cannot find the element on the page.  It is a global time setting for each step in the script.  If the element is not available within the specified time, a “NoSuchElementException” will be raised.

WebDriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(“http://somedomain/url_that_delays_loading”);

WebElement myDynamicElement = driver.findElement(By.id(“myDynamicElement”));

Explicit wait: There can be instance when a particular element takes more than a minute to load. In those cases, we use Explicit wait or WebDriver wait. WebDriver wait will wait until the specified amount of time or the Expected Conditions are met like element to visible etc.

public  WebElement  isElementLoaded (WebElement elementToBeLoaded) {

WebDriverWait wait = new WebDriverWait(driver, 15);

WebElement element = wait.until(ExpectedConditions.visibilityOf (WebelementToBeLoaded));

return element;

}

FluentWait : Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

Sample usage:

// Waiting 30 seconds for an element to be present on the page, checking

// for its presence once every 5 seconds.

Wait wait = new FluentWait(driver)

.withTimeout(30, SECONDS)

.pollingEvery(5, SECONDS)

.ignoring(NoSuchElementException.class);

 

WebElement foo = wait.until(new Function() {

public WebElement apply(WebDriver driver) {

return driver.findElement(By.id(“foo”));

}

};

Q#6. How to select value in a dropdown list?

A value in the drop down list can be selected using WebDriver Select Class.

Syntax:

Select by value:

Select selectByValue = new Select (driver.findElement(By.id(“Select_ID”)));

selectByValue.SelectByValue(“greenvalue”);

SelectByVisibleText:
Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));
selectByVisibleText.selectByVisibleText(“Lime”);

SelectByIndex:
Select selectByIndex = newSelect(driver.findElement(By.id(“SelectID_Three”)));
selectByIndex.selectByIndex(2);

Q#7. How to handle windows based pop up with Selenium WebDriver?

Selenium only supports web application testing. Therefore, windows base pop up cannot be handled by selenium. However,  using some third party tools we can handle window base pop up. Such as Robot class, AutoIT Tool of Java.

Robot Class we can simulate keyboard and mouse events in Selenium.  This is also used while we need to handle file upload and download activity using selenium WebDriver.

Code Example of Using Robot Class:

 public class RobotClass {   

    @Test

    public void robotandselenium() throws InterruptedException

    {

        WebDriver driver=new FirefoxDriver();       

        Robot robot=null;       

        driver.get(“http://www.makemytrip.com”);

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

        driver.findElement(By.xpath(“.//*[@id=’ssologinlink’]”)).click();

        driver.findElement(By.xpath(“.//*[@id=’username’]”)).sendKeys(“username@gmail.com”);

        driver.findElement(By.xpath(“.//*[@id=’password_text’]”)).sendKeys(“password”);       

        try {

            robot=new Robot();

        } catch (AWTException e) {

            e.printStackTrace();

        }

        //Keyboard Activity Using Robot Class and it will press enter key

        robot.keyPress(KeyEvent.VK_ENTER); 

    }

}

Some of the methods of Robot Class are:

  • .keyPress();
  • .keyRelease();
  • .mousePress();
  • .mouseMove();
  • .mouseRelease();

AutoIT  Tool is a tool that can automate the windows GUI. It generates an  ‘.exe’  file which is used by selenium script for file upload or download activity.  This tool automatically enters the required file path which we want to upload in the ‘File Name’ text box. This whole process is done by using ‘AutoIT’ tool. If this tool is not available in the system we need to install it. A AutoIT script can be written in AutoIT  script editor or simple notpad editor.  We need to save the script file with the extension: “ .au3 “

Example:

WinWaitActive(“File Upload”)

Send(“C:\Users\john3\Desktop\TestUploadFile.txt”)

ControlClick(“”, “&Open”, “Button1”)

Q#8. How can you handle web based pop up with Selenium?

We can handle Web based pop up with Selenium using Selenium “Alert” interface. There are 4 methods those we can use along with the Alert interface:

  1. Void dismiss () – the accept () method clicks on the “Cancel” button as soon as the pop up window appears.
  2. Void accept() – the accept() method clicks on the OK button as soon as the pop up window appears
  3. String getText() – the getText() method returns the text displayed on the alert box
  4. Void sendKeys() – the sedKeys() method enters the specified string pattern into the alert box

Syntax:

// accepting javascript alert 
                Alert alert = driver.switchTo().alert();
alert.accept();

Q#9. What are the advantages of TestNG?

TestNG with WebDriver provides an efficient and effective test result format that can in turn be shared with the stake holders to have a glimpse on the product’s/application’s health thereby eliminating the drawback of WebDriver’s incapability to generate test reports. TestNG has an inbuilt exception handling mechanism which lets the program to run without terminating unexpectedly.

There are various advantages that make TestNG superior to JUnit. Some of them are:

  • Added advance and easy annotations
  • Execution patterns can set
  • Concurrent execution of test scripts
  • Test case dependencies can be set

 

Leave a Reply