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();
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 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();
- 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.
- 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: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Eg: WebElement messageElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("loginResponse")) );
- 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);
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();
}
}