Selenium WebDriver- Web Application Testing : Introduction to Selenium
Selenium Intro? |
Selenium WebDriver is a tool for automating web application testing, and in particular to verify that they work as expected.
It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium API, which will help to make your tests easier to read and maintain.
It’s not bound to any particular test framework, so it can be used equally well in a unit testing or from a plain old “main” method.
This section introduces WebDriver’s API and helps get you started becoming familiar with it.
Start by setting up a WebDriver project if you haven’t already.
Once your project is set up, you can see that WebDriver acts just as any normal library: it is entirely self-contained, and you usually don’t need to remember to start any additional processes or run any installers before using it.
Sample code Test Code of Selenium Web Driver
package com.javaskool;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class TestEndToEndPages {
private WebDriver driver;
@Before
public void setUp() {
// Create a new instance of the html unit driver
driver = new HtmlUnitDriver();
//Navigate to desired web page
driver.get("http://127.0.0.1:8081/myseleniumtest.com");
}
@Test
public void shouldBeAbleEnterUserNameAndClickSubmitToVerifyWelcomeMessage()
{
// verify title of index page
verifyTitle("Enter your name");
//verify header of index page
verifyHeaderMessage("Please enter your name");
//enter user name as Allen
enterUserName("James");
//verify title of welcome page
verifyTitle("Welcome");
//verify header of welcome page
verifyHeaderMessage("1Welcome James!!!");
//verify back link and click on it
//backToPreviousPage("back");
//verify title of index page again to make sure link is working
//verifyTitle("Enter your name");
}
private void verifyTitle(String expectedTitle) {
//get the title of the page
String actualTitle = driver.getTitle();
// verify title
assertThat(actualTitle, equalTo(expectedTitle));
}
private void verifyHeaderMessage(String expectedHeaderMessage) {
// find header element
WebElement element = driver.findElement(By.tagName("h3"));
String actualHeaderMessage = element.getText();
// verify header text
assertThat(actualHeaderMessage, equalTo(expectedHeaderMessage));
}
private void enterUserName(String userName) {
// find the input text box
WebElement element = driver.findElement(By.name("userName"));
// set the user name in input text box
element.sendKeys(userName);
// submit form
element.submit();
}
private void backToPreviousPage(String expectedLinkText) {
// find the link by its id
WebElement element = driver.findElement(By.id("back"));
//get the actual link text
String actualLinkText = element.getText();
//verify link text with expected like text
assertThat(actualLinkText, equalTo(expectedLinkText));
// click the link
element.click();
}
}
Sample code of Selenium Web Driver for Command Prompt
package com.javaskool;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumExample {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://localhost:8080/test.com");
// driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("userName"));
// WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Anuj!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
How to execute thru Command Prompt
Deploy the myseleniumtest.com provided in donwload section of this article and follow below command
D:\>javac -d . SeleniumExample.java
D:\>java com.javaskool.SeleniumExample
Downloads Example |
Recent Comments