package pt.fccn.sobre.arquivo.pages;

import java.io.FileNotFoundException;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SuggestionPage {

	WebDriver driver;
	private final int timeout = 25;
	
	public SuggestionPage( WebDriver driver ) throws FileNotFoundException{
		this.driver = driver;
	}
	
	
	public boolean sendSuggestion( String language ) {
		System.out.println( "[sendSuggestion]" );
		try{
			String xpathSites 		= "//*[@id=\"wpcf7-f2384-p2063-o1\"]/form/div[2]/span/textarea";
			String xpathDescription = "//*[@id=\"wpcf7-f2384-p2063-o1\"]/form/div[3]/span/input";
			String xpathEmail 		= "//*[@id=\"wpcf7-f2384-p2063-o1\"]/form/div[4]/span/input";
			String xpathSendButton 	= "//*[@id=\"wpcf7-f2384-p2063-o1\"]/form/p/input";
			String xpathCaptcha 	= "//*[@id=\"recaptcha-anchor\"]/div[5]";
			
	      	WebElement UrlsElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 25 seconds should throw RunTimeExcpetion*/
	            .until( 
	            		ExpectedConditions.presenceOfElementLocated( 
	            				By.xpath( xpathSites ) ) );             
	      	UrlsElement.clear( );
	      	UrlsElement.sendKeys( "[Test Selenium] Url field." );
	        
	        WebElement descriptionElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 25 seconds should throw RunTimeExcpetion*/
	            .until(
	            		ExpectedConditions.presenceOfElementLocated( 
	            				By.xpath( xpathDescription ) ) );
	        descriptionElement.clear( );
	        descriptionElement.sendKeys( "[Test Selenium] Description field." );
	
	        WebElement emailElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 25 seconds should throw RunTimeExcpetion*/
	                .until(
	                		ExpectedConditions.presenceOfElementLocated( 
	                				By.xpath( xpathEmail ) ) );
	        emailElement.clear( );
	        emailElement.sendKeys( "[Test Selenium] Email field." );
	        
	        driver.findElement( By.xpath( xpathCaptcha ) ).click( );
	        
	      	try {
	            Thread.sleep( 4000 );                 //wait for page to load
	      	} catch( InterruptedException ex ) {
	            Thread.currentThread( ).interrupt( );
	        }
	        
	        WebElement btnSubmitElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 25 seconds should throw RunTimeExcpetion*/
	            .until(
	            		ExpectedConditions.presenceOfElementLocated(
	            				By.id( xpathSendButton ) ) );
	        btnSubmitElement.click( );
	            
			return true;
			
		}catch( NoSuchElementException e ){
	    	System.out.println( "Could not find the link element" );
	    	throw e;
	    }
	
	}
	
	
	
	
}

@@ -1040,16 +1040,109 @@
 iv%5B5%5D%22;%0A
+%09%09%09String xpathiframe%09%09= %22//*%5B@id=%5C%22wpcf7-f2384-p2063-o1%5C%22%5D/form/div%5B5%5D/div/div/div/iframe%22;%0A
 %09%09%09%0A%09   
@@ -2294,64 +2294,562 @@
     
-driver.findElement( By.xpath( xpathCaptcha ) ).click( );
+WebElement iframeSwitch = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 25 seconds should throw RunTimeExcpetion*/%0A%09                .until(%0A%09                %09%09ExpectedConditions.presenceOfElementLocated( %0A%09                %09%09%09%09By.xpath( xpathiframe ) ) );%0A%09        %0A%09        driver.switchTo( ).frame( iframeSwitch ); // switch to Captcha iframe%0A%09        System.out.println( %22Switched to iframe%22 );%0A%09        driver.findElement( By.xpath( xpathCaptcha ) ).click( );%0A%09        %0A%09        driver.switchTo( ).defaultContent( ); // switch default content
 %0A%09  
@@ -2903,30 +2903,16 @@
  4000 );
-              
    //wai
@@ -3039,32 +3039,50 @@
     %7D%0A%09        %0A
+%09      %09%0A%09      %09%0A
 %09        WebElem

Fix bug in Suggestion Page v2