Selenium
has become a front-runner framework for the browser automation. When creating
regression test suite biggest challenge is speed vs. maintainability. If we go
with scripting approach we get speed but maintenance of scripts is difficult
and if we code tests by hand it takes longer time to automate and this
discourages automation.
SeleniumBuilder – An open source Firefox add-on, with this tool we can record and
capture the scripts quickly and later export to the one of the many supported
programming language / framework to generate the code. Once the code is exported ; refactor it to make it maintainable and reusable.
Here are the steps of installing and using Selenium Builder.
Step 1. Install – Open Firefox browser and go to https://github.com/SeleniumBuilder/se-builder and click on install button. It will install add-on Firefox.
Step 2. Launch Selenium Builder. Go to Tools -> Web Developer -> Launch Selenium
Builder.
Step 3. Start Recording using Selenium 2
Step 4. Record Verification. Click on Record a Verification
button and then go to webpage (Bing) and select the text you want to verify.
Step 5. Search for ravikothiyal.blogspot on Bing
·
Click on the search result Ravi – blogsspot.com
·
Record Verification – Ravi
·
Record Verification - Ravi's blog on
Computing, Java, Agile, Big Data, Enterprise Development, and more
·
Record Verification - Monday, July 28,
2014
Step 6. Stop recording
Step 7. Save Script
Step 8. Edit Script
Step 9. Export Script – Export script as java/junit
Step 10. Export Script - Export script as Ruby
Generated Java Code
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.io.File;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.*;
import static org.openqa.selenium.OutputType.*;
public class RaviBlogTest {
FirefoxDriver wd;
@Before
public void setUp() throws Exception {
wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
public void RaviBlogTest() {
wd.get("http://www.bing.com/");
if (!wd.findElement(By.tagName("html")).getText().contains("Bing")) {
System.out.println("verifyTextPresent failed");
}
if (!wd.findElement(By.id("sb_form_q")).getAttribute("value").equals()) {
System.out.println("verifyElementValue failed");
}
wd.findElement(By.id("sb_form_q")).click();
wd.findElement(By.id("sb_form_q")).clear();
wd.findElement(By.id("sb_form_q")).sendKeys("ravikothiyal.blogspot");
wd.findElement(By.id("sb_form_go")).click();
wd.findElement(By.linkText("Ravi - blogspot.com")).click();
if (!wd.findElement(By.tagName("html")).getText().contains("Ravi")) {
System.out.println("verifyTextPresent failed");
}
if (!wd.findElement(By.tagName("html")).getText().contains("Ravi's blog on Computing, Java, Agile, Big Data, Enterprise Development, and more")) {
System.out.println("verifyTextPresent failed");
}
if (!wd.findElement(By.tagName("html")).getText().contains("Monday, July 28, 2014")) {
System.out.println("verifyTextPresent failed");
}
}
@After
public void tearDown() {
wd.quit();
}
public static boolean isAlertPresent(FirefoxDriver wd) {
try {
wd.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
}
Refactored Java Code
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.firefox.FirefoxDriver;
public class RaviBlogTest {
private static final String HTML = "html";
private static final String SB_FORM_GO = "sb_form_go";
private static final String SB_FORM_Q = "sb_form_q";
FirefoxDriver wd;
static final String BING_URL="http://www.bing.com/";
@Before
public void setUp() throws Exception {
wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
public void BlogTest() {
wd.get(BING_URL);
Assert.assertTrue("Page is Bing", wd.findElement(By.tagName(HTML))
.getText().contains("Bing"));
wd.findElement(By.id(SB_FORM_Q)).sendKeys("ravikothiyal.blogspot");
wd.findElement(By.id(SB_FORM_GO)).click();
wd.findElement(By.linkText("Ravi - blogspot.com")).click();
Assert.assertTrue("Verify blog header",
wd.findElement(By.tagName(HTML)).getText().contains("Ravi"));
Assert.assertTrue(
"Verify blog title",
wd.findElement(By.tagName(HTML))
.getText()
.contains(
"Ravi's blog on Computing, Java, Agile, Big Data, Enterprise Development, and more"));
Assert.assertTrue(
"Verify blog date",
wd.findElement(By.tagName(HTML)).getText()
.contains("Monday, July 28, 2014"));
}
@After
public void tearDown() {
wd.quit();
}
public static boolean isAlertPresent(FirefoxDriver wd) {
try {
wd.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
}
Generated ruby code
require 'rubygems'
require 'selenium-webdriver'
wd = Selenium::WebDriver.for :firefox
wd.get "http://www.bing.com/"
if not wd.find_element(:tag_name, "html").text.include? "Bing"
print "verifyTextPresent failed"
end
if not wd.find_element(:id, "sb_form_q").value ==
print "verifyElementValue failed"
wd.find_element(:id, "sb_form_q").click
wd.find_element(:id, "sb_form_q").clear
wd.find_element(:id, "sb_form_q").send_keys "ravikothiyal.blogspot"
wd.find_element(:id, "sb_form_go").click
wd.find_element(:link_text, "Ravi - blogspot.com").click
if not wd.find_element(:tag_name, "html").text.include? "Ravi"
print "verifyTextPresent failed"
end
if not wd.find_element(:tag_name, "html").text.include? "Ravi's blog on Computing, Java, Agile, Big Data, Enterprise Development, and more"
print "verifyTextPresent failed"
end
if not wd.find_element(:tag_name, "html").text.include? "Monday, July 28, 2014"
print "verifyTextPresent failed"
end
wd.quit
Nice article, thanks!
ReplyDeletehttp://web-app.com.au
This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. Search Bar Firefox 57 Quantum addon
ReplyDelete