package com.gosch.addressbook;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class GroupCreationTests {
   FirefoxDriver wd;

   @BeforeMethod
   public void setUp() throws Exception {
      System.setProperty("webdriver.gecko.driver", "C:\\Users\\Georgi\\Documents\\Firefox Driver\\geckodriver.exe");
      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 testGroupCreation() {
      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("test2");
      wd.findElement(By.name("group_footer")).click();
      wd.findElement(By.name("group_footer")).clear();
      wd.findElement(By.name("group_footer")).sendKeys("test3");
   }

   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;
      }
   }
}

@@ -1173,24 +1173,156 @@
 reation() %7B%0A
+      // %D0%9F%D0%B5%D1%80%D0%B2%D1%8B%D0%B9 %D0%BC%D0%B5%D1%85%D0%B0%D0%BD%D0%B8%D0%B7%D0%BC %D1%80%D0%B5%D1%84%D0%B0%D0%BA%D1%82%D0%BE%D1%80%D0%B8%D0%BD%D0%B3%D0%B0 - %D0%B2%D1%8B%D0%B4%D0%B5%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5 %D0%B2%D1%81%D0%BF%D0%BE%D0%BC%D0%BE%D0%B3%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D0%BD%D1%8B%D1%85 %D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2%0A      // Refactoring Mechanism - creating helping methods%0A
       gotoGr

Comment about refactoring.