Skip to content

Instantly share code, notes, and snippets.

@pieceofquality
Created September 6, 2016 22:12
Show Gist options
  • Save pieceofquality/a7b4a9581ff1df7e41ab83ed4a32cabd to your computer and use it in GitHub Desktop.
Save pieceofquality/a7b4a9581ff1df7e41ab83ed4a32cabd to your computer and use it in GitHub Desktop.
package com.pieceofquality.addressbook.appmanager;
import com.pieceofquality.addressbook.model.ContactData;
import com.pieceofquality.addressbook.model.Contacts;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class ContactHelper extends HelperBase {
public ContactHelper(WebDriver wd) {
super(wd);
}
public void submitContactCreation() {
click(By.xpath("//div[@id='content']/form/input[21]"));
}
public void fillContactForm(ContactData contactData, boolean creation) {
type(By.name("firstname"), contactData.getFirstName());
type(By.name("lastname"), contactData.getLastName());
if (creation) {
new Select(wd.findElement(By.name("new_group"))).selectByVisibleText(contactData.getGroup());
} else {
Assert.assertFalse(isElementPresent(By.name("new_group")));
}
}
public void initContactCreation() {
click(By.cssSelector("li.all:nth-child(2) > a:nth-child(1)"));
}
public void deleteSelectedContact() {
click(By.cssSelector("div.left:nth-child(8)"));
wd.switchTo().alert().accept();
}
public void selectContactById(int id) {
wd.findElement(By.cssSelector("input[value='" + id + "']")).click();
}
public void initContactModification() {
click(By.cssSelector(".center img[title=\"Edit\"]:nth-of-type(1)"));
}
public void submitContactMofication() {
click(By.cssSelector("[value=\"Update\"]"));
}
public int getContactCount() {
return wd.findElements(By.name("selected[]")).size();
}
public void create(ContactData contact) {
initContactCreation();
fillContactForm(contact, true);
submitContactCreation();
returnToHomePage();
}
private void returnToHomePage() {
click(By.linkText(("home")));
}
public void modify(ContactData contact) {
selectContactById(contact.getId());
initContactModification();
fillContactForm(contact,false);
submitContactMofication();
returnToHomePage();
}
public void delete(ContactData contact) {
selectContactById(contact.getId());
deleteSelectedContact();
returnToHomePage();
}
public Contacts all() {
Contacts contacts = new Contacts();
List<WebElement> elements = wd.findElements(By.xpath("//tr[@name='entry']"));
for (WebElement element : elements) {
int id = Integer.parseInt(element.findElement(By.tagName("input")).getAttribute("value"));
String firstName = element.findElement(By.xpath("./td[3]")).getText();
String lastName = element.findElement(By.xpath("./td[2]")).getText();
contacts.add(new ContactData().withId(id).withFirstName(firstName).withLastName(lastName));
}
return contacts;
}
public boolean isThereAContact() {
return isElementPresent(By.name("selected[]"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment