package ru.stqa.sch.addressbook;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.*;

public class GroupCreationTests {
    FirefoxDriver wd;

    @BeforeMethod
    public void setUp() throws Exception {
        wd = new FirefoxDriver();
        wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        wd.get("http://localhost/addressbook/");
        login();
    }

    private void login() {
        wd.findElement(By.name("user")).click();
        wd.findElement(By.name("user")).clear();
        wd.findElement(By.name("user")).sendKeys("admin");
        wd.findElement(By.name("pass")).click();
        wd.findElement(By.name("pass")).clear();
        wd.findElement(By.name("pass")).sendKeys("secret");
        wd.findElement(By.xpath("//form[@id='LoginForm']/input[3]")).click();
    }

    @Test
    public void testGroupCreationTests() {
        gotoGroupPage();
        initGroupCreation();
        fillGroupForm();
        submitGroupCreation();
        returnToGroupPage();
    }

    private void returnToGroupPage() {
        wd.findElement(By.linkText("group page")).click();
    }

    private void submitGroupCreation() {
        wd.findElement(By.name("submit")).click();
    }

    private void fillGroupForm() {
        wd.findElement(By.name("group_name")).click();
        wd.findElement(By.name("group_name")).clear();
        wd.findElement(By.name("group_name")).sendKeys("test1");
        wd.findElement(By.name("group_header")).click();
        wd.findElement(By.name("group_header")).clear();
        wd.findElement(By.name("group_header")).sendKeys("header1");
        wd.findElement(By.name("group_footer")).click();
        wd.findElement(By.name("group_footer")).clear();
        wd.findElement(By.name("group_footer")).sendKeys("footer1");
    }

    private void initGroupCreation() {
        wd.findElement(By.name("new")).click();
    }

    private void gotoGroupPage() {
        wd.findElement(By.linkText("groups")).click();
    }

    @AfterMethod
    public void tearDown() {
        wd.quit();
    }

    public static boolean isAlertPresent(FirefoxDriver wd) {
        try {
            wd.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }
}

@@ -555,16 +555,33 @@
   login(
+%22admin%22, %22secret%22
 );%0A    %7D
@@ -605,16 +605,48 @@
 d login(
+String username, String password
 ) %7B%0A    
@@ -788,23 +788,24 @@
 endKeys(
-%22admin%22
+username
 );%0A     
@@ -946,24 +946,24 @@
 endKeys(
-%22secret%22
+password
 );%0A     
@@ -1171,16 +1171,45 @@
 oupForm(
+%22test1%22, %22header1%22, %22footer1%22
 );%0A     
@@ -1505,16 +1505,57 @@
 oupForm(
+String name, String header, String footer
 ) %7B%0A    
@@ -1715,23 +1715,20 @@
 endKeys(
-%22test1%22
+name
 );%0A     
@@ -1893,25 +1893,22 @@
 endKeys(
-%22
 header
-1%22
 );%0A     
@@ -2073,25 +2073,22 @@
 endKeys(
-%22
 footer
-1%22
 );%0A    %7D

refactor of "testGroupCreationTests " / parameters extraction