Loading presentation...
Prezi is an interactive zooming presentation

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Cucumber & Selenium 2.0

No description
by

David de florinier

on 21 September 2010

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Cucumber & Selenium 2.0

Cucumber & Selenium 2.0 ...or how can I implement UI Testing without wanting to shoot myself three months later... David de Florinier Follow on twitter:@davedf http://deflorinier.blogspot.com/ Hang on, I've seen a great blog post about this... Follow @cuke4ninja Business Rules UI Workflow Let's see an example with Ninjas? Technical Activities Thankyou Document the feature Can be automated Validate the feature Cucumber is a good fit for this I've assumed you know about cucumber already
The main site is cukes.info
Document published on cuke4ninja.com
Follow @cuke4ninja for information about updates Feature: Report on assigned problems
In order to see the bugs that are assigned to me
As a Ninja Developer
I want to run a run a report that lists all the issues assigned to me

Scenario: Assigned problem
Given there are open issues with the properties
| Title | Severity |
| Chuck Norris beat me | Fatal |
When the issue "Chuck Norris beat me" is assigned to Ninja2
Then Ninja2 sees the following issues in his report
| Title | Severity |
| Chuck Norris beat me | Fatal |
And Ninja1 sees no issues in his report Feature: Report on assigned problems
In order to see the bugs that are assigned to me
As a Ninja Developer
I want to run a run a report that lists all the issues assigned to me Scenario: Assigned problem
Given there are open issues with the properties
| Title | Severity |
| Chuck Norris beat me | Fatal |
When the issue "Chuck Norris beat me" is assigned to Ninja2
Then Ninja2 sees the following issues in his report
| Title | Severity |
| Chuck Norris beat me | Fatal |
And Ninja1 sees no issues in his report
We are using an open source bug tracking system in this example Called CodeTrack See http://kennwhite.sourceforge.net/codetrack/ for more info Workflow Fluent API Given there are open issues with the properties
| Title | Severity|
| Chuck Norris beat me | Fatal| @Given("^there are open issues with the properties$")
public void thereAreOpenIssuesWithThePropertiesWithTable(Table issues) {
project = userWorkflow
.logOnAs(ADMIN)
.createNewProject()
.addIssues(tableConverter.toIssues(issues))
.getProjectId();
} When the issue "Chuck Norris beat me" is assigned to Ninja2 @When("^the issue \"(.*)\" is assigned to (.*)$")
public void theIssueWithTitleIsAssignedToUser(String issueTitle, String user) {
userWorkflow
.logOnAs(ADMIN)
.usingProject(project)
.assignIssueToUser(issueTitle, user);
} Then Ninja2 sees the following issues in his report
| Title | Severity |
| Chuck Norris beat me | Fatal | @Then("^(.*) sees the following issues in his report$")
public void userSeesTheFollowingIssuesInHisReportWithTable(String user, Table issues) {
List<Issue> assignedIssues = userWorkflow
.logOnAs(user)
.usingProject(project)
.viewAssignedIssuesReport()
.getIssues();

assertEquals(assignedIssues, tableConverter.toIssues(issues));
} And Ninja1 sees no issues in his report @Then("^(.*) sees no issues in his report$")
public void userSeesNoIssuesInHisReport(String user) {
assertEquals(0, userWorkflow
.logOnAs(user)
.usingProject(project)
.viewAssignedIssuesReport()
.getNumberOfIssues());
} Implement Workflow as Fluent Objects responsible for an area of the system In this case:
User workflow encapsulates logging in/out and selection of project
Project workflow deals with running report The workflow still doesn't use Selenium though... package webninja.actions;

import webninja.selenuim.CodeTrack;
import webninja.selenuim.LogonPage;
import webninja.selenuim.ProjectForm;

import java.util.UUID;

public class UserWorkflow {
private final ProjectWorkflow projectWorkflow ;
private final CodeTrack codeTrack;

public UserWorkflow(CodeTrack codeTrack) {
this.codeTrack = codeTrack;
this.projectWorkflow = new ProjectWorkflow(codeTrack);
}

public UserWorkflow logOnAs(String user) {
logOut();
LogonPage logonPage = codeTrack.gotoLogonPage();
logonPage.setName(user);
logonPage.setPassword(user);
codeTrack.submit();
return this;
}

public ProjectWorkflow createNewProject() {
ProjectForm projectForm = codeTrack.showAdminPage().showNewProjectForm();
String nextProjectName = getNextProjectName();
projectForm.setName(nextProjectName);
projectForm.setDescription("Test project");
codeTrack.submit();
return usingProject(nextProjectName);
}

public ProjectWorkflow usingProject(String project) {
projectWorkflow.setCurrentProject(project);
return projectWorkflow;
}


public UserWorkflow logOut() {
if (codeTrack.isLoggedIn())
codeTrack.logOut();
return this;
}

private String getNextProjectName() {
return "CP" + new DateTime().toString("yyyyMMddhhmmss");
}
} So, three levels .. Business rules
User interface workflow
Technical activities Business Workflow Technical Technical Activities Steps should be fine grained
Aim for resuse wherever possible
Writing tests first will tend to produce UI that is suited for testing
Divide UI Elements into symmetric classes package webninja.selenuim;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import webninja.UserRepository;

public class CodeTrack {
private final String baseUrl;
private final WebDriver webDriver;
private final UserRepository userRepository;

public CodeTrack(
String baseUrl,
WebDriver webDriver,
UserRepository userRepository) {
this.baseUrl = baseUrl;
this.webDriver = webDriver;
this.userRepository = userRepository;
}



public void logOut() {
webDriver.findElement(
By.xpath("//a[@title='Log off the CodeTrack System']"))
.click();
}



public LogonPage gotoLogonPage() {
webDriver.get(getUrl("?page=login"));
return new LogonPage(webDriver);
}



public HomePage gotoHomePage() {
webDriver.findElement(
By.xpath("//a[@title='Summary of your current project']"))
.click();
return new HomePage(webDriver, this, userRepository);
}



public boolean isLoggedIn() {
webDriver.get(getUrl(""));
try {
gotoHomePage();
return true;
}
catch (NoSuchElementException e) {
return false;
}
}



public AdminPage showAdminPage() {
webDriver.findElement(
By.xpath("//a[@title='CodeTrack Administration and Setup']"))
.click();
return new AdminPage(webDriver);
}

public IssueForm showNewIssueForm() {
webDriver.findElement(
By.xpath("//a[@title='Create a new defect report or Change Request']"))
.click();
return new IssueForm(webDriver, this, userRepository);
}




public ReportsPage gotoReportsPage() {
webDriver.findElement(
By.xpath("//a[@title='Create simple and advanced reports']"))
.click();
return new ReportsPage(webDriver);
}






public void selectOption(By by, String text) {
Select select = new Select(webDriver.findElement(by));
select.selectByVisibleText(text);
}



public void setProjectName(String projectName) {
selectOption(By.name("project"), projectName);
}

public void submit() {
webDriver.findElement(
By.xpath("//input[@type='submit']"))
.click();
}



private String getUrl(String relativePath) {
return String.format("%s%s", baseUrl, relativePath);
}

} package webninja.selenuim;

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

public class LogonPage {
private final WebDriver webDriver;

public LogonPage(WebDriver webDriver) {
this.webDriver = webDriver;
}

public void setName(String name){
webDriver.findElement(
By.name("userLogin[username]"))
.sendKeys(name);
}

public void setPassword(String password){
webDriver.findElement(
By.name("userLogin[password]"))
.sendKeys(password);
}

} package webninja.selenuim;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import webninja.actions.Issue;

import java.util.ArrayList;
import java.util.List;

public class IssueTable {
private final WebDriver webDriver;

public IssueTable(WebDriver webDriver) {
this.webDriver = webDriver;
}


public List<Issue> getIssues() {
List<Issue> issues = new ArrayList<Issue>();
List<WebElement> rows =
webDriver.findElements(By.xpath("//table[@id='results']/tbody/tr"));
for (WebElement row : rows) {
String title = row.findElement(By.xpath("//td[4]")).getText();
String severity = row.findElement(By.xpath("//td[3]")).getText();
issues.add(new Issue(severity, title));
}
return issues;

}
} Demo? www.cuke4ninja.com Use Domain Language Selenium 2.0 & WebDriver Improved support for multiple browsers, including headless
Handles muliple frames, browser windows, popups, alerts
Drag and drop
Ajax based UI Elements supported
See http://seleniumhq.org/docs/09_webdriver.html for good covereage of the main features public UserWorkflow logOnAs(String user) {
logOut();
LogonPage logonPage = codeTrack.gotoLogonPage();
logonPage.setName(user);
logonPage.setPassword(user);
codeTrack.submit();
return this;
} public AssignedIssuesReport viewAssignedIssuesReport() {
ReportsPage reportsPage = codeTrack.gotoReportsPage();
IssueTable issueTable = reportsPage.showIssuesAssignedToLoggedInUser();
return new AssignedIssuesReport(issueTable.getIssues());
} package webninja.selenuim;

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

public class ReportsPage {
private final WebDriver webDriver;

public ReportsPage(WebDriver webDriver) {
this.webDriver = webDriver;
}

public IssueTable showIssuesAssignedToLoggedInUser() {
webDriver.findElement(By.xpath("//div[@id='bodyFram']/div[3]/ul/li[2]/a")).click();
return new IssueTable(webDriver);

}
} package webninja.stepdefinitions;

import cuke4duke.Table;
import webninja.Issue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TableConverter {
public List<Issue> toIssues(Table propertiesList) {
List<Issue> list = new ArrayList<Issue>();
for (Map<String, String> properties : propertiesList.hashes()) {

Issue issue = new Issue(
properties.get("Severity").trim(),
properties.get("Title").trim());
list.add(issue);
}
return list;
}
} package webninja.workflow;

import webninja.AssignedIssuesReport;
import webninja.Issue;
import webninja.selenuim.*;

import java.util.List;

public class ProjectWorkflow {
private final CodeTrack codeTrack;
private String projectName;

public ProjectWorkflow(CodeTrack codeTrack) {

this.codeTrack = codeTrack;
}

public ProjectWorkflow addIssues(List<Issue> issues) {
for (Issue issue : issues) {
IssueForm issueForm = codeTrack.showNewIssueForm();
issueForm.setProjectName(projectName);
issueForm.setTitle(issue.getTitle());
issueForm.setDescription(issue.getTitle());
issueForm.setSeverity(issue.getSeverity());
codeTrack.submit();
}
return this;
}

public String getProjectName() {
return projectName;
}

public ProjectWorkflow assignIssueToUser(String issueTitle, String user) {
HomePage homePage = codeTrack.gotoHomePage();
codeTrack.setProjectName(projectName);
IssuePage issuePage = homePage.showIssueWithTitle(issueTitle);
IssueForm issueForm = issuePage.startEdit();
issueForm.assignTo(user);
codeTrack.submit();
return this;
}

public AssignedIssuesReport viewAssignedIssuesReport() {
ReportsPage reportsPage = codeTrack.gotoReportsPage();
IssueTable issueTable = reportsPage.showIssuesAssignedToLoggedInUser();
return new AssignedIssuesReport(issueTable.getIssues());
}

public void setCurrentProject(String project) {
this.projectName = project;
codeTrack.setProjectName(projectName);
}
} Download the pdf
Read online
Aim to O/S on git hub Where were we again?
Full transcript