package ru.stqa.pft.addressbook.appmanager;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import ru.stqa.pft.addressbook.model.ContactData;

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

public class ContactHelper extends HeplerBase {

    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());
        type(By.name("mobile"), contactData.getMobile());
        type(By.name("email"), contactData.getEmail());

        if (creation) {
            new org.openqa.selenium.support.ui.Select(wd.findElement(By.name("new_group"))).selectByVisibleText(contactData.getGroup());
        } else {
            Assert.assertFalse(isElementPresent(By.name("new_group")));
        }
    }

    public void initContactCreation() {
        click(By.linkText("add new"));
    }

    public void initContactModification(int index) {
        wd.findElements(By.xpath("//table[@id='maintable']//["+index+"]//a/img"));  // Изменила путь, так как выдавало ошибку о недоступности пути. Сократила.
        click(By.xpath("//table[@id='maintable']//["+index+"]//a/img"));
    }

    public void deleteContact() {
        click(By.xpath("//div[@id='content']/form[2]/input[2]"));
    }

    public void submitContactModification() {
        click(By.name("update"));
    }

    public void createContact(ContactData contact) {
        initContactCreation();
        fillContactForm(contact, true);
        submitContactCreation();
    }

    public boolean isThereAContact() {
        return isElementPresent(By.xpath("//table[@id='maintable']/tbody/tr[2]/td[8]/a/img"));
    }

    public int getContactCount() {
        return wd.findElements(By.name("//table[@id='maintable']/tbody/tr[2]/td[8]/a/img")).size();
    }

    public List<ContactData> getContactList() {
        List<ContactData> contacts = new ArrayList<ContactData>();
        List<WebElement> elements = wd.findElements(By.tagName("tr"));
        for (WebElement element : elements) {
            List<WebElement> cells = element.findElements(By.tagName("td"));
            //String lastName = element.findElements(By.name("td"));
            String lastName = cells.get(1).getText();
            String firstName = cells.get(2).getText();

            int id = Integer.parseInt (element.findElement(By.tagName("tr")).getAttribute("value"));
            //String id = element.findElement(By.tagName("tr")).getAttribute("value"); //поиск одного элемента внутри другого
            ContactData contact = new ContactData(id, firstName, lastName, null, null, null);
        contacts.add(contact);
        }
        return contacts;
    }
}

@@ -275,16 +275,57 @@
 .List;%0A%0A
+import static org.openqa.selenium.By.*;%0A%0A
 public c
@@ -480,35 +480,32 @@
 %7B%0A        click(
-By.
 xpath(%22//div%5B@id
@@ -628,27 +628,24 @@
        type(
-By.
 name(%22firstn
@@ -689,27 +689,24 @@
        type(
-By.
 name(%22lastna
@@ -748,27 +748,24 @@
        type(
-By.
 name(%22mobile
@@ -807,19 +807,16 @@
    type(
-By.
 name(%22em
@@ -937,27 +937,24 @@
 findElement(
-By.
 name(%22new_gr
@@ -1071,19 +1071,16 @@
 Present(
-By.
 name(%22ne
@@ -1163,19 +1163,16 @@
   click(
-By.
 linkText
@@ -1269,19 +1269,16 @@
 lements(
-By.
 xpath(%22/
@@ -1407,35 +1407,32 @@
 .%0A        click(
-By.
 xpath(%22//table%5B@
@@ -1526,19 +1526,16 @@
   click(
-By.
 xpath(%22/
@@ -1642,19 +1642,16 @@
   click(
-By.
 name(%22up
@@ -1901,19 +1901,16 @@
 Present(
-By.
 xpath(%22/
@@ -2030,27 +2030,24 @@
 indElements(
-By.
 name(%22//tabl
@@ -2277,27 +2277,44 @@
 ents(By.
-tagName(%22tr
+xpath(%22//tr%5B@name = 'entry'%5D
 %22));%0A   
@@ -2414,19 +2414,16 @@
 lements(
-By.
 tagName(
@@ -2667,35 +2667,54 @@
 dElement(By.
-tagName(%22tr
+xpath(%22//td%5B@class = 'center'%5D
 %22)).getAttri

ошибка NumberFormatException: null