Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Saturday, 1 May 2021

Basic steps of Selenium testing and its widely used commands via a practical application.

Basic steps of Selenium testing and its widely used commands via a practical application.

Selenium testing can be divided into the following seven basic elements:

Creating an instance of a web driver: This is the first step for all the usages of a Selenium webdriver API. An instance of a webdriver interface is created using a constructor of a particular browser. This webdriver instance is used to invoke methods and to access other interfaces. Following are the most commonly used commands for initializing a web driver:


    Firefox:
    WebDriver driver = new FirefoxDriver();
    
    Chrome:
    WebDriver driver = new ChromeDriver(); 
    
    Safari Driver:
    WebDriver driver = new SafariDriver(); 
    
    Internet Explorer: 
    WebDriver driver = new InternetExplorerDriver(); 


Navigating to a webpage:
 The second step after initializing an instance of a webdriver, to navigate to a particular webpage we want to test. Following are the most commonly used commands for webpage navigation:

    Navigate to URL: 
    driver.get(“https://www.interviewbit.com”) 
    driver.navigateo.to(“https://www.interviewbit.com”) 
    
    Refresh page:
    driver.navigate().refresh()
    
    Navigate forward in browser history:
    driver.navigate().forward()
    
    Navigate backward in browser history:
    driver.navigate().backward()
    

Locating an HTML element on the webpage
:
 To interact with a web element and perform actions on it like clicking a button or entering text, we first need to locate the desired elements such as the button or the textbox on the web page. Following are the most commonly used commands for web element navigation:

    Locating by ID:
    driver.findElement(By.id("q")).sendKeys("Selenium 3");
    
    Location by Name:
    driver.findElement(By.name("q")).sendKeys ("Selenium 3");
    
    Location by Xpath:
    driver.findElement(By.xpath("//input[@id==’q’])).sendKeys("Selenium 3");
    
    Locating Hyperlinks by Link Text:
    driver.FindElement(By.LinkText("edit this page")).Click();
    
    Locating by ClassName
    driver.findElement(By.className("profileheader"));
    
    Locating by TagName
    driver.findElement(By.tagName("select')).click();
    
    Locating by LinkText
    driver.findElement(By.linkText("NextPage")).click();
    
    Locating by PartialLinkText
    driverlindElement(By.partialLinkText(" NextP")).click();
    

Performing actions on an HTML element: Once we have located the HTML element, the next step is interacting with it. Following are the most commonly used commands for performing actions on HTML element:

    Entering a username
    usernameElement.sendKeys("InterviewBit");
    
    Entering a password
    passwordElement.sendKeys("Raw");
    
    Submitting a text input element
    passwordElement.submit(); 
    
    Submitting a form element:
    formElement.submit();  
    

Anticipating browser response from the action
:
 Once an action is performed, anticipating a response from the browser to test comes under this step. It takes a second or two for the action to reach the browser, and hence wait is often required for this step. There are two main types of wait conditions:
    • Implicit Wait: It sets a fixed, definite time for all the webdriver interactions. It’s slightly unreliable as web driver response times are usually unpredictable.

      Eg: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    • Explicit Wait: This type of wait condition sets an expected condition to occur on the web page or a maximum wait time for all the webdriver interactions.
    • Eg: WebElement messageElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("loginResponse")) );

Running tests and recording their results using a test framework
:
 in this step, we run tests in an automated test script to evaluate an application's function and performance. Various test frameworks are used for this step, such as:
    • JUnit for Java
    • NUnit for C#
    • Unittest or Pyunit for Python
    • RUnit for Ruby

    Most frameworks use some sort of asset statement to verify their test results from the expected results.

    Eg: assertEquals (expectedMessage, actualMessage);

Concluding a test
:
In this step, we conclude a test by invoking a quit method on the driver variable. This step closes all the webpages, quits the WebDriver server, and releases the driver.
    Eg: driver.quit();

The following is an example of an app that covers all the steps mentioned above:

import org.openqa.selenium.By,
import org.openqa.selenium.WebElement, 

import org.openqa.selenium.support.ni.ExpectedConditiof, 
import org.openqa.selenium.support.ni.WebOriverWait,

import org.junit.Assert;

public class Example {

public static void main(String[] args) {

// Creating a driver instance
WebDriver driver = new FirefoxDriver(), 

// Navigate to a web page
­driver.get("http://www.foo.com");

// Enter text to submit the form
WebElement usernameElement = driver.findElement( By.name("username"));
WebElement passwordElement = driver.findElement(By.name(”password"));
WebElement formElement = driver.findElement(By.id(”loginForm")); 

usernameElement.sendKeys("Scaler Academy");
passwordElement.sendKeys("Raw"); 

formElement.submit();      // submit by form element 


//Putting an explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement messageElement = wait.until(
       ExpectedConditions.presenceofElementLocated(By.id(”loginResponse")) 
       ) ; 

// Run a test
String message = messageElement.getrept();
String successMsg = "Welcome to foo. You logged in successfully.”;
Assert.assertEquals (message, successMsg); 

// Conclude a test
driver.quit();
}
}