0 votes
in Selenium by
Explain basic steps of Selenium testing and its widely used commands via a practical application.

1 Answer

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

1. Creating an instance of a Webdriver: 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 access other interfaces. The 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();

2. Navigating to a webpage: The second step after initializing an instance of a webdriver, is to navigate to a particular webpage you want to test. The 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()

3. 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. The 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();

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

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

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

6. 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);

7. 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();

}

}

Related questions

0 votes
asked Dec 22, 2022 in Sqoop by SakshiSharma
0 votes
asked Jan 9 in Selenium by sharadyadav1986
...