Monday, September 25, 2017

Selenium Interview Questions and Answers- Selenese, Selenium 2.0, Xpath, Locators

1. What is Selenium?

     Selenium is a web based application functional and regression testing tool. Selenium supports most of the languages like Java, PHP, Python, Perl, Ruby etc. It is the open source tool so become one of the most accepted tools amongst the testing professionals. It is package of different testing tools like Selenium IDE, Selenium RC, WebDriver and Selenium Grid.


2. What is Selenese ?

     Selenese is the language which is used to write the test scripts in Selenium IDE.

3. What is Selenium 2.0?

     It is the combination of Selenium RC and WebDriver.

4. What is Xpath ?

     It is a path of element in a XML documents. By using this we can easily find out a web element on a webpage.

     Syntax of Xpath is:

        //tagname[@attribute='value']

5. What is Locators ?
 
     Locators is used to identify the unique web elements on the webpage.It is the properties of HTML web pages. There are different types of locators we are using to find the web elements like:

    ID
    Name
    cssSelector
    Link text
    PartialLinkText
    ClassName
     tagName



6. What is difference between Absolute path and Relative Path ?

     Absolute xpath start with single forward slash (/) which means you can select the element from the root node.
     Example: html/body/div[4]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[1]/div/div/div/div[1]/div[1]/div/div/div/h3/a

   

     Relative xpath start with double forward slash (//) and middle of the HTML content. It is small and robust than absolute xpath.
 
    Example: .//*[@id='Blog1']/div[1]/div[1]/div/div/div/h3/a

   
 
 

7. What are the different challenges with Selenium ?
   
  1. Selenium only supports web based application. 
  2. It can't read the capcha code.
  3. Selenium doesn't have inbuilt facility to generate test results.
  4. Since Selenium is a Open Source tool so it doesn't provide any technical supports.
  5. Selenium doesn't any object repository so we can't maintain object in this. To overcome this we are using Page Object Model concept. 

8. What is difference between Assert and Verify ?

     Assert - If we are using assert command in our test script then test execution will be stop if there is any test step failed. Test case is terminated at that point.

    Verify - It is also use for verify the test steps but at any point test steps failed, test execution will be continue. Selenium move on next line of code and testing is continue.


9. What is synchronization ?

    Sometimes Selenium is faster than our web application or vice versa. In this case we have to manage the speed of both to run our test scripts. So we have different wait commands for this like implicit wait, explicit wait etc.


10. What is difference between implicit wait and explicit wait ?

      Implicit wait: In this we set time for entire duration of test execution. For example if selenium is not found any element then it wait for element for given period of time. If element doesn't found that element in given time then it will be throw an exception 'ElementNotVisibleException'.

          driver.manage().timeouts().implicitlyWait(20, TimeUnits.SECONDS);

    Explicit wait: In this we set time for particular one element. Where we have certain conditions like  Element is Visible, Button is clickable etc. For example if we set explicit wait 10 seconds for a button then selenium wait for 10 seconds for that button. If button will not be clickable in 10 seconds then selenium throw a exception 'ElementNotVisibleException'.


      WebDriverWait wait = new WebDriverWait(driver, 10);

      wait.until(ExpectedConditions.elementToBeClickable(By.id("id")));


11. What is iframe and how to handle it with selenium?
      
      Iframe is a web page which is separate part of HTML pages. Iframe use for to show some different things like Advertisment etc. For iframe we uses <iframe> tag.

   How to handle iframe: Before handle the iframe first we should know the how to identify the iframe in any web page. So first you need to search the iframe in html of the web page like below image


   

     You can see iframe tag in the image.
     Now in selenium iframe can be handle in three different ways:
      1. By Name

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

      2. By Index

        driver.switchTo().frame(indexOfFrame);

      3. By WebElement

       driver.switchTo().frame("xpath of frame ");


12. What are the different types of drivers in WebDriver ?
     
      FirefoxDriver
      ChromeDriver
      InternetExplorerDriver
      SafariDriver
      OperaDriver
      AndriodDriver
      IPhoneDriver
      HtmlUnitDriver
   

13. How to check any checkbox is selected or not ?

      By using isSelected() method.
   
      driver.findElement(By.xpath("locator of Checkbox")).isSelected();

14. How to check any button is enabled or not ?
   
      By using isEnabled() method.

      driver.findElement(By.xpath("locator of  button")).isEnabled();


15 How to check any web element is visible or not ?

        By using isDisplayed() method.

        driver.findElement(By.xpath("locator of  web element")).isDisplayed();


16. How to get a text from a textbox ?

      By using getText() method.

      driver.findElement(By.xpath("xpath of textbox")).getText();


17. What is Actions class ?

      Actions class is used to handle the keyboard events, mouse hover events (such a Drag and Drop , Press Enter, Tab, Shift key etc.). We can also double click on any element, Right click on any element using Action class.

    Example:
   
     Actions act=new Actions(driver);
    // mouse hover on an element and click on it.
     act.moveToElement(element).click().build().perform();

   Note: build method is use to close all series of actions and perform method is to use to perform all actions at a time.
 

18. How to select a value from dropdown ?

      By using Select class we can do this. Select class having different methods to select the dropdown value.

  1. selectByIndex()
  2. selectByValue()
  3. selectByVisibleText()          
     Example:

       WebElement element= driver.findElement(By.xpath("xpath of city dropdown"));

 
   
      Select dropdownvalue = new Select(element);
      dropdownvalue.selectByIndex(1); // It will select the 2nd drop down value- Jaipur
      dropdownvalue.selectByValue("Jaipur");
      dropdownvalue.selectByVisibleText("Jaipur");


  19. What is difference between get() and navigate() ?

        Get(): When we used get command the page will wait until the whole page gets loaded.

                   driver.get("URL of the site");
     
       Navigate(): When we used navigate command then selenium just navigate the page to the given url.
                  driver.navigate().To("URL");

      Navigate is also used for move forward, back direction and refresh the page
               
                driver.navigate().forward();
                driver.navigate().back();
                driver.navigate().refresh();


20. How to handle the web based popup with selenium ?

      By using Alert interface we are handle web based popup. It has different methods like accept(), dismiss() etc. So first we need ti switch on alert popup then perform any action.

            driver.switchTo().alert().aceept(); // To click on OK button on alert.

            driver.switchTo().alert().dismiss(); // To click on cancel button on alert

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


21. How to get page title of the web application ?

          String pagetitle = driver.getTitle();





22. How to handle multiple windows in selenium ?

      Sometime when we click on any link then a window is opened and in our test case we have to perform some action on that opened window (called as child window) so first we need to switch on this child window then we can perform any action. Below code handle the multiple window.

        String parent_win = driver.getWindowHandle(); //store parent window in a string

        Now assume after click on any link a child window will be open.

        Set<String> child_win = driver.getWindowHandles(); // store all child windows in a string using Set interface.

       for (String child_Windows : child_win )
             {
             driver.switchTo().window(child_Windows); // switch to child window
             }
       

       
 23. How to verify tooltip text in selenium ?

       To verify the tooltip text first we have to mouse over on that element on which tooltip is coming then read the tooltip by using getAttribute() method.

     


    In above image we will read tooltip of Gallery menu.

    // First we mouse hover on Gallery

      WebElement element = driver.findElement(By.xpath("xpath of Gallery"));
      Action act = new Actions(driver);
      act.moveToElement(element).build().perform();

   // Read tooltip and store in a string
 
      String tooltip_text = element.getAttribute("title"); // In title we can found tooltip text i.e see in image html section.

  // Use assert command to verify the tooltip text

      Assert.assertEquals("visitor gallery", tooltip_text);


23. What are the different exceptions we are facing in Selenium WebDriver ?

       NoSuchElementException
       NoSuchWindowException
       ElementNotVisibleException
       TimeOutException
       StaleElementReferenceException
       UnReachableBrowserException


24. How to upload the file using Selenium ?

      By using sendKeys() command we can upload it. In sendKeys we will pass the location of file, which we want to upload.
      But Sometime when we click on upload file button then window popup is opened to select the file from the Pc. In this case sendKeys command is not working so we will use AutoIt,Robot third party tool to upload the file.

25. How to perform the right click with selenium ?

      By using Actions class and conextClick() command we can right click.
   
     WebElement element = driver.findElement(By.xpath("locator of element"));
      Actions act = new Actions(driver);
      act.moveToElement(element).perform();
      act.contextClick().perform();



    

      
                 

No comments:

Post a Comment