Thursday, November 17, 2016

Log file for Selenium IDE execution using FileLogging addon for firefox

We can create log files of selenium IDE test execution and store in an file using the below process:

1. Download the File Logging add-on to Firefox as shown below.








2. Firefox will restart once the add-on is installed.

3. Now Open Selenium IDE in Firefox.

4. Go to Options>Options. 

5. Selenium IDE options window will open.

6. Go to FileLogging tab.

7. Provide location of file where logs needs to be created.

8. Define the logging level.

9. Select the required checkbox for logging information.

Give it a try, it is quite useful.


Friday, November 11, 2016

Selenium Commands in Selenium IDE

Components of teststep in Selenium IDE

A test step in Selenium comprises of following:
Selenese Commands - set of selenium commands used in Selenium IDE. e.g: Open , type , sendKeys, etc.
Target -  object on which to execute the commands. Target object can be identified based on the locator for the object,i.e. by Id, classname, xpath,CSS, etc.
Value  - acts as input or the expected result on execution of command on the target object. 


Selenium commands in Selenium IDE

Below are some of the useful commands used in Selenium.

  • Open - open(url). URL provided accepts both relative and absolute URLs. The "open" command waits for the page to load before proceeding. URL is provided in the target of the command. In case only ‘/’is provided in the target. It will open the URL as provided in base URL. Following are few ways in which open command works. Suppose base url is http://www.google.com, below are the scenarios
    •  / in target will open http://www.google.com/ (relative path)
    • /#q=google in target will open http://www.google.com/#q=google (relative Path)
    • http://www.rediff.com in target will open http://www.rediff.com (absolute Path). Similar to this is OpenWindow(url) This will open url provided in target in a new window.
  • type(Locator, value) - Types the value in the object identified by the locator. This can be used to set the value of combo boxes, check boxes, etc. Target will be object
  • Sendkeys(Locator, value) - This command types the value key-by-key defined in value on the object identified by Locator. It may be confusing for new users the use of sendkeys when type command already exists. The difference between Sendkeys and type is when we use sendkeys, it does not replace the existing content in the field, whereas when we use type, it replaces the content of the field. Both the methods are used to input data in an edit field.
  • Click(locator) – Clicks an object defined by locator in Target. In case we need to wait for page to load completely before performing further action, we use ClickandWait.
  • Select(Locator, value) This is used to select a value from a webList identified by Locator defined in target and sets the value as defined in the value. In case of Page load in case of selecting value from the weblist, use selectandwait.
  • Store(expression, variable Name) – This stores the value of element defined in expression in the target and stores in the variable name defined in value. This value can be used further in the test by using variable as $variableName.
  • Similar to store are storeAttribute(an element locator followed by a @ sign and then the name of the attribute, e.g. "id=name@href"), storeText(stores the text of an element) to name a few.waitForPageToLoad(time in millisecond) – This waits for the Page to Load completely in the time defined in target. Will move to next step in case the Page loads before the defined time
  • pause(time in milliseconds) – This is similar to wait statement and execution is paused for the time defined for pause command in target.
  • Refresh – This command refreshes the page.
  • verifyText(locator, expected pattern) – This compares the text in the locator element with the expected pattern defined in value. To provide a pattern, you can use text within *value*. This will verify if value appears in the text displayed for locator. Similar to verifyText, we can use verifynottext, verifyalert, verifyattribute to validate information in the Page.
  • AssertText(locator, expected pattern) – This compares the text in the locator element with the expected pattern defined in value. To provide a pattern, you can use text within *value*. This will verify if value appears in the text displayed for locator. Similar to AssertText, we can use Assertnottext, Assertalert, Assertattribute to validate information in the Page. Difference between Assert and verify is while test stops in case of assert fails but remaining statements in the test are executed in case of verify statement even if the verify statement fails.
  • Highlight(Locator) – highlights the object identified by locator in the page.
  • Break – This halts the test execution, test execution will continue once we click on continue in the IDE. This should be used in test but removed once test stabilizes as it results in manual intervention.
  • captureEntirePageScreenshot(filename) – This command captures screenshot and save in the location defined in filename in the target
  • check(locator) – this checks a radio button/checkbox based on locator defined in target.
  • Close() – This command simulates the user clicking the "close" button in the title bar of a popup window or tab
  • windowMaximize() – this command will resize currently selected window to take up entire screen


Different types of Selenese Commands

Selenese commands can be classified into following types:
a.    Actions – Performs action on an object. E.g. Click, type
b.    Assertions – Used to add validation steps. E.g. AssertText, verifytext
c.    Accessors – checks the state of application and stores values from the test. E.g. storetext.


Default timeout for Selenium Commands

Default timeout for selenium commands is 30000. The timeout can be changed from Options>General>default timeout of recorded command

Exporting selenium IDE tests to WebDriver

Script created in Selenium IDE can be exported to WebDriver/Remote Control in the languages java/c#/python/ruby that can be run using junit(java), RSpec(ruby), nUnit and testNG. 


To export test in TestNG with selenium webdriver, follow below steps:

  • Record the required operations on the application.
  • Go to File>Export Test Case as Java / TestNG / WebDriver (can export test script in different formats)
Exporting test in Selenium IDE

  • Export the test.
  • A java file will be created with content as shown below


package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TESTINGAA {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @BeforeClass(alwaysRun = true)
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.google.co.in/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testINGAA() throws Exception {
    driver.get(baseUrl + "/?gfe_rd=cr&ei=_OMdWJKzEO3I8AeAqrBg&gws_rd=ssl");
    driver.findElement(By.id("lst-ib")).clear();
    driver.findElement(By.id("lst-ib")).sendKeys("TESTING AUTOMATION CONEPTS");
    assertTrue(isElementPresent(By.linkText("TESTING AUTOMATION CONCEPTS")));
    driver.findElement(By.linkText("TESTING AUTOMATION CONCEPTS")).click();
    driver.findElement(By.linkText("Test automation - Wikipedia")).click();
    assertTrue(isElementPresent(By.linkText("Test automation - Wikipedia")));
  }

  @AfterClass(alwaysRun = true)
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}