mardi 30 décembre 2014

Converting practice project to use data driven principles, any other suggestions

So I used JUnit, Selenium, and Cucumber to make a basic web app test and I had some questions about how I might be able to improve it. This is just a personal practice type thing. Here is what I have so far:


CucumberRunner class:



package cucumber;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
format = {"pretty", "json:target/"},
features = {"src/cucumber/"}
)

public class CucumberRunner {

}


Feature File:



Feature: To test budget calculator form works when there are no errors

Scenario: Check that form is validated when there are no errors
Given I am on budget calculator site
When I populate the budget fields
And click the calculate button
Then print the calculated total
And close the browser


And my step definitions:



package cucumber.features;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinitions {

WebDriver driver = null;

@Given("^I am on budget calculator site$")
public void shouldNavigateToBudgetCalculatorSite() throws Throwable {
try {
driver = new FirefoxDriver();
driver.navigate().to("http://frugalliving.about.com/library/Budget_Calculator/"
+ "bl_budgetcalculator.htm");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@When("^I populate the budget fields$")
public void shouldPopulateBudgetFields() throws Throwable {
try {
driver.findElement(By.name("in1")).sendKeys("3500.00");
driver.findElement(By.name("in2")).sendKeys("1000.00");
driver.findElement(By.name("ex01")).sendKeys("800.00");
driver.findElement(By.name("ex02")).sendKeys("400.00");
driver.findElement(By.name("ex03")).sendKeys("50.00");
driver.findElement(By.name("ex04")).sendKeys("0.00");
driver.findElement(By.name("ex05")).sendKeys("80.00");
driver.findElement(By.name("ex06")).sendKeys("150.00");
driver.findElement(By.name("ex07")).sendKeys("200.00");
driver.findElement(By.name("ex08")).sendKeys("0.00");
driver.findElement(By.name("ex09")).sendKeys("50.00");
driver.findElement(By.name("ex10")).sendKeys("80.00");
driver.findElement(By.name("ex11")).sendKeys("0.00");
driver.findElement(By.name("ex12")).sendKeys("40.00");
driver.findElement(By.name("ex13")).sendKeys("0.00");
driver.findElement(By.name("ex14")).sendKeys("0.00");
driver.findElement(By.name("ex15")).sendKeys("0.00");
driver.findElement(By.name("ex16")).sendKeys("0.00");
driver.findElement(By.name("ex17")).sendKeys("0.00");
driver.findElement(By.name("ex18")).sendKeys("200.00");
driver.findElement(By.name("ex19")).sendKeys("50.00");
driver.findElement(By.name("ex20")).sendKeys("0.00");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@And("^click the calculate button$")
public void shouldClickTheCalculateButton() throws Throwable {
try {
driver.findElement(By.cssSelector("input[value=\"Calculate\"]")).click();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Then("^print the calculated total$")
public void print_the_calculated_total() throws Throwable {
try {
String total = driver.findElement(By.name("res")).getAttribute("value");
int value = Integer.parseInt(total);
System.out.println("The calculated total is: " + value);
System.out.println(" ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


@And("^close the browser$")
public void shouldCloseTheBrowser() throws Throwable {
try {
Thread.sleep(2000);
driver.quit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}


(If you try and run any of that, obviously you'd need the jars for cucumber, selenium, and junit, plus firefox installed). Okay so a couple questions:




  1. When I am populating the fields, instead of hardcoding 1 value in each, I'd like to set it up to be data driven through excel or MySQL (what word would I use to describe that? would robust be accurate there? or maybe versatile).




  2. Also when populating the fields, that code looks cumbersome, and I'm trying to figure out a way I could use a loop to populate for each field, and what type of loop it would be?




  3. Being new to automation (QA Engineer Level I intern), and testing in general, is there another/better method or tool to use if I wanted to stick with Java for automation? My job had me using QTP and VB Script, which was really powerful, for a little while to kind of introduce this to me, but I'd rather stick with Java because Java is where I want to focus (and they're giving me the option, so it's not like I'm being insubordinate with them)




Thanks in advance!


EDIT:


If you do decide to try and run this, these are the jars in the build path:



cucumber-core-1.1.5.jar
cucumber-html-0.2.3.jar
cucumber-java-1.1.5.jar
cucumber-junit-1.1.5.jar
cucumber-jvm-dep-1.0.3.jar
gherkin-2.12.1.jar
hamcrest-all-1.3.jar
junit-4.11.jar
selenium-server-standalone-2.44.0.jar


As an aside everything currently works perfect, so just clarifying that I don't have errors or anything like that. More so just looking for tools, or practices that I may be missing or not aware of in general, that I could improve this with. And again, thanks in advance!


Aucun commentaire:

Enregistrer un commentaire