Selenium is one of the best tool for automating web application testing. If number of pages of web application increase gradually, it is really hard for application tester to test every page after new deployment.
Selenium provide web driver for almost browser including firefox, IE, Google Chrome etc. Even you can integrate selenium web driver with Junit, TestNG or plan Java, .Net code.
Selenium provide two type of web drivers. Browser base driver like FirefoxDriver will open the Firefox browser and perform the test. In this case, Selenium will fill up all fields and click on necessary places, just like an automated robot. But if you want to write test cases that does't require browser then you can use HtmlUnitDriver. It will create instance of in-memory browser. But browser base driver is important when you want to test your application whether its compatible with all modern browser like Google chrome, Firefox and IE.
You can download Selenium library from http://docs.seleniumhq.org/download/.
However, you can add it in your project as maven dependency.
Create a new instance of the html unit driver
private WebDriver driver= new HtmlUnitDriver();
// Create a new instance of the Firefox driver
private WebDriver driver= new FirefoxDriver();
Here is an example showing how to perform login before test any secure page. In Junit @BeforeClass annotation method will be called before run any test case.
Selenium provide web driver for almost browser including firefox, IE, Google Chrome etc. Even you can integrate selenium web driver with Junit, TestNG or plan Java, .Net code.
Selenium provide two type of web drivers. Browser base driver like FirefoxDriver will open the Firefox browser and perform the test. In this case, Selenium will fill up all fields and click on necessary places, just like an automated robot. But if you want to write test cases that does't require browser then you can use HtmlUnitDriver. It will create instance of in-memory browser. But browser base driver is important when you want to test your application whether its compatible with all modern browser like Google chrome, Firefox and IE.
You can download Selenium library from http://docs.seleniumhq.org/download/.
However, you can add it in your project as maven dependency.
Following snippet is showing initialization of Firefox web driver and HtmlUnitDriver.org.seleniumhq.selenium selenium-java 2.39.0 org.seleniumhq.selenium selenium-iphone-driver xml-apis xml-apis
Create a new instance of the html unit driver
private WebDriver driver= new HtmlUnitDriver();
// Create a new instance of the Firefox driver
private WebDriver driver= new FirefoxDriver();
Here is an example showing how to perform login before test any secure page. In Junit @BeforeClass annotation method will be called before run any test case.
@BeforeClass public static void setUpBeforeClass() throws Exception { entity = new Users("moin", "123", "abc@gmail.com", "xyz", ""); // Create a new instance of the html unit driver driver = new HtmlUnitDriver(); // And now use this to visit your login URL. e.g driver.get(serverBaseUrl + "/login"); // set the user name in user id text field driver.findElement(By.name("j_username")) .sendKeys(entity.getUserName()); // set the password in user password text field driver.findElement(By.name("j_password")) .sendKeys(entity.getPassword()); driver.findElement(By.className("pull-right")).click(); System.out.println(driver.getPageSource()); if (driver.findElement(By.cssSelector(".jumbotron")).getText() .contains("Welcome to ScrapperX")) { System.out.println("Login Success"); } else { System.out.println("Login fail"); throw new Exception("Login Require"); } Thread.sleep(1000); }After login you can test any secure page in the same session. Below snippet is showing how to test "update user profile page"
@Test public void testUpdateProfile() { driver.get(serverBaseUrl + "/users/editprofile"); // clear the exist first name or else sendKeys will append new text driver.findElement(By.name("firstName")).clear(); driver.findElement(By.name("firstName")) .sendKeys(entity.getFirstName()); driver.findElement(By.name("lastName")).clear(); driver.findElement(By.name("lastName")).sendKeys(entity.getLastName()); driver.findElement(By.name("password")).clear(); driver.findElement(By.name("password")).sendKeys(entity.getPassword()); driver.findElement(By.name("c_password")).clear(); driver.findElement(By.name("c_password")) .sendKeys(entity.getPassword()); // in several way you can select a checkbox /* * ListYou can access any field or html element by css class name, id and field name. Following example is showing how to select a drop-down option.checkbox = * driver.findElements(By.name("paymentActive")); ((WebElement) * checkbox.get(0)).click(); */ // select paymentActive checkbox WebElement checkbox = driver.findElement(By.name("paymentActive")); if (checkbox.isSelected() == false) { checkbox.click(); } driver.findElement(By.className("btn")).click(); System.out.println(driver.findElement(By.cssSelector(".alert")) .getText()); assertTrue("Update user profile fail", driver.findElement(By.cssSelector(".alert")).getText() .contains("Update Complete")); }
Select cardTypeDropDown = new Select(driver.findElement(By .name("cardType"))); cardTypeDropDown.selectByValue("visa");Here is an working example of testing Google search.
package com.scrapperx.web.controller; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class GoogleSearchTest { @Test public void testGoogleSearch() throws InterruptedException { // Create a new instance of the html unit driver // Notice that the remainder of the code relies on the interface, // not the implementation. System.out.println("http://www.google.com"); WebDriver driver = new FirefoxDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("tometo"); // 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()); Thread.sleep(10000); driver.quit(); } }
Do you like this post? Please link back to this article by copying one of the codes below.
URL: HTML link code: BB (forum) link code:
Nice post
ReplyDeleteRegards,
Fahad
http://testingtoolstecniques.blogspot.com/